Class: SlackKeepPresence::Main

Inherits:
Object
  • Object
show all
Defined in:
lib/slack_keep_presence.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options)
  @logger = Logger.new(STDOUT)
  @logger.level = options[:debug] ? Logger::DEBUG : Logger::INFO
  @client = Slack::Client.new(token: ENV['SLACK_TOKEN'])

  get_user
  keep_presence
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



9
10
11
# File 'lib/slack_keep_presence.rb', line 9

def client
  @client
end

#loggerObject

Returns the value of attribute logger.



9
10
11
# File 'lib/slack_keep_presence.rb', line 9

def logger
  @logger
end

#should_shutdownObject

Returns the value of attribute should_shutdown.



9
10
11
# File 'lib/slack_keep_presence.rb', line 9

def should_shutdown
  @should_shutdown
end

#userObject

Returns the value of attribute user.



9
10
11
# File 'lib/slack_keep_presence.rb', line 9

def user
  @user
end

#wsObject

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_shutdownObject



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_userObject



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_presenceObject



40
41
42
43
44
45
46
47
# 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
  end
end

#set_activeObject



114
115
116
117
118
119
120
121
# File 'lib/slack_keep_presence.rb', line 114

def set_active
  logger.info("Marking #{user} as active")
  res = client.users_setPresence(presence: 'auto', set_active: true)
  logger.debug(res)

  res = client.users_getPresence(user: user, set_active: true)
  logger.debug(res)
end

#start_realtimeObject



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
# File 'lib/slack_keep_presence.rb', line 49

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.debug('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']}")

      # Slack's setActive api doesn't seem to be working.
      # For now just reestablish the realtime connection
      # which automatically sets you as active
      # set_active
      # res = client.users_getPresence(user: user)
      # logger.debug "#{res}"

      logger.info("Marking #{user} as active")
      ws.close
      @ws = nil
      @should_shutdown = true
      start_realtime
    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