lib/ruboty/adapters/slack_rtm.rb in ruboty-slack_rtm-2.7.2 vs lib/ruboty/adapters/slack_rtm.rb in ruboty-slack_rtm-2.9.0

- old
+ new

@@ -10,16 +10,16 @@ env :SLACK_TOKEN, "Account's token. get one on https://api.slack.com/web#basics" env :SLACK_EXPOSE_CHANNEL_NAME, "if this set to 1, message.to will be channel name instead of id", optional: true env :SLACK_IGNORE_BOT_MESSAGE, "If this set to 1, bot ignores bot_messages", optional: true env :SLACK_IGNORE_GENERAL, "if this set to 1, bot ignores all messages on #general channel", optional: true env :SLACK_GENERAL_NAME, "Set general channel name if your Slack changes general name", optional: true + env :SLACK_AUTO_RECONNECT, "Enable auto reconnect", optional: true def run init bind connect - # TODO: reconnect end def say(message) channel = message[:to] if channel[0] == '#' @@ -45,10 +45,14 @@ mrkdwn: true ) end end + def add_reaction(reaction, channel_id, timestamp) + client.reactions_add(name: reaction, channel: channel_id, timestamp: timestamp) + end + private def init response = client.auth_test @user_info_caches = {} @@ -75,15 +79,20 @@ sleep 5 set_active end end - realtime.main_loop + loop do + realtime.main_loop rescue nil + break unless ENV['SLACK_AUTO_RECONNECT'] + @url = null + @realtime = null + end end def url @url ||= begin - response = Net::HTTP.post_form(URI.parse('https://slack.com/api/rtm.start'), token: ENV['SLACK_TOKEN']) + response = Net::HTTP.post_form(URI.parse('https://slack.com/api/rtm.connect'), token: ENV['SLACK_TOKEN']) body = JSON.parse(response.body) URI.parse(body['url']) end end