Skip to content

ianfoose/php-messenger-bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 

Repository files navigation

PHP Messenger Bot

PHP simple Facebook Messenger Bot template

Setup

Use this file on a webserver where your messenger bot logic will be located.

Use

The first parameter when created a new bot instance is the verify token, the second is the page access token. A third and optional parameter is the Graph API URL, by default it is set to version 2.8.

$bot = new Bot('verify_token','access_token');

$bot->run(function($reqBody,$bot) {
	$bot->sendResponse($bot->buildSimpleResponse($reqBody['senderId'],'mesage text'));
});

When running the bot, the bot instance will return the request body from the request and an instance of itself. Use the returned instance to build and send responses

Request Parsing

The Request Body contains the following values:

  • senderId: The id of the sender
  • message: The message sent to the bot
  • payload: The recieved payload

Responses

Sending Responses

$bot->sendResponse($resp);

Simple Response

$resp = $bot->buildSimpleResponse($recipient_id,'Message Text');

Card

The cards must be in an array even if only one card is needed. To show a carousel response, add create multiple cards and add them to this array.

Note, a 'title' and 'subttile' are required.

When adding buttons, they must be in an array even if only one is needed.

$card = $bot->buildCard('title','subtitle','image_url',$default_action,$buttons);
$resp = $bot->buildRichResponse('recpient_id',[$card]);

Buttons

The default type for a button should be 'web_url', the 'payload' parameter is optional and is only used when the button has a postback value.

$btn = $bot->buildButton('title','type','url','payload');

List view support coming soon...

Refer to https://developers.facebook.com/docs/messenger-platform for more help on how messenger bots work.

About

PHP simple Facebook Messenger Bot template

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages