elfsundae/bearychat

此包已被废弃,不再维护。未建议替代包。

与BearyChat webhooks交互的优雅方式。

1.3.1 2017-09-04 05:54 UTC

This package is auto-updated.

Last update: 2022-03-15 13:27:31 UTC


README

Latest Version on Packagist Software License Build Status StyleCI SensioLabsInsight Quality Score Code Coverage Total Downloads

一个PHP包,用于通过BearyChat入站Webhook发送消息,并为出站机器人创建响应负载。

安装

您可以使用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实例或任何通过其toArraytoJson方法提供负载的对象。此外,有一个现成的Message类可用于为入站消息或出站响应创建负载。在Message类中,有各种方便的方法可以与负载一起使用。

为了方便起见,任何调用到Client实例的未处理方法都将发送到一个新的Message实例,而大多数Message实例的方法返回自身,因此您可以链式调用消息修改以实现一行代码。

您还可以使用消息内容调用强大的sendsendTo方法发送消息

$client->to('#all')->text('Hello')->add('World')->send();

$client->sendTo('all', 'Hello', 'World');

消息修改

Message类中修改消息的可用方法

  • 文本getTextsetText($text)text($text)
  • 通知getNotificationsetNotification($notification)notification($notification)
  • MarkdowngetMarkdownsetMarkdown($markdown)markdown($markdown = true)
  • 频道getChannelsetChannel($channel)channel($channel)to($channel)
  • 用户getUsersetUser($user)user($user)to('@'.$user)
  • 目标(用户或频道):getTargetsetTarget($target)target($target)removeTargetto($target)
  • 附件getAttachmentssetAttachments($attachments)attachments($attachments)addAttachment(...)add(...)addImageremoveAttachments(...)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();

要删除附件,请使用索引调用removeAttachmentsremove

$message->remove(0)->remove(0, 1)->remove([1, 3])->remove();

消息表示

在消息实例上调用toArray()方法将获取此消息的有效负载数组。您可以使用$message->toJson()json_encode($message)(string) $message获取$message的JSON有效负载。

⚠️ 消息有效负载可用于请求入站Webhook或为出站机器人创建响应。

$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"
        }
    ]
}

发送消息

您可以在消息实例上调用sendsendTo方法来发送该消息。

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实例是可变的,这意味着您可以通过调用setWebhookwebhooksetMessageDefaults来更改其webhook URL或消息默认值。

$client->webhook($webhook_ios)->setMessageDefaults([
    'channel' => 'ios_dev'
])->send('App reviewing status has updated.');

测试

$ composer test

许可证

BearyChat PHP包在MIT许可证下可用。