A PHP Facebook Messenger API for the Symfony framework.
$ composer require pouler/facebook-messenger-bundle
Next, enable the bundle in the kernel:
<?php
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new PouleR\FacebookMessengerBundle\FacebookMessengerBundle(),
);
}
Edit your config.yml
with the following configuration:
pouler_facebook_messenger:
app_id: 'YourFBMessengerAppId'
app_secret: 'YourFBMessengerAppSecret'
$service = new FacebookMessengerService('...', '...', new NullLogger());
$service->setAccessToken('PAGE_ACCESS_TOKEN');
$config = new GreetingTextConfiguration();
$config->setText('Hello and welcome!');
$service->setGreetingText($config);
$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);
$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
$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)