Class: Hg::MessageWorker

Inherits:
Workers::Base show all
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

Instance Method Details

#perform(user_id, redis_namespace, bot_class_name)

This method returns an undefined value.

Process an inbound message.

Parameters:

  • user_id (String, Integer)

    The ID representing the user on this platform.

  • redis_namespace (String)

    The redis namespace under which the message to process is nested.

  • bot_class_name (String)

    The string version of the bot's class name



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.
  raw_message = pop_raw_message(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 raw_message.empty?
    # Instantiate a message object with the raw message from the queue.
    message = Facebook::Messenger::Incoming::Message.new(raw_message)
    
    # 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 = message.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,
        message.text,
        false
      ) if ENV['CHATBASE_API_KEY']

    # If the message has attachments.
    elsif message.attachments
      attachment = message.attachments.first

      # If the attachment is coordinates.
      if attachment['type'] == 'location'
        # Generate a coordinates request with lat/long as parameters.
        request = Hg::Request.new(
          user:       user,
          message:    message,
          intent:     Hg::InternalActions::HANDLE_COORDINATES,
          action:     Hg::InternalActions::HANDLE_COORDINATES,
          parameters: {
            lat:   attachment['payload']['coordinates']['lat'],
            long:  attachment['payload']['coordinates']['long']
          }
        )

        # Set location attachment chatbase fields
        @client.set_chatbase_fields(
          request.intent,
          message.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, message)

      # Set chatbase fields for dialog action
      if ENV['CHATBASE_API_KEY']
        @client.set_chatbase_fields(
          request.action,
          message.text,
          false)
      end

    # If the message is text...
    else
      # Parse the message.
      nlu_response, params = parse_message(message.text, user)

      # Build a request.
      request = build_request(message, 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, message.text, false)
        end
      end
    end

    # Send to Chatbase if env var present
    @client.send_user_message(message) 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.
    raw_message = pop_raw_message(user_id, redis_namespace)
  end
end