hungneox/ramen-messenger

Lumen/Laravel 包,用于开发 Facebook Messenger 聊天机器人

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

This package is not auto-updated.

Last update: 2024-09-15 04:36:49 UTC


README

Build Status Coverage Status StyleCI Latest Unstable Version Total Downloads License

一个用于开发 Facebook Messenger 聊天机器人的 Laravel/Lumen 包

使用方法

安装

使用以下命令通过 Composer 安装此包:

composer require composer require hungneox/ramen-messenger

将以下环境变量添加到您的 .env 文件中:

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

将 config/facebook.php 中的配置模板复制到您的应用配置目录中,并根据需要进行修改。

将以下行添加到 bootstrap/app.php 中:

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

为了 配置您的应用 webhook,添加一个用于 Facebook 验证的路由

$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!';
}

消息服务

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

使用模板

文本模板

快速回复

Quick replies

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

按钮模板

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

$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);

创建持久菜单

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