-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.rb
38 lines (31 loc) · 855 Bytes
/
bot.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
require 'http'
require 'json'
require 'eventmachine'
require 'faye/websocket'
response = HTTP.post("https://slack.com/api/rtm.start", params: { token: ENV['SLACK_API_TOKEN'] })
rc = JSON.parse(response.body)
url = rc['url']
EM.run do
ws = Faye::WebSocket::Client.new(url)
ws.on :open do
p [:open]
end
ws.on :message do |event|
data = JSON.parse(event.data)
send_apology(ws, data, data['channel']) if data['text'] =~ /^[:](atsumori)[:]/
send_apology(ws, data, data['item']['channel']) if data['reaction'] =~ /^(atsumori)/
end
ws.on :close do
p [:close, event.code]
ws = nil
EM.stop
end
def send_apology(ws, data, channel)
p [:message, data]
ws.send({
type: 'message',
text: "失礼しました。熱盛と出てしまいました。",
channel: channel
}.to_json)
end
end