distilleries / messenger
Component to allow the comunication between messenger bot and your application.
Requires
- php: >=5.5.0
- guzzlehttp/guzzle: ^6.2
- illuminate/support: 5.2.*
Requires (Dev)
- mockery/mockery: 0.9.*
- orchestra/database: ~3.2
- orchestra/testbench: ~3.2
- phpunit/phpunit: ~4.5
This package is auto-updated.
Last update: 2024-10-23 07:26:33 UTC
README
Facebook Messenger
This repo contain some tools to work with facebook messenger bot and laravel/lumen. This repo work on beta of facebook messenger. Some big change are coming for sure but I guess that can help you.
Table of contents
- Installation
- Implement contract
- Change service provider
- Example of MessengerContract implementation
- Create application
- Host your application
- Facade
- Example
##Installation for Laravel
composer required distilleries/messenger
Add Service provider to bootstrap/app.php
:
$app->register(Distilleries\Messenger\MessengerLumenServiceProvider::class);
##Installation for Lumen
composer required distilleries/messenger
Add Service provider to config/app.php
:
'providers' => [ \Distilleries\Messenger\MessengerServiceProvider::class, ]
And Facade (also in config/app.php
) replace the laravel facade Mail
'aliases' => [ 'Messenger' => 'Distilleries\Messenger\Facades\Messenger' ]
Implement contract
To easily implement the fonctionality for your application I created a Distilleries\Messenger\Contracts\MessengerReceiverContract
.
Change service provider
To change the class use go to app/Providers/MessengerServiceProvider.php
and change the class inside the share function.
$this->app->singleton('Distilleries\Messenger\Contracts\MessengerReceiverContract', function ($app) { return new MyMessengerClass(); });
Example of MessengerContract implementation
class MyMessengerClass implements MessengerContract { public function receivedAuthentication($event) { $senderID = $event->sender->id; Messenger::sendTextMessage($senderID, "Authentication successful"); } public function receivedMessage($event) { $senderID = $event->sender->id; Messenger::sendTextMessage($senderID, 'Test'); Messenger::sendImageMessage($senderID, env('APP_URL') . '/assets/images/logo.png');, Messenger::sendCard($senderID, [ 'template_type' => 'generic', 'elements' => [ [ "title" => "Messenger Boilerplate", "image_url" => env('APP_URL') . '/assets/images/logo.png', "subtitle" => "example subtitle", 'buttons' => [ [ 'type' => "web_url", 'url' => "https://github.com/Distilleries/lumen-messenger-boilerplate", 'title' => "Come download it!" ] ] ] ] ]); } public function receivedDeliveryConfirmation($event) { $senderID = $event->sender->id; Messenger::sendTextMessage($senderID, 'Test'); } public function receivedPostback($event) { $senderID = $event->sender->id; Messenger::sendTextMessage($senderID, 'Test'); } }
Create application
Follow the messenger documentation to create the app https://developers.facebook.com/docs/messenger-platform/quickstart.
- For the webhook uri use
/webhook
- For the
VALIDATION_TOKEN
, generate a random key
After the application created and the page created and associated copy the .env.example
to .env
VALIDATION_TOKEN=
PAGE_ACCESS_TOKEN=
Host your application
You have to host your application to become use it. Facebook can't send you a web hook in local. So make sure you have an hosting ready before start you development.
Your bot is in sandobox by default. Only the people with the permission in your application can talk with it.
Facade
sendTextMessage
Messenger::sendTextMessage($senderID, "Authentication successful");
sendImageMessage
Messenger::sendImageMessage($senderID, env('APP_URL') . '/assets/images/logo.png');
getCurrentUserProfile
Messenger::getCurrentUserProfile($senderID);
sendCard
Messenger::sendCard($senderID, [ 'template_type' => 'generic', 'elements' => [ [ "title" => "Messenger Boilerplate", "image_url" => env('APP_URL') . '/assets/images/logo.png', "subtitle" => "example subtitle", 'buttons' => [ [ 'type' => "web_url", 'url' => "https://github.com/Distilleries/lumen-messenger-boilerplate", 'title' => "Come download it!" ] ] ] ] ]);
Example
On this messenger class you can say hi
and the bot give you an answer like this :
Hi First name Last name
Send a picture with a picto on the bottom right`
I customize your profile picture. Do you like it?