fb-messenger-bot-php
, (*1)
Installing
Via composer, (*2)
$ composer require samiaraboglu/fb-messenger-bot-php
Config, (*3)
$messenger = new \FbMessengerBot\Messenger('<YOUR_ACCESS_TOKEN>');
Conversation Components
Use the message class, (*4)
$message = new \FbMessengerBot\Message();
Text Message
$message->text('<MESSAGE_TEXT>');
Assets & Attachments
Audio
$message->audio('<URL.mp3>');
Video
$message->video('<URL.mp4>');
Image
$message->image('<URL.jpg>');
File
$message->file('<URL.pdf>');
Message Templates
// simple
$message->url('<MESSAGE_TEXT>', '<BUTTON_TITLE>', '<BUTTON_URL>');
// multiple
$message->button('<MESSAGE_TEXT>', [
[
'title' => '<BUTTON_TITLE>',
'url' => '<BUTTON_URL>'
],
[
'title' => '<BUTTON_TITLE>',
'url' => '<BUTTON_URL>'
],
[
...
],
[...]
]);
Postback Button
// simple
$message->postback('<MESSAGE_TEXT>', '<BUTTON_TITLE>', '<POSTBACK_PAYLOAD>');
// multiple
$message->button('<MESSAGE_TEXT>', [
[
'type' => 'postback',
'title' => '<BUTTON_TITLE>',
'payload' => '<POSTBACK_PAYLOAD>'
],
[
...
],
[...]
]);
// TO DO
// TO DO
// simple
$message->call('<MESSAGE_TEXT>', '<BUTTON_TITLE>', '+<COUNTRY_CODE><PHONE_NUMBER>');
// multiple
$message->button('<MESSAGE_TEXT>', [
[
'type' => 'phone_number',
'title' => '<BUTTON_TEXT>',
'payload' => '<PHONE_NUMBER>'
],
[
...
],
[...]
]);
$message->login('<MESSAGE_TEXT>', '<YOUR_LOGIN_URL>');
$message->logout('<MESSAGE_TEXT>');
// TO DO
Quick Replies
Text
// simple
$message->quickReplie('<MESSAGE_TEXT>', '<BUTTON_TITLE>', '<POSTBACK_PAYLOAD>');
// multiple
$message->quickReplies('<MESSAGE_TEXT>', [
[
'title' => '<BUTTON_TITLE>',
'payload' => '<POSTBACK_PAYLOAD>'
],
[
...
],
[...]
]);
Text With Image
// simple
$message->quickReplie('<MESSAGE_TEXT>', '<BUTTON_TITLE>', '<POSTBACK_PAYLOAD>', '<URL.jpg>');
// multiple
$message->quickReplies('<MESSAGE_TEXT>', [
[
'title' => '<BUTTON_TITLE>',
'payload' => '<POSTBACK_PAYLOAD>',
'image' => '<URL.jpg>'
],
[
...
],
[...]
]);
Location
// simple
$message->location('<MESSAGE_TEXT>');
// multiple
$message->quickReplies('<MESSAGE_TEXT>', [
[
'type' => 'location'
],
[
...
],
[...]
]);
Phone Number
// simple
$message->phoneNumber('<MESSAGE_TEXT>');
// multiple
$message->quickReplies('<MESSAGE_TEXT>', [
[
'type' => 'user_phone_number'
],
[
...
],
[...]
]);
Email
// simple
$message->email('<MESSAGE_TEXT>');
// multiple
$message->quickReplies('<MESSAGE_TEXT>', [
[
'type' => 'user_email'
],
[
...
],
[...]
]);
Send Message
Require page-scoped ID (PSID), (*5)
$response = $messenger->send(<PSID>, $message);
Example response, (*6)
// response print
Array
(
[recipient_id] => <PSID>
[message_id] => mid.$cAAoZdzlbwyxoOR257liB9xxxxxx
)
Saving Assets for Attachments
Send second parameter true. Supported asset types: image
audio
video
file
, (*7)
$message->image('<URL.jpg>', true);
Example response, (*8)
// response print
Array
(
[recipient_id] => <PSID>
[message_id] => mid.$cAAcZdzlzwkxoQ6ss8ViEmxxxxxx
[attachment_id] => 1200933831599999
)
Send message with attachment_id, (*9)
$message->image(<ATTACHMENT_ID>);
Get Body
$body = $messenger->getBody();
// body print
Array
(
[recipient] => Array
(
[id] => ...
)
[message] => Array
(
[attachment] => Array
(
[type] => image
[payload] => Array
(
[url] => https://...
)
)
)
[access_token] => ...
)
Sender Actions
Action types: mark_seen
typing_on
typing_off
, (*10)
$response = $messenger->senderAction(<PSID>, '<ACTION_TYPE>');
Webhooks
This for require verify_token, (*11)
Config, (*12)
$messenger = new \FbMessengerBot\Messenger('<YOUR_ACCESS_TOKEN>', '<YOUR_VERIFY_TOKEN>');
Listen to user messages., (*13)
$response = $messenger->listen();