Skip to content

Commit 42bb0f5

Browse files
committed
echo example
1 parent e8bb437 commit 42bb0f5

File tree

8 files changed

+168
-0
lines changed

8 files changed

+168
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/_build
2+
/deps
3+
erl_crash.dump
4+
*.ez

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Exbot
2+
=====
3+
4+
Register a bot then `Exbot.run(<bot_token>)`

config/config.exs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# This file is responsible for configuring your application
2+
# and its dependencies with the aid of the Mix.Config module.
3+
use Mix.Config
4+
5+
# This configuration is loaded before any dependency and is restricted
6+
# to this project. If another project depends on this project, this
7+
# file won't be loaded nor affect the parent project. For this reason,
8+
# if you want to provide default values for your application for third-
9+
# party users, it should be done in your mix.exs file.
10+
11+
# Sample configuration:
12+
#
13+
# config :logger, :console,
14+
# level: :info,
15+
# format: "$date $time [$level] $metadata$message\n",
16+
# metadata: [:user_id]
17+
18+
# It is also possible to import configuration files, relative to this
19+
# directory. For example, you can emulate configuration per environment
20+
# by uncommenting the line below and defining dev.exs, test.exs and such.
21+
# Configuration from the imported file will override the ones defined
22+
# here (which is why it is important to import them last).
23+
#
24+
# import_config "#{Mix.env}.exs"
25+
#

lib/exbot.ex

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
defmodule Exbot do
2+
3+
defp start!(token) do
4+
{:ok, response} = HTTPoison.get "https://slack.com/api/rtm.start?token=" <> token, [], timeout: 10000
5+
response
6+
end
7+
8+
defp parse(response) do
9+
{:ok, json} = response.body
10+
|> Poison.Parser.parse
11+
json
12+
end
13+
14+
defp get_config(json) do
15+
<<bot_id::size(72)>> = json["self"]["id"]
16+
%{url: json["url"], bot_id: bot_id}
17+
end
18+
19+
defp connect(%{url: url} = state) do
20+
uri = URI.parse url
21+
{:ok, socket} = Socket.Web.connect uri.host, path: uri.path, secure: true
22+
state
23+
|> Map.put(:socket, socket)
24+
|> Map.put(:msg_id, 1)
25+
end
26+
27+
defp send_text(socket, id, channel, text) do
28+
socket
29+
|> Socket.Web.send! {:text, """
30+
{
31+
"id": #{id},
32+
"type": "message",
33+
"channel": "#{channel}",
34+
"text": "#{text}"
35+
}
36+
"""
37+
}
38+
end
39+
40+
defp handle(state) do
41+
state.socket
42+
|> Socket.Web.recv!
43+
|> process(state)
44+
end
45+
46+
defp process({:text, raw_json}, state) do
47+
raw_json
48+
|> Poison.Parser.parse!
49+
|> log
50+
|> do_process(state)
51+
|> handle
52+
end
53+
54+
defp log(msg) do
55+
IO.inspect(msg)
56+
msg
57+
end
58+
59+
defp process(_msg, state) do
60+
handle(state)
61+
end
62+
63+
defp do_process(%{
64+
"type" => "message",
65+
"channel" => channel,
66+
"user" => user,
67+
"text" => << ?<, ?@, bot_id::size(72), ?>, ?: >> <> text},
68+
%{socket: socket, bot_id: bot_id, msg_id: msg_id} = state) do
69+
send_text(socket, msg_id, channel, "<@#{user}>:#{text}")
70+
%{state | msg_id: msg_id+1}
71+
end
72+
73+
defp do_process(_json, state) do
74+
state
75+
end
76+
77+
def run(token) do
78+
token
79+
|> start!
80+
|> parse
81+
|> get_config
82+
|> connect
83+
|> handle
84+
end
85+
86+
end

mix.exs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
defmodule Exbot.Mixfile do
2+
use Mix.Project
3+
4+
def project do
5+
[app: :exbot,
6+
version: "0.0.1",
7+
elixir: "~> 1.0",
8+
deps: deps]
9+
end
10+
11+
# Configuration for the OTP application
12+
#
13+
# Type `mix help compile.app` for more information
14+
def application do
15+
[applications: [:logger, :socket, :httpoison]]
16+
end
17+
18+
# Dependencies can be Hex packages:
19+
#
20+
# {:mydep, "~> 0.3.0"}
21+
#
22+
# Or git/path repositories:
23+
#
24+
# {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"}
25+
#
26+
# Type `mix help deps` for more examples and options
27+
defp deps do
28+
[
29+
{:socket, github: "meh/elixir-socket"},
30+
{:httpoison, "~> 0.5"},
31+
{:poison, "~> 1.3.0"}
32+
]
33+
end
34+
end

mix.lock

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
%{"hackney": {:hex, :hackney, "0.14.3"},
2+
"httpoison": {:hex, :httpoison, "0.5.0"},
3+
"httpotion": {:hex, :httpotion, "1.0.0"},
4+
"ibrowse": {:git, "git://github.com/cmullaparthi/ibrowse.git", "7871e2ebe8811efb64e1917e2de455fa87e8dfe3", [tag: "v4.1.0"]},
5+
"idna": {:hex, :idna, "1.0.1"},
6+
"poison": {:hex, :poison, "1.3.0"},
7+
"socket": {:git, "git://github.com/meh/elixir-socket.git", "49b8604bbccd58a2e578e1b6b63c0e55ba79e7f1", []}}

test/exbot_test.exs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
defmodule ExbotTest do
2+
use ExUnit.Case
3+
4+
test "the truth" do
5+
assert 1 + 1 == 2
6+
end
7+
end

test/test_helper.exs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ExUnit.start()

0 commit comments

Comments
 (0)