Class: BotMob::Bot
- Inherits:
-
Object
- Object
- BotMob::Bot
- Defined in:
- lib/bot_mob/bot.rb
Overview
## BotMob::Bot
This is the base for all bots that will connect to Slack.
Instance Attribute Summary collapse
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#user_id ⇒ Object
readonly
Returns the value of attribute user_id.
Instance Method Summary collapse
- #active? ⇒ Boolean
- #api_post(args = {}) ⇒ Object
- #closed? ⇒ Boolean
- #connect ⇒ Object
- #connected? ⇒ Boolean
- #disconnected? ⇒ Boolean
- #establish_connection ⇒ Object
- #handle(message) ⇒ Object
-
#initialize(user_id, token) ⇒ Bot
constructor
A new instance of Bot.
- #realtime_post(args = {}) ⇒ Object
- #reconnect ⇒ Object
- #refresh ⇒ Object
- #respond?(message) ⇒ Boolean
- #waiting? ⇒ Boolean
Constructor Details
#initialize(user_id, token) ⇒ Bot
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/bot_mob/bot.rb', line 9 def initialize(user_id, token) @user_id = user_id if token.nil? @state = :inactive else @token = token setup end end |
Instance Attribute Details
#message ⇒ Object (readonly)
Returns the value of attribute message.
7 8 9 |
# File 'lib/bot_mob/bot.rb', line 7 def @message end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
7 8 9 |
# File 'lib/bot_mob/bot.rb', line 7 def state @state end |
#user_id ⇒ Object (readonly)
Returns the value of attribute user_id.
7 8 9 |
# File 'lib/bot_mob/bot.rb', line 7 def user_id @user_id end |
Instance Method Details
#active? ⇒ Boolean
71 72 73 |
# File 'lib/bot_mob/bot.rb', line 71 def active? @state != :inactive end |
#api_post(args = {}) ⇒ Object
40 41 42 43 |
# File 'lib/bot_mob/bot.rb', line 40 def api_post(args = {}) logger.info(" Sending \"#{args.inspect}\"\n") client.web_client.chat_postMessage(args) end |
#closed? ⇒ Boolean
87 88 89 |
# File 'lib/bot_mob/bot.rb', line 87 def closed? @state == :closed end |
#connect ⇒ Object
28 29 30 31 |
# File 'lib/bot_mob/bot.rb', line 28 def connect logger.info "State[#{user_id}] - connecting" establish_connection end |
#connected? ⇒ Boolean
83 84 85 |
# File 'lib/bot_mob/bot.rb', line 83 def connected? @state == :connected end |
#disconnected? ⇒ Boolean
79 80 81 |
# File 'lib/bot_mob/bot.rb', line 79 def disconnected? @state != :connected end |
#establish_connection ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/bot_mob/bot.rb', line 20 def establish_connection client.start_async rescue Slack::Web::Api::Error => e logger.info e. logger.info "State[#{user_id}] - disabled" @state = :inactive end |
#handle(message) ⇒ Object
63 64 65 |
# File 'lib/bot_mob/bot.rb', line 63 def handle() # noop end |
#realtime_post(args = {}) ⇒ Object
45 46 47 48 |
# File 'lib/bot_mob/bot.rb', line 45 def realtime_post(args = {}) logger.info(" Sending \"#{args[:text]}\"\n") client.(args) end |
#reconnect ⇒ Object
33 34 35 36 37 38 |
# File 'lib/bot_mob/bot.rb', line 33 def reconnect logger.info "State[#{user_id}] - reconnecting" @client = Slack::RealTime::Client.new(token: @token) setup establish_connection end |
#refresh ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/bot_mob/bot.rb', line 50 def refresh return unless active? case true when waiting? connect when disconnected? reconnect else logger.info "State[#{user_id}] - No Change" end end |
#respond?(message) ⇒ Boolean
67 68 69 |
# File 'lib/bot_mob/bot.rb', line 67 def respond?() false end |
#waiting? ⇒ Boolean
75 76 77 |
# File 'lib/bot_mob/bot.rb', line 75 def waiting? @state == :waiting end |