This repository has been archived by the owner on Feb 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
nouvion.rb
68 lines (52 loc) · 1.52 KB
/
nouvion.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
$LOAD_PATH.unshift(File.dirname(File.realpath(__FILE__)) + '/lib')
require 'dotenv'
Dotenv.load
require 'goliath'
require 'faye/websocket'
require 'json'
require 'nouvion'
require 'slack'
USERS = {}
CHANNELS = {}
USERS_BY_NAME = {}
CHANNELS_BY_NAME = {}
class NouvionBot < Goliath::API
EM.run {
rtm_start = Slack::RTM.start
rtm_start['users'].each do |user|
USERS[user['id']] = user['name']
USERS_BY_NAME[user['name']] = user['id']
end
rtm_start['channels'].each do |channel|
CHANNELS[channel['id']] = channel['name']
CHANNELS_BY_NAME[channel['name']] = channel['id']
end
Slack::Users.set_presence('auto')
socket = Faye::WebSocket::Client.new(rtm_start['url'])
socket.on :open do |event|
puts 'listening'
end
socket.on :message do |event|
data = JSON.parse(event.data)
puts data
if data.has_key?('subtype') && data['subtype'] == 'bot_message'
next
end
message =
case data['type']
when 'message'
Nouvion::Handlers::Message.new(data)
when 'user_typing'
Nouvion::Handlers::UserTyping.new(data)
else
nil
end
unless message.nil?
message.handle
end
end
socket.on :close do |event|
puts 'closed'
end
}
end