X
X
Xip
Search…
route
Describes the route method in BotController.
The route method in BotController is the primary entry point for messages into your bot. This method is left open for you to edit in order for you to customize that process. Here is the method after having been generated:
1
def route
2
if current_message.payload.present?
3
handle_payloads
4
# Clear out the payload to prevent duplicate handling
5
current_message.payload = nil
6
return
7
end
8
9
# Allow devs to jump around flows and states by typing:
10
# /flow_name/state_name or
11
# /flow_name (jumps to first state) or
12
# //state_name (jumps to state in current flow)
13
# (only works for bots in development)
14
return if dev_jump_detected?
15
16
if current_session.present?
17
step_to session: current_session
18
else
19
step_to flow: 'hello', state: 'say_hello'
20
end
21
end
Copied!
The method, by default, performs three tasks:
    1.
    Handles payloads.
    2.
    Handles Dev Jumps.
    3.
    Routes a user to their existing location based on their session or starts a new session if one does not already exist.
We'll cover 1 and 3 in more detail below. You can learn more about Dev Jumps via the Dev Jumps docs.

Payloads

Payloads are used to handle things like buttons in Facebook Messenger. On other platforms, like SMS and Whatsapp, payloads might be global keywords your bot is configured to support.
Payloads have to be handled globally because a button may be tapped (or keyword typed in) at any point during a conversation. Your bot, therefore, needs to be able to handle these in any flow and state.
Line 2 in the code sample above checks if the payload field of a message is present, and if so, calls the handle_payloads method. Here is that method:
1
# Handle payloads globally since payload buttons remain in the chat
2
# and we cannot guess in which states they will be tapped.
3
def handle_payloads
4
case current_message.payload
5
when 'developer_restart', 'new_user'
6
step_to flow: 'hello', state: 'say_hello'
7
when 'goodbye'
8
step_to flow: 'goodbye'
9
end
10
end
Copied!
By setting the payload value of a Facebook Messenger button to developer_restart, for example, you can trigger the conversation to restart.
More information for handling global SMS or Whatsapp keywords and button payloads can be found in the respective documentation for each message platform component.

Routing Based on Session

In the first code sample, Lines 16-20 handle routing a user to their existing session or starting a new one. For users with an existing session, you'll likely want to keep that code the same. For users without session, you may want to customize the starting flow.
Last modified 9mo ago