casperlaitw/laravel-fb-messenger

A Laravel Package to Integrate Facebook Messenger API

v3.0.0 2021-10-27 02:42 UTC

README

Build Status Coverage Status StyleCI Latest Stable Version Latest Unstable Version Total Downloads Monthly Downloads

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.

document

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,
    ],
];

Example

Debug Route

The debug route using Pusher

debug-route

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

API Document

Commands

See the document

License

This package is licensed under the MIT license.