Skip to content
Permalink
Browse files

implement basic chat service that knows how to send messages to a cha…

…t provider
  • Loading branch information...
imbriaco authored and nmohoric committed Feb 21, 2017
1 parent 0541dc9 commit 3b4c58828b2a09f9b57f89dc0cf3593c4b02cc83
@@ -0,0 +1,38 @@
defmodule Cog.Command.Service.Chat do
require Logger

alias Cog.Chat.Adapter

def send_message(destination, message) do
{:ok, provider} = Cog.Util.Misc.chat_provider_module

case build_target(provider, destination) do
{:ok, target} ->
Cog.Chat.Adapter.send("slack", target, message, %{originating_room_id: target.id})
{:error, reason} ->
Logger.debug("Invalid message destination: #{inspect destination}")
{:error, reason}
end
end

defp build_target(provider, "chat://@" <> handle) do
case Adapter.lookup_user(provider, handle) do
{:ok, %{id: user_id}} ->
{:ok, %{provider: provider, id: user_id, name: handle, is_dm: true}}
_ ->
{:error, :unknown_handle}
end
end
defp build_target(provider, "chat://#" <> room) do
case Adapter.lookup_room(provider, name: "##{room}") do
{:ok, %{id: room_id}} ->
{:ok, %{provider: provider, id: room_id, name: room, is_dm: false}}
_ ->
{:error, :unknown_room}
end
end
defp build_target(_provider, _destination) do
{:error, :invalid_destination}
end

end
@@ -12,8 +12,10 @@ defmodule Cog.Repository.Services do
# TODO: Eventually, we'll want to introspect the system for this
# information. For now, though, this will be effectively duplicating
# information contained in the service router.
[%{name: "memory",
version: "1.0.0"}]
[
%{name: "memory", version: "1.0.0"},
%{name: "chat", version: "1.0.0"}
]
end

def deployed(name),
@@ -0,0 +1,32 @@
defmodule Cog.V1.ChatServiceController do
use Cog.Web, :controller
require Logger

plug Cog.Plug.ServiceAuthentication

alias Cog.Command.Service.Chat

def send_message(conn, %{"destination" => destination, "message" => message}) do
case Chat.send_message(destination, message) do
{:error, reason} when reason == :unknown_handle or reason == :unknown_room or reason == :invalid_destination ->
conn
|> put_status(:not_found)
|> json(%{"error" => destination_error(destination, reason)})
_ ->
conn
|> put_status(:ok)
|> json(%{"status": "sent"})
end
end

defp destination_error(destination, :unknown_handle) do
"Unable to find chat user for #{destination}"
end
defp destination_error(destination, :unknown_room) do
"Unable to find chat room for #{destination}"
end
defp destination_error(destination, _reason) do
"Invalid chat destination URI #{destination}"
end

end
@@ -17,5 +17,6 @@ defmodule Cog.ServiceRouter do
put "/memory/1.0.0/:key", V1.MemoryServiceController, :update
post "/memory/1.0.0/:key", V1.MemoryServiceController, :change

post "/chat/1.0.0/send_message", V1.ChatServiceController, :send_message
end
end

0 comments on commit 3b4c588

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.