Class: SlackKeepPresence::Main
- Inherits:
-
Object
- Object
- SlackKeepPresence::Main
- Defined in:
- lib/slack_keep_presence.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#should_shutdown ⇒ Object
Returns the value of attribute should_shutdown.
-
#user ⇒ Object
Returns the value of attribute user.
-
#ws ⇒ Object
Returns the value of attribute ws.
Instance Method Summary collapse
- #clean_shutdown ⇒ Object
- #get_user ⇒ Object
-
#initialize(options) ⇒ Main
constructor
A new instance of Main.
- #keep_presence ⇒ Object
- #restart_connection ⇒ Object
- #set_presence_active ⇒ Object
- #start_realtime ⇒ Object
Constructor Details
#initialize(options) ⇒ Main
Returns a new instance of Main.
11 12 13 14 15 16 17 18 |
# File 'lib/slack_keep_presence.rb', line 11 def initialize() @logger = Logger.new(STDOUT) @logger.level = [:debug] ? Logger::DEBUG : Logger::INFO @client = Slack::Client.new(token: ENV['SLACK_TOKEN']) get_user keep_presence end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
9 10 11 |
# File 'lib/slack_keep_presence.rb', line 9 def client @client end |
#logger ⇒ Object
Returns the value of attribute logger.
9 10 11 |
# File 'lib/slack_keep_presence.rb', line 9 def logger @logger end |
#should_shutdown ⇒ Object
Returns the value of attribute should_shutdown.
9 10 11 |
# File 'lib/slack_keep_presence.rb', line 9 def should_shutdown @should_shutdown end |
#user ⇒ Object
Returns the value of attribute user.
9 10 11 |
# File 'lib/slack_keep_presence.rb', line 9 def user @user end |
#ws ⇒ Object
Returns the value of attribute ws.
9 10 11 |
# File 'lib/slack_keep_presence.rb', line 9 def ws @ws end |
Instance Method Details
#clean_shutdown ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/slack_keep_presence.rb', line 32 def clean_shutdown puts 'Shutting down...' @should_shutdown = true; ws.close; EM.stop; exit end |
#get_user ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/slack_keep_presence.rb', line 20 def get_user auth = client.auth_test if !auth['ok'] logger.info('Unable to authenticate') exit(1) end @user = auth['user_id'] logger.info("Authenticated as #{user}") end |
#keep_presence ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/slack_keep_presence.rb', line 40 def keep_presence Signal.trap("INT") { clean_shutdown } Signal.trap("TERM") { clean_shutdown } EM.run do start_realtime EM.add_periodic_timer(5) do next unless ws timeouter = EventMachine::Timer.new(2) do logger.info('Connection to Slack terminated, reconnecting...') restart_connection end ws.ping('detecting presence') do timeouter.cancel end end end end |
#restart_connection ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/slack_keep_presence.rb', line 118 def restart_connection @should_shutdown = true @ws&.close @ws = nil begin start_realtime rescue Faraday::ConnectionFailed, Faraday::TimeoutError logger.info('Failed to establish connection, retrying...') sleep 5 retry end end |
#set_presence_active ⇒ Object
131 132 133 134 |
# File 'lib/slack_keep_presence.rb', line 131 def set_presence_active res = client.users_setPresence(presence: 'auto', set_active: true) logger.debug(res) end |
#start_realtime ⇒ Object
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 115 116 |
# File 'lib/slack_keep_presence.rb', line 62 def start_realtime @should_shutdown = false rtm = client.post("rtm.connect", batch_presence_aware: true) ws_url = rtm['url'] @ws = Faye::WebSocket::Client.new(ws_url, nil, ping: 30) ws.on :open do |event| logger.info('Connected to Slack RealTime API') payload = { type: 'presence_sub', ids: [user] } ws.send(payload.to_json) res = client.users_getPresence(user: user) logger.debug "#{res}" end ws.on :message do |event| data = JSON.parse(event.data) if data['type'] == 'presence_change' logger.debug("Got event: #{event.data}") next unless data['user'] == user next unless data['presence'] == 'away' # get the status to make sure this isn't manual_away away_info = client.users_getPresence(user: user) logger.debug(away_info) if away_info['manual_away'] logger.info('User marked as manual_away, skipping') next end logger.info("Presence changed to #{data['presence']}") logger.info("Marking #{user} as active") set_presence_active restart_connection end end ws.on [:close, :error] do |event| next if should_shutdown logger.debug('Connection to Slack RealTime API terminated, reconnecting') sleep 5 start_realtime end end |