pouler / facebook-messenger-bundle
A Symfony bundle for Facebook Messenger
Installs: 10 559
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 1
Forks: 4
Open Issues: 0
Type:project
Requires
- php: >=7.0
- facebook/graph-sdk: ^5.6
- guzzlehttp/guzzle: ^6.3
- symfony/framework-bundle: ^3.0|^4.0
- symfony/property-access: ^3.0|^4.0
- symfony/serializer: ^3.0|^4.0
- symfony/yaml: ^3.0|^4.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^1.11
- php-mock/php-mock-phpunit: ^2.1
- phpunit/phpunit: ^6
- squizlabs/php_codesniffer: ^2.6
This package is auto-updated.
Last update: 2024-11-22 05:30:43 UTC
README
A PHP Facebook Messenger API for the Symfony framework.
Installation
Step 1: Download Bundle
$ composer require pouler/facebook-messenger-bundle
Step 2: Enable the bundle (Symfony 2/3)
Next, enable the bundle in the kernel:
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new PouleR\FacebookMessengerBundle\FacebookMessengerBundle(), ); }
Configuration
Edit your config.yml
with the following configuration:
pouler_facebook_messenger:
app_id: 'YourFBMessengerAppId'
app_secret: 'YourFBMessengerAppSecret'
Examples
Set your default greeting text
$service = new FacebookMessengerService('...', '...', new NullLogger()); $service->setAccessToken('PAGE_ACCESS_TOKEN'); $config = new GreetingTextConfiguration(); $config->setText('Hello and welcome!'); $service->setGreetingText($config);
Send a text message to a user (by PSID)
$service = new FacebookMessengerService('...', '...', new NullLogger()); $service->setAccessToken('PAGE_ACCESS_TOKEN'); $recipient = new Recipient('PSID'); $message = new Message('Hi there, this is a test'); $service->postMessage($recipient, $message);
Use batch requests to send a text message to different users (by PSID)
$service = new FacebookMessengerService('...', '...', new NullLogger()); $service->setAccessToken('PAGE_ACCESS_TOKEN'); $message = new Message('Hi there, this is a batch message'); $service->addMessageToBatch(new Recipient('PSID1'), $message); $service->addMessageToBatch(new Recipient('PSID2'), $message); $service->addMessageToBatch(new Recipient('PSID3'), $message); $response = $service->sendBatchRequests(); // The response variable contains an array with FailedMessageRequest objects
Create a generic template message
$message = new Message(); $templateAttachment = new TemplateAttachment(); $genericTemplatePayload = new GenericTemplatePayload(); $pbButton = new PostbackButton(); $pbButton->setTitle('Button Title Goes Here'); $pbButton->setPayload('payload_data'); $wuButton = new WebUrlButton(); $wuButton->setTitle('Button Title Goes Here'); $wuButton->setUrl('https://www.google.com'); $buttons = [$pbButton, $wuButton]; // Create some elements $genericElementOne = new GenericElement(); $genericElementOne->setTitle('Element Title Goes Here'); $genericElementOne->setImageUrl('https://placekitten.com/200/300'); $genericElementOne->setSubtitle('Subtitle Goes Here'); $genericElementOne->setButtons($buttons); $genericElementTwo = new GenericElement(); $genericElementTwo->setTitle('Element Title Goes Here'); $genericElementTwo->setImageUrl('https://placekitten.com/200/300'); $genericElementTwo->setSubtitle('Subtitle Goes Here'); $genericElementTwo->setButtons($buttons); // Add them to the payload $genericTemplatePayload->setElements([$genericElementOne, $genericElementTwo]); // Set the payload on the attachment $templateAttachment->setPayload($genericTemplatePayload); // Set the Attachment on the message $message->setAttachment($templateAttachment);
Original project by John Kosmetos (https://github.com/jkosmetos/facebook-messenger-api)