forked from slack-ruby/slack-ruby-bot
/
server.rb
114 lines (101 loc) · 2.57 KB
/
server.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
module SlackRubyBot
class Server
include Loggable
cattr_accessor :hooks
attr_accessor :token, :aliases, :send_gifs
TRAPPED_SIGNALS = %w(INT TERM).freeze
include SlackRubyBot::Hooks::Hello
include SlackRubyBot::Hooks::Message
def initialize(options = {})
@token = options[:token]
@aliases = options[:aliases]
@send_gifs = options.key?(:send_gifs) ? !!options[:send_gifs] : true
end
def run
loop do
handle_execeptions do
handle_signals
start!
end
end
end
def start!
@stopping = false
@async = false
client.start!
end
def start_async
@stopping = false
@async = true
client.start_async
end
def stop!
@stopping = true
client.stop! if @client
end
def hello!
return unless client.self && client.team
logger.info "Welcome '#{client.self.name}' to the '#{client.team.name}' team at https://#{client.team.domain}.slack.com."
end
def restart!(wait = 1)
@async ? start_async : start!
rescue StandardError => e
sleep wait
logger.error "#{e.message}, reconnecting in #{wait} second(s)."
logger.debug e
restart! [wait * 2, 60].min
end
private
def handle_execeptions
yield
rescue Slack::Web::Api::Error => e
logger.error e
case e.message
when 'migration_in_progress'
sleep 1 # ignore, try again
else
raise e
end
rescue Faraday::Error::TimeoutError, Faraday::Error::ConnectionFailed, Faraday::Error::SSLError => e
logger.error e
sleep 1 # ignore, try again
rescue StandardError => e
logger.error e
raise e
ensure
@client = nil
end
def handle_signals
TRAPPED_SIGNALS.each do |signal|
Signal.trap(signal) do
stop!
exit
end
end
end
def client
@client ||= begin
client = SlackRubyBot::Client.new(aliases: aliases, send_gifs: send_gifs, token: token)
client.on :close do |_data|
@client = nil
restart! unless @stopping
end
hooks.each do |hook|
client.on hook do |data|
begin
send hook, client, data
rescue StandardError => e
logger.error e
begin
client.message(channel: data['channel'], text: e.message) if data.key?('channel')
rescue
# ignore
end
end
end
end
client
end
end
end
end