samiaraboglu / fb-messenger-bot-php
v2.0.0
2020-10-29 16:26 UTC
Requires
- php: >=5.5
This package is auto-updated.
Last update: 2024-08-29 04:31:19 UTC
README
安装
通过composer
$ composer require samiaraboglu/fb-messenger-bot-php
配置
$messenger = new \FbMessengerBot\Messenger('<YOUR_ACCESS_TOKEN>');
会话组件
使用消息类
$message = new \FbMessengerBot\Message();
文本消息
$message->text('<MESSAGE_TEXT>');
资产和附件
音频
$message->audio('<URL.mp3>');
视频
$message->video('<URL.mp4>');
图片
$message->image('<URL.jpg>');
文件
$message->file('<URL.pdf>');
消息模板
按钮模板
URL 按钮
// simple $message->url('<MESSAGE_TEXT>', '<BUTTON_TITLE>', '<BUTTON_URL>');
// multiple $message->button('<MESSAGE_TEXT>', [ [ 'title' => '<BUTTON_TITLE>', 'url' => '<BUTTON_URL>' ], [ 'title' => '<BUTTON_TITLE>', 'url' => '<BUTTON_URL>' ], [ ... ], [...] ]);
Postback 按钮
// simple $message->postback('<MESSAGE_TEXT>', '<BUTTON_TITLE>', '<POSTBACK_PAYLOAD>');
// multiple $message->button('<MESSAGE_TEXT>', [ [ 'type' => 'postback', 'title' => '<BUTTON_TITLE>', 'payload' => '<POSTBACK_PAYLOAD>' ], [ ... ], [...] ]);
分享按钮
// TO DO
购买按钮
// TO DO
电话按钮
// simple $message->call('<MESSAGE_TEXT>', '<BUTTON_TITLE>', '+<COUNTRY_CODE><PHONE_NUMBER>');
// multiple $message->button('<MESSAGE_TEXT>', [ [ 'type' => 'phone_number', 'title' => '<BUTTON_TEXT>', 'payload' => '<PHONE_NUMBER>' ], [ ... ], [...] ]);
登录按钮
$message->login('<MESSAGE_TEXT>', '<YOUR_LOGIN_URL>');
登出按钮
$message->logout('<MESSAGE_TEXT>');
游戏按钮
// TO DO
快速回复
文本
// simple $message->quickReplie('<MESSAGE_TEXT>', '<BUTTON_TITLE>', '<POSTBACK_PAYLOAD>');
// multiple $message->quickReplies('<MESSAGE_TEXT>', [ [ 'title' => '<BUTTON_TITLE>', 'payload' => '<POSTBACK_PAYLOAD>' ], [ ... ], [...] ]);
带图片的文本
// simple $message->quickReplie('<MESSAGE_TEXT>', '<BUTTON_TITLE>', '<POSTBACK_PAYLOAD>', '<URL.jpg>');
// multiple $message->quickReplies('<MESSAGE_TEXT>', [ [ 'title' => '<BUTTON_TITLE>', 'payload' => '<POSTBACK_PAYLOAD>', 'image' => '<URL.jpg>' ], [ ... ], [...] ]);
位置
// simple $message->location('<MESSAGE_TEXT>');
// multiple $message->quickReplies('<MESSAGE_TEXT>', [ [ 'type' => 'location' ], [ ... ], [...] ]);
电话号码
// simple $message->phoneNumber('<MESSAGE_TEXT>');
// multiple $message->quickReplies('<MESSAGE_TEXT>', [ [ 'type' => 'user_phone_number' ], [ ... ], [...] ]);
电子邮件
// simple $message->email('<MESSAGE_TEXT>');
// multiple $message->quickReplies('<MESSAGE_TEXT>', [ [ 'type' => 'user_email' ], [ ... ], [...] ]);
发送消息
需要页面作用域ID (PSID)
$response = $messenger->send(<PSID>, $message);
示例响应
// response print Array ( [recipient_id] => <PSID> [message_id] => mid.$cAAoZdzlbwyxoOR257liB9xxxxxx )
保存附件资产
发送第二个参数 true。支持的资产类型:image
audio
video
file
$message->image('<URL.jpg>', true);
示例响应
// response print Array ( [recipient_id] => <PSID> [message_id] => mid.$cAAcZdzlzwkxoQ6ss8ViEmxxxxxx [attachment_id] => 1200933831599999 )
使用 attachment_id 发送消息
$message->image(<ATTACHMENT_ID>);
获取正文
$body = $messenger->getBody(); // body print Array ( [recipient] => Array ( [id] => ... ) [message] => Array ( [attachment] => Array ( [type] => image [payload] => Array ( [url] => https://... ) ) ) [access_token] => ... )
发送者操作
操作类型:mark_seen
typing_on
typing_off
$response = $messenger->senderAction(<PSID>, '<ACTION_TYPE>');
Webhooks
这是为了要求 verify_token
配置
$messenger = new \FbMessengerBot\Messenger('<YOUR_ACCESS_TOKEN>', '<YOUR_VERIFY_TOKEN>');
监听用户消息。
$response = $messenger->listen();