Class: Hg::MessageWorker
- Inherits:
-
Workers::Base
- Object
- Workers::Base
- Hg::MessageWorker
- Defined in:
- app/workers/hg/message_worker.rb
Overview
Handles processing messages. A message is any inbound, freeform text from any platform.
Instance Method Summary collapse
-
#perform(user_id, redis_namespace, bot_class_name)
Process an inbound message.
Instance Method Details
#perform(user_id, redis_namespace, bot_class_name)
This method returns an undefined value.
Process an inbound message.
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 43 44 45 46 47 48 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 113 114 115 116 117 118 119 120 121 122 |
# File 'app/workers/hg/message_worker.rb', line 18 def perform(user_id, redis_namespace, bot_class_name) # Retrieve the latest message for this user. = (user_id, redis_namespace) # Do nothing if no message available. This could be due to multiple # execution on the part of Sidekiq. This ensures idempotence. We loop # here to ensure that this worker attempts to drain the queue for # the user. until .empty? # Instantiate a message object with the raw message from the queue. = Facebook::Messenger::Incoming::Message.new() # Instantiate chatbase client @client = ChatbaseAPIClient.new if ENV['CHATBASE_API_KEY'] # Locate the class representing the bot. bot = Kernel.const_get(bot_class_name) # Fetch the User representing the message's sender. # TODO: pass in a `user_id_field` to indicate how to find user in order to # make this platform agnostic user = find_bot_user(bot, user_id) # If the message is a quick reply... if quick_reply_payload = .quick_reply # Parse the JSON from the payload. payload = JSON.parse(quick_reply_payload) # ...build a request object from the payload. request = build_payload_request(payload, user) # Set quick reply chatbase fields @client.set_chatbase_fields( request.action, .text, false ) if ENV['CHATBASE_API_KEY'] # If the message has attachments. elsif . = ..first # If the attachment is coordinates. if ['type'] == 'location' # Generate a coordinates request with lat/long as parameters. request = Hg::Request.new( user: user, message: , intent: Hg::InternalActions::HANDLE_COORDINATES, action: Hg::InternalActions::HANDLE_COORDINATES, parameters: { lat: ['payload']['coordinates']['lat'], long: ['payload']['coordinates']['long'] } ) # Set location attachment chatbase fields @client.set_chatbase_fields( request.intent, .text, false ) if ENV['CHATBASE_API_KEY'] else # TODO: What should we do if attachments aren't recognized? end # If the user is in the middle of a dialog... elsif user.context[:dialog_action] request = build_dialog_request(user, ) # Set chatbase fields for dialog action if ENV['CHATBASE_API_KEY'] @client.set_chatbase_fields( request.action, .text, false) end # If the message is text... else # Parse the message. nlu_response, params = (.text, user) # Build a request. request = build_request(, nlu_response, params, user) # Set chatbase fields and determine not_handled if ENV['CHATBASE_API_KEY'] if !nlu_response[:intent] || request.intent == 'Default' @client.not_handled = true else @client.set_chatbase_fields(request.intent, .text, false) end end end # Send to Chatbase if env var present @client.() if ENV['CHATBASE_API_KEY'] # Send the request to the bot's router. bot.router.handle(request) if request # Attempt to pop another message from the queue for processing. = (user_id, redis_namespace) end end |