casperlaitw / laravel-fb-messenger
A Laravel Package to Integrate Facebook Messenger API
Installs: 29 643
Dependents: 0
Suggesters: 0
Security: 0
Stars: 105
Watchers: 9
Forks: 27
Open Issues: 7
Requires
- php: ^7.2.5|^7.3|^8.0
- guzzlehttp/guzzle: ^6.0|^7.0
- illuminate/broadcasting: ^7.0|^8.0
- illuminate/console: ^7.0|^8.0
- illuminate/queue: ^7.0|^8.0
- illuminate/routing: ^7.0|^8.0
- illuminate/support: ^7.0|^8.0
- illuminate/view: ^7.0|^8.0
Requires (Dev)
- fzaninotto/faker: ^1.6
- mockery/mockery: ^0.9.5
- nesbot/carbon: ^1.21|^2.0
- phpunit/phpunit: ~5.0
- satooshi/php-coveralls: ^2.0
- v3.0.0
- 2.1.x-dev
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.0
- v1.x-dev
- v1.6.2
- v1.6.1
- v1.6.0
- v1.5.0
- dev-master / 1.4.x-dev
- v1.4.7
- v1.4.6
- v1.4.5
- v1.4.4
- v1.4.3
- v1.4.2
- v1.4.1
- v1.4.0
- 1.3.x-dev
- v1.3.1
- v1.3.0
- 1.2.x-dev
- v1.2.5
- v1.2.4
- v1.2.3
- v1.2.2
- v1.2.1
- v1.2.0
- 1.1.x-dev
- v1.1.8
- v1.1.7
- v1.1.6
- v1.1.5
- v1.1.4
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.0
This package is auto-updated.
Last update: 2024-11-27 09:13:10 UTC
README
This is a laravel package for Facebook Messenger Platform API.
Easy to making your facebook messenger chatbot.
Installation
Composer
composer require casperlaitw/laravel-fb-messenger
Laravel
Add Provider
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, ... ],
Publish Configuration
php artisan vendor:publish --provider="Casperlaitw\LaravelFbMessenger\LaravelFbMessengerServiceProvider" --tag="config"
Publish Menu Configure
Support define persistent menu in file.
Define persistent menu like laravel routes.
php artisan vendor:publish --provider="Casperlaitw\LaravelFbMessenger\LaravelFbMessengerServiceProvider" --tag="menu"
Configuration
Security
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"
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"
...
Custom Url
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' => [], ];
Custom Handler
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()}")); } }
Postback Handler
Create your postback handler
$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")); } }
Add to fb-messenger.php
config
return [ 'verify_token' => env('MESSENGER_VERIFY_TOKEN'), 'app_token' => env('MESSENGER_APP_TOKEN'), 'auto_typing' => true, 'handlers' => [App\YourHandler::class], 'postbacks' => [ App\StartupPostback::class, ], ];
Debug Route
The debug route using Pusher
Install pusher
Please follow Laravel Broadcasting document to install pusher package.
Configure your .env
APP_DEBUG=true
BROADCAST_DRIVER=pusher
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
Publish script
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
Open browser
http://[your-site]/fb-messenger/debug
API
Commands
See the document
License
This package is licensed under the MIT license.