赵乐 / facebook-messenger-bundle
用于Facebook Messenger的Symfony框架扩展包
v0.4.1
2019-03-21 17:05 UTC
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-09-22 05:11:10 UTC
README
用于Symfony框架的PHP Facebook Messenger API。
安装
步骤1: 下载扩展包
$ composer require pouler/facebook-messenger-bundle
步骤2: 启用扩展包(Symfony 2/3)
接下来,在kernel中启用扩展包
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new PouleR\FacebookMessengerBundle\FacebookMessengerBundle(), ); }
配置
使用以下配置编辑您的config.yml
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);
向用户发送文本消息(通过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);
使用批量请求向不同用户(通过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
创建通用模板消息
$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);
原始项目由John Kosmetos创建(https://github.com/jkosmetos/facebook-messenger-api)