Coder Social home page Coder Social logo

hungneox / ramen-messenger Goto Github PK

View Code? Open in Web Editor NEW
2.0 3.0 1.0 101 KB

Laravel/Lumen package for developing facebook messenger chat bot

License: MIT License

PHP 100.00%
lumen facebook-messenger php php-library laravel laravel-5-package lumen-messenger-sdk lumen-package

ramen-messenger's Introduction

Ramen Messenger

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

ramen-messenger's People

Contributors

hungneox avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

xuanhoa88

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.