This is a laravel package for Facebook Messenger Platform API.
Easy to making your facebook messenger chatbot.
composer require casperlaitw/laravel-fb-messenger
In your config/app.php
add Casperlaitw\LaravelFbMessenger\LaravelFbMessengerServiceProvider::class
to the providers array:
'providers' => [
...
Casperlaitw\LaravelFbMessenger\LaravelFbMessengerServiceProvider::class,
...
],
'alias => [
...
'Menu' => Casperlaitw\LaravelFbMessenger\Facades\MessengerMenu::class,
...
],
php artisan vendor:publish --provider="Casperlaitw\LaravelFbMessenger\LaravelFbMessengerServiceProvider" --tag="config"
Support define persistent menu in file.
Define persistent menu like laravel routes.
php artisan vendor:publish --provider="Casperlaitw\LaravelFbMessenger\LaravelFbMessengerServiceProvider" --tag="menu"
Almost every API request with access_token
, if you want to improved security in your app,
you can use appsecret_proof
. Please add MESSENGER_APP_SECRET
to .env
file and enable proof on all calls.
If you don't know how to get secret token and enabled proof, please checkout Graph Api
.env
MESSENGER_APP_SECRET="APP SECRET TOKEN"
Add you token to .env
file or modify fb-messenger.php
config.
If you don't know how to get token, please checkout Facebook Developer
.env
...
MESSENGER_VERIFY_TOKEN="By You Writing"
MESSENGER_APP_TOKEN="Page Access Token"
...
If you want to custom url, replace /webhook
to you want.
Finally, you can run php artisan route:list
to check.
return [
'verify_token' => env('MESSENGER_VERIFY_TOKEN'),
'app_token' => env('MESSENGER_APP_TOKEN'),
'auto_typing' => true,
'handlers' => [App\DefaultHandler::class],
'custom_url' => '/chatbot', // like this
'postbacks' => [],
];
The DefaultHandler
will reply the same words to user.
You can check out DefaultHandler
Handler MUST BE extends BaseHandler
.
<?php
namespace App;
use Casperlaitw\LaravelFbMessenger\Contracts\BaseHandler;
use Casperlaitw\LaravelFbMessenger\Messages\ReceiveMessage;
use Casperlaitw\LaravelFbMessenger\Messages\Text;
class DefaultHandler extends BaseHandler
{
/**
* handle method is required.
*/
public function handle(ReceiveMessage $message)
{
$this->send(new Text($message->getSender(), "Default Handler: {$message->getMessage()}"));
}
}
$payload
is you setting that fb:get-start command or button message's postback button etc.
$payload
support regex or string.
<?php
namespace App;
use Casperlaitw\LaravelFbMessenger\Contracts\PostbackHandler;
use Casperlaitw\LaravelFbMessenger\Messages\ReceiveMessage;
use Casperlaitw\LaravelFbMessenger\Messages\Text;
class StartupPostback extends PostbackHandler
{
// If webhook get the $payload is `USER_DEFINED_PAYLOAD` will run this postback handler
protected $payload = 'USER_DEFINED_PAYLOAD'; // You also can use regex!
/**
* Handle the chatbot message
*
* @param ReceiveMessage $message
*
* @return mixed
*/
public function handle(ReceiveMessage $message)
{
$this->send(new Text($message->getSender(), "I got your payload"));
}
}
return [
'verify_token' => env('MESSENGER_VERIFY_TOKEN'),
'app_token' => env('MESSENGER_APP_TOKEN'),
'auto_typing' => true,
'handlers' => [App\YourHandler::class],
'postbacks' => [
App\StartupPostback::class,
],
];
The debug route using Pusher
Please follow Laravel Broadcasting document to install pusher package.
APP_DEBUG=true
BROADCAST_DRIVER=pusher
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
If package updated, you need to run this script again. Keep your javascript is up-to-date
php artisan vendor:publish --provider="Casperlaitw\LaravelFbMessenger\LaravelFbMessengerServiceProvider" --tag="public" --force
http://[your-site]/fb-messenger/debug
See the document
This package is licensed under the MIT license.