This Drupal module provides a tool to build a chat bot to work on Facebook's Messenger Platform.
This Drupal module provides a tool to build a chat bot to work on Facebook’s Messenger
Platform.
Note: You can use the demo_fb_messenger_bot folder as a starting point or
reference.
As of right now, this module can handle sending text, button, generic, and video
messages from Facebook’s Send API.
$textMessage = new TextMessage('Text Message!');
$buttonMessage = new ButtonMessage(
array(
new PostbackButton('Next Step', 'buttonMessage_Next'),
new UrlButton('URL Button', 'http://www.example.com');
)
);
$videoMessage = new VideoMessage('http://techslides.com/demos/sample-videos/small.mp4');
The meat of a workflow resides in its steps. Each step is an object responsible
for providing the workflow with a human readable name (see Roadmap), machine
name, and message(s) to send the user. In addition to those basics, steps need
to indicate what the next step to go to in the workflow based on a user’s
response. Lastly, steps should provide a validation callback which will be
invoked to validate the user’s response to the step, as well as an invalid
response message, which will be sent to the user in the event that the callback
returns false.
$stepHumanReadableName
$stepMachineName
$messageToSend = array(new TextMessage('Hi there!'));
// Set step welcoming user to conversation.
$welcomeStep = new BotWorkflowStep($humanReadableName, $stepMachineName, $messageToSend);
As mentioned above, every step needs to indicate the next step to go to based on
a user’s response to that step. This is achieved with response handlers.
$welcomeStep->setResponseHandlers(
array(
'*' => array(
'handlerMessage' => NULL,
'goto' => 'builtABot',
),
)
);
In the example above, we indicate that regardless of what the user sends us (the
asterisk), we want to proceed to the step in the workflow with machine name
‘builtABot’. By indicating ‘NULL’ for the handlerMessage, we are saying we want
to send whatever message text is configured in the builtABot step. If we want to
override that behavior, we can simply set the handlerMessage value to be an
array of messages(s) of type MessageInterface.
If you sent out a message with buttons, and want to route a user to different
steps depending on which button they clicked, you would achieve that using
response handlers:
$builtStep = new BotWorkflowStep('Built A Bot', 'builtABot',
new ButtonMessage('Glad you stopped by for a chat. Have you ever built a chat bot?',
array(
new PostbackButton('Yep!', 'builtABot_Yes'),
new PostbackButton("Nope!", 'builtABot_No'),
)
)
);
$builtStep->setResponseHandlers(
array(
'builtABot_Yes' => array(
'handlerMessage' => NULL,
'goto' => 'veteranBuilder',
),
'builtABot_No' => array(
'handlerMessage' => NULL,
'goto' => 'neverBuilt',
),
)
);
Often times, you’ll want to collect some sort of information from the user that
requires validation prior to saving it. The module ships with a few validation
callbacks you can use, but you’re free to override them, and/or
implement your own. These include:
$validationFunction = $this->getTextMessageValidatorFunction();
$invalidResponse = $this->getGenericValidationFailMessage();
$welcomeStep->setValidationCallback($validationFunction);
$welcomeStep->setInvalidResponseMessage($invalidResponse);
Add documentation on:
We’d love to: