hungneox/ramen-messenger

Lumen/Laravel package for developing facebook messenger chat bot

dev-master 2017-12-27 11:50 UTC

This package is not auto-updated.

Last update: 2024-10-27 05:36:15 UTC


README

Build Status Coverage Status StyleCI Latest Unstable Version Total Downloads License

A Laravel/Lumen package for developing facebook messenger chat bot

Usage

Installation

Run the following command to install the package through Composer:

composer require composer require hungneox/ramen-messenger

Add these environment variables to your .env

FACEBOOK_PAGE_ID=<YOUR_FACEBOOK_PAGE_ID>
FACEBOOK_VERIFY_TOKEN=<TOKEN_FOR_VERIFY_YOUR_BOT>
FACEBOOK_ACCESS_TOKEN=<YOUR_FACEBOOK_PAGE_ACCESS_TOKEN>

Copy the configuration template in config/facebook.php to your application's config directory and modify it to suit your needs.

Add the following line to bootstrap/app.php:

$app->register(\Neox\Ramen\Messenger\MessengerServiceProvider::class);

To configure the webhook for your app, adding a route for facebook verification

$router->get('/webhook', [
    'as'   => 'webhook.index',
    'uses' => 'WebHookController@index'
]);
/**
 * For facebook verification
 *
 * @param Request $request
 */
public function index(Request $request)
{
    if ($request->get('hub_verify_token') === config('facebook.verify_token')) {
        return $request->get('hub_challenge');
    }

    return 'Wrong verification token!';
}

Messenger Service

/** @var RamenBot $bot */
$bot = app(RamenBot::class);
$bot->hears('hello', function(RamenBot $bot) {
    $bot->replies('greeting from the bot!');
});

Working with templates

Text template

Quick replies

Quick replies

$template = (new TextTemplate($sender, 'Please share your location'))
            ->addQuickReply(
                (new QuickReply())->setContentType('location')
            );

Button template

Button template

$template = (new ButtonTemplate())
                ->setRecipientId($sender)
                ->setText('What do you want to do next?')
                ->addButton(
                    (new UrlButton())
                        ->setUrl('https://www.messenger.com')
                        ->setTitle('Get Order Status')
                )->addButton(
                    (new UrlButton())
                        ->setUrl('https://www.messenger.com')
                        ->setTitle('Call Me')
                );
// Reply the button template when the bot hears `help`
/** @var RamenBot $bot */
$bot = app(RamenBot::class);
$bot->hears('help', function(RamenBot $bot) use ($template) {
    $bot->sends($template);
});

Open graph template

Open graph template

$tempalte = (new OpenGraphTemplate())
            ->addElement(
                (new OpenGraphElement())
                    ->setUrl('https://open.spotify.com/track/7GhIk7Il098yCjg4BQjzvb')
                    ->addButton(
                        (new UrlButton())
                            ->setUrl('https://en.wikipedia.org/wiki/Rickrolling')
                            ->setTitle('View More')
                    )
            )->setRecipientId($sender);

Creating Persistent Menu

Persistent Menu

// Implements the getMenu() method from SetPersistentMenuCommand abstract class
public function getMenu() {
    return (new PersistentMenu())
               ->addMenu(
                   (new Menu())->addItem(
                       (new Menu())
                           ->setType('nested')
                           ->setTitle('My Account')
                           ->addItem(
                               (new PostBackButton())
                                    ->setTitle('Pay Bill')
                                    ->setPayload('PAYBILL_PAYLOAD')
                           )->addItem(
                               (new PostBackButton())
                                    ->setTitle('History')
                                    ->setPayload('HISTORY_PAYLOAD')
                           )->addItem(
                               (new PostBackButton())
                                    ->setTitle('Contact Info')
                                    ->setPayload('CONTACT_INFO_PAYLOAD')
                           )
                   )->addItem(
                       (new UrlButton())
                            ->setTitle('Latest News')
                            ->setUrl('https://yle.fi/uutiset/osasto/news/')
                   )
               )->addMenu(
                   (new Menu())->addItem(
                       (new UrlButton())
                            ->setTitle('Latest News FI')
                            ->setUrl('https://yle.fi/uutiset')
                   )->setLocale('fi_FI')
               );
}

License

See LICENSE