Class: Relax::Bot

Inherits:
Base
  • Object
show all
Defined in:
lib/relax/bot.rb

Class Method Summary collapse

Methods inherited from Base

redis

Class Method Details

.relax_bots_keyObject



53
54
55
# File 'lib/relax/bot.rb', line 53

def self.relax_bots_key
  ENV['RELAX_BOTS_KEY']
end

.relax_bots_pubsubObject



49
50
51
# File 'lib/relax/bot.rb', line 49

def self.relax_bots_pubsub
  ENV['RELAX_BOTS_PUBSUB']
end

.start!(team_uid, token, opts = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/relax/bot.rb', line 8

def self.start!(team_uid, token, opts = {})
  if relax_bots_key.nil? || relax_bots_key == ""
    raise BotsKeyNotSetError, "Environment Variable RELAX_BOTS_KEY is not set"
  end

  if relax_bots_pubsub.nil? || relax_bots_pubsub == ""
    raise BotsPubsubNotSetError, "Environment Variable RELAX_BOTS_PUBSUB is not set"
  end

  namespace = (opts[:namespace] || opts['namespace']).to_s.strip
  hset_payload = {team_id: team_uid, token: token}
  hset_payload.merge!(namespace: namespace) if namespace != ""
  key = namespace == "" ? team_uid : "#{namespace}-#{team_uid}"
  pubsub_payload = {type: 'team_added', team_id: team_uid}
  pubsub_payload.merge!(namespace: namespace) if namespace != ""

  redis.with do |conn|
    conn.multi do
      conn.hset(relax_bots_key, key, hset_payload.to_json)
      conn.publish(relax_bots_pubsub, pubsub_payload.to_json)
    end
  end
end

.start_typing!(team_uid, channel_uid) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/relax/bot.rb', line 32

def self.start_typing!(team_uid, channel_uid)
  if relax_bots_key.nil? || relax_bots_key == ""
    raise BotsKeyNotSetError, "Environment Variable RELAX_BOTS_KEY is not set"
  end

  if relax_bots_pubsub.nil? || relax_bots_pubsub == ""
    raise BotsPubsubNotSetError, "Environment Variable RELAX_BOTS_PUBSUB is not set"
  end

  message_id = SecureRandom.uuid
  payload = { id: message_id, type: 'typing', channel: channel_uid }.to_json

  redis.with do |conn|
    conn.publish(relax_bots_pubsub, {type: 'message', team_id: team_uid, id: message_id, payload: payload}.to_json)
  end
end