Download the PHP package elfsundae/bearychat without Composer
On this page you can find all versions of the php package elfsundae/bearychat. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package bearychat
BearyChat for PHP
A PHP package for sending message to BearyChat with the Incoming Webhook, and creating response payload for the Outgoing Robot.
- :cn: 中文文档
- Laravel integration: BearyChat for Laravel
- Laravel Notification Channel: BearyChatChannel
- Yii integration: BearyChat for Yii 2
Installation
You can install this package using the Composer manager.
Then you may create an Incoming Robot on your BearyChat team account, and read the payload format.
Documentation
Overview
To send messages, first create a BearyChat client with your webhook URL:
Besides the webhook, you may want to setup some default values for all messages which will be sent with this client:
All defaults keys are listed in MessageDefaults
. You can access message default with $client->getMessageDefaults($key)
, or retrieve all defaults with $client->getMessageDefaults()
.
To send a message, just call sendMessage
on the client instance with a message payload array or a payload JSON string:
In addition to the ugly payload, sendMessage
can handle any JsonSerializable
instances or any object which provides a payload via its toArray
or toJson
method. And there is a ready-made Message
class.
For convenience, any unhandled methods called to a Client
instance will be sent to a new Message
instance, and the most methods of a Message
instance return itself, so you can chain message modifications to achieve one-liner code.
You can also call the powerful send
or sendTo
method with message contents for sending a message.
Message Modifications
Available methods for message modification in the Message
class:
- text:
getText
,setText($text)
,text($text)
- notification:
getNotification
,setNotification($notification)
,notification($notification)
- markdown:
getMarkdown
,setMarkdown($markdown)
,markdown($markdown = true)
- channel:
getChannel
,setChannel($channel)
,channel($channel)
,to($channel)
- user:
getUser
,setUser($user)
,user($user)
,to('@'.$user)
- target (user or channel):
getTarget
,setTarget($target)
,target($target)
,removeTarget
,to($target)
- attachments:
getAttachments
,setAttachments($attachments)
,attachments($attachments)
,addAttachment(...)
,add(...)
,addImage
,removeAttachments(...)
,remove(...)
content($text, $markdown, $notification)
,content($text, $attachment_text, $attachment_title, $attachment_images, $attachment_color)
As you can see, the to($target)
method can change the message's target to an user if $target
is started with @
, otherwise it will set the channel that the message should be sent to. The channel's starter mark #
is optional in to
method, which means the result of to('#dev')
and to('dev')
is the same.
Method addAttachment($attachment)
accepts a PHP array of attachment payload, or a variable arguments list in order of text, title, images, color
, and the images
can be an image URL or an array contains image URLs. And this type of attachment parameters is also applicable to the method add
.
To remove attachments, call removeAttachments
or remove
with indices.
Message Representation
Call the toArray()
method on a Message instance will get the payload array for this message. You may use $message->toJson()
, json_encode($message)
or (string) $message
to get the JSON payload for $message
.
:warning: The message payload may be used for requesting an Incoming Webhook or creating response for an Outgoing Robot.
The above example will output:
Sending Message
You can call send
or sendTo
method on a Message instance to send that message.
The send
method optional accepts variable number of arguments to quickly change the payload content:
- Sending a basic message:
send($text, $markdown = true, $notification)
- Sending a message with one attachment added:
send($text, $attachment_text, $attachment_title, $attachment_images, $attachment_color)
The sendTo
method is useful when you want to change the message's target before calling send
method.
Customize Client
If you want to create a Message
instance explicitly, the client's createMessage
method will return a fresh Message
instance configured with the client's message defaults.
A Client
instance is mutable, it means you can change its webhook URL or the message defaults by calling setWebhook
, webhook
or setMessageDefaults
.
Testing
License
The BearyChat PHP package is available under the MIT license.