elfsundae / bearychat
与BearyChat webhooks交互的优雅方式。
Requires
- php: >=5.4.0
- ext-mbstring: *
- guzzlehttp/guzzle: ~5.3|~6.0
Requires (Dev)
- mockery/mockery: 0.9.*
- phpunit/phpunit: ~5.7
Suggests
- elfsundae/laravel-bearychat: Required for Laravel integration.
- laravel-notification-channels/bearychat: BearyChat notifications channel for Laravel 5.
README
一个PHP包,用于通过BearyChat的入站Webhook发送消息,并为出站机器人创建响应负载。
- 🇨🇳 中文文档
- Laravel集成: BearyChat for Laravel
- Laravel通知通道: BearyChatChannel
- Yii集成: BearyChat for Yii 2
安装
您可以使用Composer管理器安装此包。
composer require elfsundae/bearychat
然后在您的BearyChat团队账户中创建一个入站机器人,并阅读负载格式。
文档
概述
(new Client('https://hook.bearychat.com/=...')) ->text('content') ->add('attachment', 'title') ->addImage($imageUrl, 'image description') ->sendTo('admin'); (new Client($webhook))->send('content', 'attachment');
要发送消息,首先使用您的webhook URL创建一个BearyChat客户端
$client = new ElfSundae\BearyChat\Client('http://hook.bearychat.com/=.../incoming/...');
除了webhook,您可能还想为所有通过此客户端发送的消息设置一些默认值
use ElfSundae\BearyChat\Client; $client = new Client($webhook, [ 'channel' => 'server-log', 'attachment_color' => '#3e4787' ]);
所有默认键都列在MessageDefaults
中。您可以使用$client->getMessageDefaults($key)
访问消息默认值,或使用$client->getMessageDefaults()
检索所有默认值。
要发送消息,只需在客户端实例上调用sendMessage
方法,并传递一个消息负载数组或负载JSON字符串
$client->sendMessage([ 'text' => 'Hi, Elf!', 'user' => 'elf' ]); $json = '{"text": "Good job :+1:", "channel": "all"}'; $client->sendMessage($json);
除了丑陋的负载之外,sendMessage
还可以处理任何JsonSerializable
实例或任何通过其toArray
或toJson
方法提供负载的对象。此外,有一个现成的Message
类可用于为入站消息或出站响应创建负载。在Message
类中,有各种方便的方法可以与负载一起使用。
为了方便起见,任何调用到Client
实例的未处理方法都将发送到一个新的Message
实例,而大多数Message
实例的方法返回自身,因此您可以链式调用消息修改以实现一行代码。
您还可以使用消息内容调用强大的send
或sendTo
方法发送消息。
$client->to('#all')->text('Hello')->add('World')->send(); $client->sendTo('all', 'Hello', 'World');
消息修改
在Message
类中修改消息的可用方法
- 文本:
getText
,setText($text)
,text($text)
- 通知:
getNotification
,setNotification($notification)
,notification($notification)
- Markdown:
getMarkdown
,setMarkdown($markdown)
,markdown($markdown = true)
- 频道:
getChannel
,setChannel($channel)
,channel($channel)
,to($channel)
- 用户:
getUser
,setUser($user)
,user($user)
,to('@'.$user)
- 目标(用户或频道):
getTarget
,setTarget($target)
,target($target)
,removeTarget
,to($target)
- 附件:
getAttachments
,setAttachments($attachments)
,attachments($attachments)
,addAttachment(...)
,add(...)
,addImage
,removeAttachments(...)
,remove(...)
content($text, $markdown, $notification)
,content($text, $attachment_text, $attachment_title, $attachment_images, $attachment_color)
如你所见,当$target
以@
开头时,to($target)
方法可以将消息的目标更改为用户,否则它将设置消息应发送到的频道。在to
方法中,频道的起始标记#
是可选的,这意味着to('#dev')
和to('dev')
的结果是相同的。
方法addAttachment($attachment)
接受一个PHP附件有效负载数组,或按照text, title, images, color
顺序的变量参数列表,其中images
可以是图片URL或包含图片URL的数组。此类附件参数也适用于方法add
。
$client ->to('@elf') ->text('message') ->add([ 'text' => 'Content of the first attachment.', 'title' => 'First Attachment', 'images' => [$imageUrl, $imageUrl2], 'color' => '#10e4fe' ]) ->add( 'Content of the second attachment.', 'Second Attachment', [$imageUrl, $imageUrl2], 'red' ) ->send();
要删除附件,请使用索引调用removeAttachments
或remove
。
$message->remove(0)->remove(0, 1)->remove([1, 3])->remove();
消息表示
在消息实例上调用toArray()
方法将获取此消息的有效负载数组。您可以使用$message->toJson()
,json_encode($message)
或(string) $message
获取$message
的JSON有效负载。
$message = $client->to('@elf')->text('foo')->markdown(false) ->add('bar', 'some images', 'path/to/image', 'blue'); echo $message->toJson(JSON_PRETTY_PRINT);
上述示例将输出
{ "text": "foo", "markdown": false, "user": "elf", "attachments": [ { "text": "bar", "title": "some images", "images": [ { "url": "path\/to\/image" } ], "color": "blue" } ] }
发送消息
您可以在消息实例上调用send
或sendTo
方法来发送该消息。
send
方法可选接受可变数量的参数,以快速更改有效负载内容
- 发送基本消息:
send($text, $markdown = true, $notification)
- 发送添加了一个附件的消息:
send($text, $attachment_text, $attachment_title, $attachment_images, $attachment_color)
当您想在调用send
方法之前更改消息的目标时,sendTo
方法很有用。
$client = new Client($webhook, [ 'channel' => 'all' ]); // Sending a message to the default channel $client->send('Hi there :smile:'); // Sending a customized message $client->send('disable **markdown**', false, 'custom notification'); // Sending a message with one attachment added $client->send('message title', 'Attachment Content'); // Sending a message with an customized attachment $client->send( 'message with an customized attachment', 'Attachment Content', 'Attachment Title', $imageUrl, '#f00' ); // Sending a message with multiple images $client->send('multiple images', null, null, [$imageUrl1, $imageUrl2]); // Sending a message to a different channel $client->sendTo('iOS', '**Lunch Time !!!**'); // Sending a message to an user $client->sendTo('@elf', 'Where are you?');
自定义客户端
如果您想显式创建一个Message
实例,客户端的createMessage
方法将返回一个配置了客户端消息默认值的全新Message
实例。
Client
实例是可变的,这意味着您可以通过调用setWebhook
,webhook
或setMessageDefaults
来更改其webhook URL或消息默认值。
$client->webhook($webhook_ios)->setMessageDefaults([ 'channel' => 'ios_dev' ])->send('App reviewing status has updated.');
测试
$ composer test
许可证
BearyChat PHP包在MIT许可证下可用。