项目作者: hungneox

项目描述 :
Laravel/Lumen package for developing facebook messenger chat bot
高级语言: PHP
项目地址: git://github.com/hungneox/ramen-messenger.git
创建时间: 2017-12-14T14:23:29Z
项目社区:https://github.com/hungneox/ramen-messenger

开源协议:MIT License

下载


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:

  1. composer require composer require hungneox/ramen-messenger

Add these environment variables to your .env

  1. FACEBOOK_PAGE_ID=<YOUR_FACEBOOK_PAGE_ID>
  2. FACEBOOK_VERIFY_TOKEN=<TOKEN_FOR_VERIFY_YOUR_BOT>
  3. 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:

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

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

  1. $router->get('/webhook', [
  2. 'as' => 'webhook.index',
  3. 'uses' => 'WebHookController@index'
  4. ]);
  1. /**
  2. * For facebook verification
  3. *
  4. * @param Request $request
  5. */
  6. public function index(Request $request)
  7. {
  8. if ($request->get('hub_verify_token') === config('facebook.verify_token')) {
  9. return $request->get('hub_challenge');
  10. }
  11. return 'Wrong verification token!';
  12. }

Messenger Service

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

Working with templates

Text template

Quick replies

Quick replies

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

Button template

Button template

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

Open graph template

Open graph template

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

Creating Persistent Menu

Persistent Menu

  1. // Implements the getMenu() method from SetPersistentMenuCommand abstract class
  2. public function getMenu() {
  3. return (new PersistentMenu())
  4. ->addMenu(
  5. (new Menu())->addItem(
  6. (new Menu())
  7. ->setType('nested')
  8. ->setTitle('My Account')
  9. ->addItem(
  10. (new PostBackButton())
  11. ->setTitle('Pay Bill')
  12. ->setPayload('PAYBILL_PAYLOAD')
  13. )->addItem(
  14. (new PostBackButton())
  15. ->setTitle('History')
  16. ->setPayload('HISTORY_PAYLOAD')
  17. )->addItem(
  18. (new PostBackButton())
  19. ->setTitle('Contact Info')
  20. ->setPayload('CONTACT_INFO_PAYLOAD')
  21. )
  22. )->addItem(
  23. (new UrlButton())
  24. ->setTitle('Latest News')
  25. ->setUrl('https://yle.fi/uutiset/osasto/news/')
  26. )
  27. )->addMenu(
  28. (new Menu())->addItem(
  29. (new UrlButton())
  30. ->setTitle('Latest News FI')
  31. ->setUrl('https://yle.fi/uutiset')
  32. )->setLocale('fi_FI')
  33. );
  34. }

License

See LICENSE