• This repository has been archived on 03/Mar/2022
  • Stars
    star
    562
  • Rank 78,991 (Top 2 %)
  • Language
    PHP
  • License
    Other
  • Created almost 8 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

This Drupal module provides a tool to build a chat bot to work on Facebook's Messenger Platform.

Features

This Drupal module provides a tool to build a chat bot to work on Facebook's Messenger Platform.

Requirements

  • Drupal 8
  • PHP 5.6 or greater
  • Facebook App and Page

Installation

  1. Enable the fb_messenger_bot module.
  2. Visit /admin/config/fb-messenger and enter a custom value for the Facebook Verify Token (save the configuration).
  3. Complete steps 1-4 outlined in Facebook's quickstart documentation including the copying of the page access token in step 3 (note: this module sets up the webhook at /webhook/contact).
  4. Return to /admin/config/fb-messenger and paste the page access token into the Facebook Page Access Token field (save the configuration).
  5. Clear caches.
  6. Visit the Facebook page your app is subscribed to and send it a message. You should receive a response from the bot!

Customizing your bot

Note: You can use the demo_fb_messenger_bot folder as a starting point or reference.

  1. In a custom module, create a custom workflow by creating a class that extends the FBMessengerBotWorkflow class.
  2. Modify the fb_messenger_bot.workflow service to use your custom workflow by creating a class that extends ServiceProviderBase and implements the alter() method (documentation from d.o.).

Supported message types

As of right now, this module can handle sending text, button, generic, and video messages from Facebook's Send API.

Text message

$textMessage =  new TextMessage('Text Message!');

Button message

$buttonMessage = new ButtonMessage(
  array(
    new PostbackButton('Next Step', 'buttonMessage_Next'),
    new UrlButton('URL Button', 'http://www.example.com');
  )
);

Video message

$videoMessage = new VideoMessage('http://techslides.com/demos/sample-videos/small.mp4');

Steps

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.

Instantiating a step in your workflow

$stepHumanReadableName
$stepMachineName
$messageToSend = array(new TextMessage('Hi there!'));

// Set step welcoming user to conversation.
$welcomeStep = new BotWorkflowStep($humanReadableName, $stepMachineName, $messageToSend);

Adding response handlers

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',
    ),
  )
);

Step validation

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:

  • text message (ensure the user sent text)
  • postback (ensure the user clicked one of the buttons you sent them)
  • zip code
  • e-mail
  • phone number (U.S.)
$validationFunction = $this->getTextMessageValidatorFunction();
$invalidResponse = $this->getGenericValidationFailMessage();
$welcomeStep->setValidationCallback($validationFunction);
$welcomeStep->setInvalidResponseMessage($invalidResponse);

Roadmap

Documentation Improvements

Add documentation on:

  • starting over a conversation
  • fetching and storing a user's info from Facebook
  • pre and post conversation process methods
  • handling trolling users

Enhancements and hopes

We'd love to:

  • Refactor as much of the code as possible to be usable in a general PHP context, outside of Drupal.
  • Convert objects to be Drupal entities where appropriate. Refactoring steps to being config entities, for example, would allow for configuration of steps by an admin.
  • Support multiple workflows (e.g. you could have a intro workflow with certain steps and then transition a user to a gather contact info workflow with a different set of steps etc.)
  • See you all fork and contribute to the module! (It would be great to see someone from the community polish the code and contribute it to drupal.org).

More Repositories

1

api-standards

3,079
star
2

petitions

Drupal installation profile powering We The People at petitions.whitehouse.gov
JavaScript
1,143
star
3

wh-app-ios

Objective-C
671
star
4

budgetdata

The data behind the President's 2016 Budget
541
star
5

wh-app-android

Java
406
star
6

fortyfour

7.x base theme for White House sites
CSS
336
star
7

source-code-policy

Federal Source Code Policy
CSS
249
star
8

fitara

Management and Oversight of Federal IT Resources
CSS
105
star
9

omb_place

Federal Place-based Initiatives Map
CSS
47
star
10

datacenters

Data Center Optimization Initiative (DCOI) Memo
JavaScript
20
star
11

CIOmanagement

Management and Oversight of Federal Information Technology
SCSS
17
star
12

ndoch-hackathon

16
star
13

buildmanager

Note: This project is in active development. Documentation may not be totally up to date. Stable release (and corresponding documentation) hopefully coming soon.
PHP
12
star
14

education-compliance-reports

Python
12
star
15

drushsubtree

Note: This project is in active development. Documentation may not be totally up to date. Stable release (and corresponding documentation) hopefully coming soon.
9
star
16

heartbleed

PHP
9
star
17

write-api-beta-testing

9
star
18

twitterapi

Drupal module with a simple interface for Twitter's REST API
9
star
19

services_documentation

PHP
8
star
20

cyber-acquisitions

CSS
7
star
21

tweetserver

Drupal install profile to grab tweets and push up to a CDN for redistribution.
PHP
6
star
22

webform_submit_button

The webform_submit_button module for Drupal
6
star
23

shunt

PHP
5
star
24

petitions_thermometer

Sheldon Rampton's project at White House hackathon on 2/22/2013
PHP
5
star
25

choropleth

JavaScript
4
star
26

software-policy

Category Management Policy 16-1: Improving the Acquisition and Management of Common Information Technology: Software Licensing
CSS
4
star
27

logger

Repository for the Drupal Logger module.
PHP
3
star
28

petitions-php-sdk

PHP
3
star
29

petition

This project has been renamed petitionS.
2
star
30

choropleth_dataset

JavaScript
2
star
31

ostp-ai-rfi-responses

Public comments submitted in response to 2016 OSTP RFI on Artificial Intelligence
2
star
32

tweetfetch

Stand-alone version of Tweet Fetch module originally developed for 2014 State of the Union Address as part of Tweet Server, https://github.com/whitehouse/tweetserver.
2
star
33

whrmake

A drush make file for compiling the code necessary to implement much of the functionality of the new responsive whitehouse.gov
1
star
34

ostp-data-portability-rfi-responses

Public comments submitted in response to 2016 OSTP RFI on Data Portability
1
star
35

mobile-policy

Category Management Policy 16-2: Improving the Acquisition and Management of Common Information Technology: Mobile Devices and Services
CSS
1
star
36

netstorage

Drupal module to help you sync files up to Akamai NetStorage.
PHP
1
star