PHP code example of samiaraboglu / fb-messenger-bot-php

1. Go to this page and download the library: Download samiaraboglu/fb-messenger-bot-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

samiaraboglu / fb-messenger-bot-php example snippets


$messenger = new \FbMessengerBot\Messenger('<YOUR_ACCESS_TOKEN>');

$message = new \FbMessengerBot\Message();

$message->text('<MESSAGE_TEXT>');

$message->audio('<URL.mp3>');

$message->video('<URL.mp4>');

$message->image('<URL.jpg>');

$message->file('<URL.pdf>');

// 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>'
    ],
    [
        ...
    ],
    [...]
]);

// 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

// simple
$message->quickReplie('<MESSAGE_TEXT>', '<BUTTON_TITLE>', '<POSTBACK_PAYLOAD>');

// multiple
$message->quickReplies('<MESSAGE_TEXT>', [
    [
        'title' => '<BUTTON_TITLE>',
        'payload' => '<POSTBACK_PAYLOAD>'
    ],
    [
        ...
    ],
    [...]
]);

// 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>'
    ],
    [
        ...
    ],
    [...]
]);

// simple
$message->location('<MESSAGE_TEXT>');

// multiple
$message->quickReplies('<MESSAGE_TEXT>', [
    [
        'type' => 'location'
    ],
    [
        ...
    ],
    [...]
]);

// simple
$message->phoneNumber('<MESSAGE_TEXT>');

// multiple
$message->quickReplies('<MESSAGE_TEXT>', [
    [
        'type' => 'user_phone_number'
    ],
    [
        ...
    ],
    [...]
]);

// simple
$message->email('<MESSAGE_TEXT>');

// multiple
$message->quickReplies('<MESSAGE_TEXT>', [
    [
        'type' => 'user_email'
    ],
    [
        ...
    ],
    [...]
]);

$response = $messenger->send(<PSID>, $message);

// response print
Array
(
    [recipient_id] => <PSID>
    [message_id] => mid.$cAAoZdzlbwyxoOR257liB9xxxxxx
)

$message->image('<URL.jpg>', true);

// response print
Array
(
    [recipient_id] => <PSID>
    [message_id] => mid.$cAAcZdzlzwkxoQ6ss8ViEmxxxxxx
    [attachment_id] => 1200933831599999
)

$message->image(<ATTACHMENT_ID>);

$body = $messenger->getBody();

// body print
Array
(
    [recipient] => Array
        (
            [id] => ...
        )

    [message] => Array
        (
            [attachment] => Array
                (
                    [type] => image
                    [payload] => Array
                        (
                            [url] => https://...
                        )

                )

        )

    [access_token] => ...
)

$response = $messenger->senderAction(<PSID>, '<ACTION_TYPE>');

$messenger = new \FbMessengerBot\Messenger('<YOUR_ACCESS_TOKEN>', '<YOUR_VERIFY_TOKEN>');

$response = $messenger->listen();

$ composer