Skip to content

Commit

Permalink
Release 0.0.1-alpha2
Browse files Browse the repository at this point in the history
  • Loading branch information
ghaabor committed Mar 28, 2017
1 parent 4917f71 commit cd5f538
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 42 deletions.
5 changes: 0 additions & 5 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
use Mix.Config

config :logger, level: :debug

config :golem, slack_token: System.get_env("SLACK_TOKEN")
config :golem, adapter: Golem.Adapter.Slack
18 changes: 11 additions & 7 deletions lib/golem.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ defmodule Golem do
The entry point for the Golem.
"""
alias Golem.CommandRegistry
alias Golem.ExampleCommands
use Application

require Logger

defmacro __using__ do
quote do
import Golem
unquote(Application.fetch_env!(:golem, :adapter)).connect()
require Golem
end
end

def start(_type, _args) do
def start(commands) do
{:ok, _registrty_pid} = CommandRegistry.start_link

ExampleCommands.Test.init
# Init all the commands the user specified.
commands |> Enum.each(&init_commands/1)

{:ok, _pid} = :erlang.apply(Application.fetch_env!(:golem, :adapter), :connect, [])
end

{:ok, _pid} = Application.fetch_env!(:golem, :adapter).connect()
defp init_commands(command) do
:erlang.apply(command, :init, [])
end
end
13 changes: 13 additions & 0 deletions lib/golem/adapter/slack.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,23 @@ defmodule Golem.Adapter.Slack do
response = get("/rtm.start")
if response.status == 200 do
:ets.new(:config, [:named_table, :public])

:ets.new(:users, [:named_table])
for user <- response.body["users"] do
:ets.insert(:users, {user["id"], user})
end

:ets.new(:channels, [:named_table])
for channel <- response.body["channels"] do
:ets.insert(:channels, {channel["id"], channel})
end
for group <- response.body["groups"] do
:ets.insert(:channels, {group["id"], group})
end
for im <- response.body["ims"] do
:ets.insert(:channels, {im["id"], im})
end

{:ok, response.body["url"]}
else
{:error, [status: response.status, body: response.body]}
Expand Down
62 changes: 38 additions & 24 deletions lib/golem/adapter/slack/socket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,9 @@ defmodule Golem.Adapter.Slack.Socket do
Logger.debug("[#{type}] #{resp}")
msg = type |> extract_message_for_type(resp)

if msg == :ping, do: socket |> pong! |> recv!

case msg["type"] do
"message" ->
command = CommandRegistry.match(msg["text"])
if command do
socket |> text!(command.function.(), msg["channel"]) |> recv!
end
"reconnect_url" ->
:ets.insert(:config, {:reconnect_url, msg["url"]})
Logger.info("New reconnect_url: #{msg["url"]}")
"presence_change" ->
[{_id, user}] = :ets.lookup(:users, msg["user"])
Logger.info("@#{user["name"]} is now #{msg["presence"]}")
"error" ->
Logger.error("#{msg["error"]["msg"]} (#{msg["error"]["code"]})")
_ ->
Logger.info("Unhandled message: [#{type or "nil"}] #{resp}")
end

socket |> recv!
socket
|> handle_message(msg)
|> recv!
end

def text!(socket, message, channel) do
Expand All @@ -56,12 +38,44 @@ defmodule Golem.Adapter.Slack.Socket do
case type do
:ping ->
Logger.info("Ping.")
:ping
%{"type" => "ping"}
:text ->
resp |> parse_response
_ ->
Logger.info("Unhandled type: #{type}")
nil
Logger.info("Unhandled type: :#{type}")
%{}
end
end

defp handle_message(socket, msg) do
case msg["type"] do
"ping" ->
socket |> pong!
"message" ->
socket |> find_and_execute_command(msg)
"reconnect_url" ->
:ets.insert(:config, {:reconnect_url, msg["url"]})
Logger.info("New reconnect_url: #{msg["url"]}")
"presence_change" ->
[{_id, user}] = :ets.lookup(:users, msg["user"])
Logger.info("@#{user["name"]} is now #{msg["presence"]}")
"user_typing" ->
[{_id, user}] = :ets.lookup(:users, msg["user"])
[{_id, channel}] = :ets.lookup(:channels, msg["channel"])
Logger.info("@#{user["name"]} is now typing in #{channel["name"] || user["name"]}")
"error" ->
Logger.error("#{msg["error"]["msg"]} (#{msg["error"]["code"]})")
_ ->
Logger.info("Unhandled message: [#{msg["type"] || "nil"}] #{msg}")
end

socket
end

defp find_and_execute_command(socket, msg) do
command = CommandRegistry.match(msg["text"])
if command do
socket |> text!(command.function.(), msg["channel"])
end
end
end
10 changes: 4 additions & 6 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Golem.Mixfile do
def project do
[
app: :golem,
version: "0.0.1-alpha1",
version: "0.0.1-alpha2",
elixir: "~> 1.4",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
Expand All @@ -23,10 +23,7 @@ defmodule Golem.Mixfile do
end

def application do
[
mod: {Golem, []},
extra_applications: [:logger]
]
[extra_applications: [:logger]]
end

defp deps do
Expand All @@ -36,7 +33,8 @@ defmodule Golem.Mixfile do
{:poison, "~> 2.0"},
{:ex_doc, "~> 0.14", only: :dev, runtime: false},
{:credo, "~> 0.7", only: [:dev, :test]},
{:coverex, "~> 1.4.10", only: :test}
{:coverex, "~> 1.4.10", only: :test},
{:git_cli, "~> 0.2", only: :dev}
]
end

Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"credo": {:hex, :credo, "0.7.1", "bd09fe5bc33357aed187a405a695b586d36b4a836d4377ce496651aa483a7c18", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, optional: false]}]},
"earmark": {:hex, :earmark, "1.2.0", "bf1ce17aea43ab62f6943b97bd6e3dc032ce45d4f787504e3adf738e54b42f3a", [:mix], []},
"ex_doc": {:hex, :ex_doc, "0.15.0", "e73333785eef3488cf9144a6e847d3d647e67d02bd6fdac500687854dd5c599f", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, optional: false]}]},
"git_cli": {:hex, :git_cli, "0.2.3", "052f0ecfc43d86fb4a750741540bdb00ac63354c6588024555be62c8ea1eff33", [:mix], []},
"hackney": {:hex, :hackney, "1.6.6", "5564b4695d48fd87859e9df77a7fa4b4d284d24519f0cd7cc898f09e8fbdc8a3", [:rebar3], [{:certifi, "1.0.0", [hex: :certifi, optional: false]}, {:idna, "4.0.0", [hex: :idna, optional: false]}, {:metrics, "1.0.1", [hex: :metrics, optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, optional: false]}]},
"idna": {:hex, :idna, "4.0.0", "10aaa9f79d0b12cf0def53038547855b91144f1bfcc0ec73494f38bb7b9c4961", [:rebar3], []},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], []},
Expand Down

0 comments on commit cd5f538

Please sign in to comment.