class Relax::Bot

Public Class Methods

relax_bots_key() click to toggle source
# File lib/relax/bot.rb, line 75
def self.relax_bots_key
  ENV['RELAX_BOTS_KEY']
end
relax_bots_pubsub() click to toggle source
# File lib/relax/bot.rb, line 71
def self.relax_bots_pubsub
  ENV['RELAX_BOTS_PUBSUB']
end
start!(team_uid, token, opts = {}) click to toggle source
# 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) click to toggle source
# File lib/relax/bot.rb, line 54
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
stop!(team_uid, opts = {}) click to toggle source
# File lib/relax/bot.rb, line 32
def self.stop!(team_uid, 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
  key = namespace == "" ? team_uid : "#{namespace}-#{team_uid}"
  pubsub_payload = {type: 'team_removed', team_id: team_uid}
  pubsub_payload.merge!(namespace: namespace) if namespace != ""

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