socheatsok78 / manychat-block-sdk
ManyChat Block SDK(非官方)
Requires (Dev)
- laravel/framework: ^6.2
- phpunit/phpunit: ^8.5
README
ManyChat Block SDK(⚠️ 非官方)
发送任何消息块,如文本、图库、列表等。附加带有自定义负载的按钮以继续交互。
在ManyChat中触发操作,如标记用户、设置自定义字段或通知管理员。
⚠️ 这是一个非官方SDK,使用风险自负!
示例代码
namespace App\Http\Controllers\Flow; use ManyChat\Dynamic\Chat; use Illuminate\Http\Request; use App\Http\Controllers\Controller; class WelcomeMessage extends Controller { /** * Handle the incoming request. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function __invoke(Request $request) { $chat = new Chat(); $text = new Text('Welcome to ManyChat Dynamic Response'); $chat->reply($text); return $chat; } }
示例响应
{ "version": "v2", "content": { "messages": [ { "type": "text", "text": "Welcome to ManyChat Dynamic Response" } ], "actions": [], "quick_replies": [] } }
安装
将 socheatsok78/manychat-block-sdk
包添加到您的项目中
composer require socheatsok78/manychat-block-sdk
或编辑 composer.json
{ "require": { "socheatsok78/manychat-block-sdk": "^1.0" }, }
在此处检查最新版本。有关发行说明,请参阅CHANGELOG.md。
内容
附加文档
🚨 限制
动态块中消息块的消息数量不得超过10条,快速回复11条,操作5条。
类型 | 限制 |
---|---|
消息 | 10 |
操作 | 5 |
快速回复 | 11 |
按钮 | 3 |
消息
创建类似于文本、列表或卡片的消息块。
注意:一条消息可以添加最多3个
Button
块,超过3个ManyChat
将不会显示您的响应
文本
创建用于发送文本消息的 Text
消息块。
可以使用 Url
、Flow
、Node
和 Call
按钮与 Text
块一起使用。
use ManyChat\Dynamic\Messages\Text; $text = new Text('Example text message'); # or $text = Text::create('Example text message');
列表
创建列表消息块,一组垂直排列的项目。有两种类型的列表 CompactList
和 LargeList
。
-
CompactList
以相同的方式渲染每个项目,适用于展示不突出显示任何项目的项目列表。 -
LargeList
以带有覆盖图像和文字叠加的第一项进行渲染
可以使用 Url
、Flow
、Node
、Call
和 Buy
按钮与 List
块一起使用。
注意:我们强烈建议您使用HTTPS协议来保护您的URL
警告:⚠️ 垂直列表将被Facebook弃用,并作为
Card
进行渲染
use ManyChat\Dynamic\Messages\Element; use ManyChat\Dynamic\Messages\LargeList; use ManyChat\Dynamic\Messages\CompactList; # You need to create at lease 2 Element block $element_1 = new Element([/* ... */]); $element_2 = new Element([/* ... */]); $compactList = new CompactList([$element_1, $element_2]); $largeList = new LargeList([$element_1, $element_2]);
卡片
创建一个水平滚动的画廊。卡片有两种类型:SquareCard
和 HorizontalCard
。
可以与 Card
块一起使用 Url
、Flow
、Node
、Call
和 Buy
按钮。
注意:我们强烈建议您使用HTTPS协议来保护您的URL
use ManyChat\Dynamic\Messages\Element; use ManyChat\Dynamic\Messages\SquareCard; use ManyChat\Dynamic\Messages\HorizontalCard; # You need to create at lease 2 Element block $element_1 = new Element([/* ... */]); $element_2 = new Element([/* ... */]); $horizontalCard = new HorizontalCard([$element_1, $element_2]); $squareCard = new SquareCard([$element_1, $element_2]);
元素
创建元素块。它只能在 List
或 Card
块上使用。
可以与 Element
块一起使用 Call
、Url
、Buy
、Node
和 Flow
按钮。
注意:我们强烈建议您使用HTTPS协议来保护您的URL
use ManyChat\Dynamic\Messages\Element; $element = new Element(); $element->title = 'Unsplash'; $element->subTitle = 'Photos for everyone'; $element->imageUrl = 'https://source.unsplash.com/random'; $element->actionUrl = 'https://unsplash.com'; # or $element = new Element([ 'title' => 'Unsplash', 'subTitle' => 'Photos for everyone', 'imageUrl' => 'https://source.unsplash.com/random', 'actionUrl' => 'https://unsplash.com', ]);
附件
创建类似文件、图片、音频和视频的附件块。
文件
创建文件块以发送不超过 25 MB 的其他文件。
注意:我们强烈建议您使用HTTPS协议来保护您的URL
use ManyChat\Dynamic\Attachments\File; $file = new File('/* URL to the file */'); # or $file = File::url('/* URL to the file */');
图片
创建图片块以发送图片。Image
支持 JPG、PNG 和 GIF 图片。
可以与 Element
块一起使用 Call
、Url
、Buy
、Node
和 Flow
按钮。
注意:我们强烈建议您使用HTTPS协议来保护您的URL
use ManyChat\Dynamic\Attachments\Image; $image = new Image('https://source.unsplash.com/random'); # or $image = Image::url('https://source.unsplash.com/random');
音频
创建音频块发送不超过 25 MB 的音频文件。
可以与 Element
块一起使用 Call
、Url
、Buy
、Node
和 Flow
按钮。
注意:我们强烈建议您使用HTTPS协议来保护您的URL
use ManyChat\Dynamic\Attachments\Audio; $audio = new Audio('/* URL to the audio file */'); # or $audio = Audio::url('/* URL to the audio file */');
视频
创建视频块发送不超过 25 MB 的视频文件。
可以与 Element
块一起使用 Call
、Url
、Buy
、Node
和 Flow
按钮。
注意:我们强烈建议您使用HTTPS协议来保护您的URL
use ManyChat\Dynamic\Attachments\Video; $video = new Video('/* URL to the video file */'); # or $video = Video::url('/* URL to the video file */');
按钮
创建类似 Call
、Url
、Buy
、Node
和 Flow
的按钮块。
可以为按钮提供自定义的 Action
。可以将 Action
附加到 Url
、Flow
和 Node
按钮类型。
电话
创建电话按钮块。
use ManyChat\Dynamic\Buttons\Call; $call = new Call('+123456789'); # or $call = Call::phone('+123456789');
URL
创建 URL 按钮块。
注意:我们强烈建议您使用HTTPS协议来保护您的URL
use ManyChat\Dynamic\Buttons\Url; $url = new Url('https://example.com'); # or $url = Url::create('https://example.com');
Url
有 3 种样式选项
use ManyChat\Dynamic\Buttons\Url; $url = new Url('https://example.com'); $url->full() # 100 %, Default ->medium() # 75 % ->compact(); # 50 %
购买
创建购买按钮块。
use ManyChat\Dynamic\Buttons\Buy; $buy = new Buy('T-Shirt', 2000); # or $buy = Buy::create('T-Shirt', 2000);
您可以为 Buy
按钮配置支付选项。请查看下面的代码。
use ManyChat\Dynamic\Buttons\Buy; use ManyChat\Dynamic\Foundation\Customer; use ManyChat\Dynamic\Foundation\Product; $buy = new Buy(); # Update Product name and price $buy->withProduct(function (Product $product) { $product->name = 'T-Shirt'; $product->price = 2000; }); # Update Customer requirement $buy->withCustomer(function (Customer $customer) { $customer->withContactName() # Require contact name ->withContactPhone() # Require contact phone ->withShippingAddress(); # Require shipping address # or $customer->withoutContactName() # Not require contact name ->withoutContactPhone() # Not require contact phone ->withoutShippingAddress(); # Not require shipping address });
节点
创建节点按钮块以链接到现有流程。
节点名称可以在其标题中找到,您需要使用唯一名称连接到节点。如果同一流程内存在多个具有相似名称的节点,则转换行为可能不会符合预期。
use ManyChat\Dynamic\Buttons\Node; $node = new Node('Welcome Message'); # or $node = Node::create('Welcome Message');
流程
创建流程按钮块。Flow
块类似于 Node
块,但 Flow
使用 Node
或 Flow
的 id
。
当您正在编辑节点/流程时,可以在地址栏中找到 id
。
use ManyChat\Dynamic\Buttons\Flow; $flow = new Flow('content20180221085508_278589'); # or $flow = Flow::create('content20180221085508_278589');
示例节点/流程 URL
# URL: https://manychat.com/fb152631685162536/cms/files/content20191211074127_716888
The `content20191211074127_716888` is the `id` of the node/flow.
操作
可以将操作附加到 Chat
、Message
、Attachment
或 Button
对象。
只有两种类型的操作:Tag
和 Custom Field
。
注意:
$object
可以是Chat
、Message
、Attachment
或Button
对象。
添加标签
向订阅者添加标签
$object->addTag('tag_name'); # or add multiple tags $object->addTags(['tag_1', 'tag_2']);
移除标签
从订阅者中移除标签
$object->removeTag('tag_name'); # or remove multiple tags $object->removeTags(['tag_1', 'tag_2']);
添加自定义字段
向订阅者添加自定义字段
$object->addField('Example_Field_1', 'value'); # or add multiple custom fields $object->addFields([ 'Example_Field_1' => 'value', 'Example_Field_2' => 'value' ]);
删除自定义字段
从订阅者中删除自定义字段
$object->removeField('Example_Field_1'); # or add multiple custom fields $object->removeFields(['Example_Field_1', 'Example_Field_2']);
快速回复
创建一个快速回复块。快速回复只能附加到 Chat
对象。
可以使用 Node
、Flow
按钮 和 DynamicBlock
回调与快速回复块一起使用。
use ManyChat\Dynamic\Chat; use ManyChat\Dynamic\Buttons\Node; use ManyChat\Dynamic\Buttons\Flow; $chat = new Chat(); $node = new Node('Welcome Message'); $flow = new Flow('content20180221085508_278589'); $dynamic = new DynamicBlock('https://example.com/api'); $chat->quickReply($node); $chat->quickReply($flow); $chat->quickReply($dynamic);
动态块
创建一个动态块回调。
注意:我们强烈建议您使用HTTPS协议来保护您的URL
use ManyChat\Dynamic\Callback\DynamicBlock; $dynamic = new DynamicBlock('https://example.com/api'); # Set Block caption $dynamic->setCaption('Custom Caption'); # Add HTTP request header $dynamic->setHeader('x-header', 'value'); $dynamic->setHeaders([ 'x-header-2' => 'value', 'x-header-2' => 'value' ]); # Add HTTP request payload $dynamic->setPayload('key', 'value'); $dynamic->setPayloads([ 'key-1' => 'value', 'key-2' => 'value' ]);
外部回调
您可以使用 ExternalCallback
块请求 ManyChat 在您的端处理下一位订阅者的消息。
注意:只能将一个回调附加到
Chat
对象。
use ManyChat\Dynamic\Chat; use ManyChat\Dynamic\Callback\ExternalCallback; $chat = new Chat(); $external = new ExternalCallback('https://example.com/api/flow/2'); # Add HTTP request header $external->setHeader('x-header', 'value'); $external->setHeaders([ 'x-header-2' => 'value', 'x-header-2' => 'value' ]); # Add HTTP request payload $external->setPayload('key', 'value'); $external->setPayloads([ 'key-1' => 'value', 'key-2' => 'value' ]); $chat->callback($external);
商标
标志、服务标志和商号是 ManyChat, Inc 的商标。
许可
此包是一个 非官方SDK,受 MIT 许可。