Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

License

NotificationsYou must be signed in to change notification settings

lonalore/fb_messenger_bot

 
 

Repository files navigation

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

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 FacebookVerify Token (save the configuration).
  3. Complete steps 1-4 outlined inFacebook's quickstart documentationincluding 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 intothe 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 orreference.

  1. In a custom module, create a custom workflow by creating a class thatextends the FBMessengerBotWorkflow class.
  2. Modify the fb_messenger_bot.workflow service to use your custom workflow bycreating 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 videomessages fromFacebook'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 responsiblefor providing the workflow with a human readable name (see Roadmap), machinename, and message(s) to send the user. In addition to those basics, steps needto indicate what the next step to go to in the workflow based on a user'sresponse. Lastly, steps should provide a validation callback which will beinvoked to validate the user's response to the step, as well as an invalidresponse message, which will be sent to the user in the event that the callbackreturns 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 ona 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 (theasterisk), we want to proceed to the step in the workflow with machine name'builtABot'. By indicating 'NULL' for the handlerMessage, we are saying we wantto send whatever message text is configured in the builtABot step. If we want tooverride that behavior, we can simply set the handlerMessage value to be anarray of messages(s) of type MessageInterface.

If you sent out a message with buttons, and want to route a user to differentsteps depending on which button they clicked, you would achieve that usingresponse 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 thatrequires validation prior to saving it. The module ships with a few validationcallbacks you can use, but you're free to override them, and/orimplement 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 generalPHP context, outside of Drupal.
  • Convert objects to be Drupal entities where appropriate. Refactoring stepsto being config entities, for example, would allow for configuration of steps byan admin.
  • Support multiple workflows (e.g. you could have a intro workflow withcertain steps and then transition a user to a gather contact info workflow witha different set of steps etc.)
  • See you all fork and contribute to the module! (It would be great to seesomeone from the community polish the code and contribute it todrupal.org).

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP100.0%

[8]ページ先頭

©2009-2025 Movatter.jp