Class: Lita::Handlers::SlackHandler
- Inherits:
-
Handler
- Object
- Handler
- Lita::Handlers::SlackHandler
- Defined in:
- lib/lita/handlers/slack_handler.rb
Class Method Summary collapse
-
.default_config(default) ⇒ Object
Class method called by Lita for handler configuration.
Instance Method Summary collapse
- #config ⇒ Object
- #config_valid? ⇒ Boolean
-
#log ⇒ Object
Accessor shortcuts.
- #receive(req, res) ⇒ Object
- #request_valid?(req) ⇒ Boolean
Class Method Details
.default_config(default) ⇒ Object
Class method called by Lita for handler configuration
10 11 12 13 14 |
# File 'lib/lita/handlers/slack_handler.rb', line 10 def self.default_config(default) # Slack:Outgoing WebHook integration token default.webhook_token = nil default.team_domain = nil end |
Instance Method Details
#config ⇒ Object
77 78 79 |
# File 'lib/lita/handlers/slack_handler.rb', line 77 def config Lita.config.handlers.slack_handler end |
#config_valid? ⇒ Boolean
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/lita/handlers/slack_handler.rb', line 44 def config_valid? valid = true if config.webhook_token.nil? log.error 'SlackHandler: refuse to run; missing config "webhook_token"' valid = false end if config.team_domain.nil? log.error 'SlackHandler: refuse to run; missing "team_domain"' valid = false end return valid end |
#log ⇒ Object
Accessor shortcuts
73 74 75 |
# File 'lib/lita/handlers/slack_handler.rb', line 73 def log Lita.logger end |
#receive(req, res) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/lita/handlers/slack_handler.rb', line 16 def receive(req, res) # For security, do not run with missing config if not config_valid? res.status = 500 return end log.debug 'SlackHandler::receive started' # Validate request if not request_valid?(req) res.status = 403 return end res.status = 200 log.debug "SlackHandler::receive webhook_token:#{req['token']} team_domain:#{req['team_domain']}" log.debug "SlackHandler::receive user id:#{req['user_id']} name:#{req['user_name']}" log.debug "SlackHandler::receive room id:#{req['channel_id']} name:#{req['channel_name']}" log.debug "SlackHandler::receive message text size #{req['text'].size} byte(s)" user = User.create(req['user_id'], name: req['user_name'], mention_name: req['user_name']) # Register channel using Lita::User class room = User.create(req['channel_id'], name: req['channel_name']) source = Source.new(user: user, room: room.id) = Message.new(robot, req['text'], source) # Route the message to the adapter log.info 'SlackHandler::receive routing message to the adapter' robot.receive log.debug 'SlackHandler::receive ending' end |
#request_valid?(req) ⇒ Boolean
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/lita/handlers/slack_handler.rb', line 57 def request_valid?(req) valid = true if req['token'] != config.webhook_token log.warn "SlackHandler: ignoring request; token does not match (received:#{req['token']})" valid = false end if req['team_domain'] != config.team_domain log.warn "SlackHandler: ignoring request; team domain does not match (received:#{req['team_domain']})" valid = false end return valid end |