2017 © Pedro Peláez
 

library bearychat

An elegant way of interacting with BearyChat webhooks.

image

elfsundae/bearychat

An elegant way of interacting with BearyChat webhooks.

  • Wednesday, September 20, 2017
  • by ElfSundae
  • Repository
  • 6 Watchers
  • 64 Stars
  • 7,945 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 14 Forks
  • 1 Open issues
  • 20 Versions
  • 39 % Grown

The README.md

BearyChat for PHP

Latest Version on Packagist Software License Build Status StyleCI SensioLabsInsight Quality Score Code Coverage Total Downloads, (*1)

A PHP package for sending message to BearyChat with the Incoming Webhook, and creating response payload for the Outgoing Robot., (*2)

Installation

You can install this package using the Composer manager., (*3)

composer require elfsundae/bearychat

Then you may create an Incoming Robot on your BearyChat team account, and read the payload format., (*4)

Documentation

Overview

(new Client('https://hook.bearychat.com/=...'))
    ->text('content')
    ->add('attachment', 'title')
    ->addImage($imageUrl, 'image description')
    ->sendTo('admin');

(new Client($webhook))->send('content', 'attachment');

To send messages, first create a BearyChat client with your webhook URL:, (*5)

$client = new ElfSundae\BearyChat\Client('http://hook.bearychat.com/=.../incoming/...');

Besides the webhook, you may want to setup some default values for all messages which will be sent with this client:, (*6)

use ElfSundae\BearyChat\Client;

$client = new Client($webhook, [
    'channel' => 'server-log',
    'attachment_color' => '#3e4787'
]);

All defaults keys are listed in MessageDefaults . You can access message default with $client->getMessageDefaults($key), or retrieve all defaults with $client->getMessageDefaults() ., (*7)

To send a message, just call sendMessage on the client instance with a message payload array or a payload JSON string:, (*8)

$client->sendMessage([
    'text' => 'Hi, Elf!',
    'user' => 'elf'
]);

$json = '{"text": "Good job :+1:", "channel": "all"}';
$client->sendMessage($json);

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 available for creating payloads for Incoming messages or Outgoing responses. There are a variety of convenient methods that can work with the payload in Message class., (*9)

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., (*10)

You can also call the powerful send or sendTo method with message contents for sending a message., (*11)

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

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

Message Modifications

Available methods for message modification in the Message class:, (*12)

  • 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., (*13)

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., (*14)

$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();

To remove attachments, call removeAttachments or remove with indices., (*15)

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

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., (*16)

:warning: The message payload may be used for requesting an Incoming Webhook or creating response for an Outgoing Robot., (*17)

$message = $client->to('@elf')->text('foo')->markdown(false)
    ->add('bar', 'some images', 'path/to/image', 'blue');

echo $message->toJson(JSON_PRETTY_PRINT);

The above example will output:, (*18)

{
    "text": "foo",
    "markdown": false,
    "user": "elf",
    "attachments": [
        {
            "text": "bar",
            "title": "some images",
            "images": [
                {
                    "url": "path\/to\/image"
                }
            ],
            "color": "blue"
        }
    ]
}

Sending Message

You can call send or sendTo method on a Message instance to send that message., (*19)

The send method optional accepts variable number of arguments to quickly change the payload content:, (*20)

  • 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., (*21)

$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?');

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., (*22)

A Client instance is mutable, it means you can change its webhook URL or the message defaults by calling setWebhook, webhook or setMessageDefaults., (*23)

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

Testing

$ composer test

License

The BearyChat PHP package is available under the MIT license., (*24)

The Versions

20/09 2017

dev-master

9999999-dev https://github.com/ElfSundae/bearychat

An elegant way of interacting with BearyChat webhooks.

  Sources   Download

MIT

The Requires

 

The Development Requires

webhook robot incoming outgoing bearychat

04/09 2017

1.3.1

1.3.1.0 https://github.com/ElfSundae/bearychat

An elegant way of interacting with BearyChat webhooks.

  Sources   Download

MIT

The Requires

 

The Development Requires

webhook robot incoming outgoing bearychat

02/09 2017

1.3.0

1.3.0.0 https://github.com/ElfSundae/bearychat

An elegant way of interacting with BearyChat webhooks.

  Sources   Download

MIT

The Requires

 

The Development Requires

webhook robot incoming outgoing bearychat

28/08 2017

1.2.1

1.2.1.0 https://github.com/ElfSundae/bearychat

An elegant way of interacting with BearyChat webhooks.

  Sources   Download

MIT

The Requires

 

The Development Requires

webhook robot incoming outgoing bearychat

27/08 2017

1.2.0

1.2.0.0 https://github.com/ElfSundae/BearyChat

An elegant way of interacting with BearyChat webhooks.

  Sources   Download

MIT

The Requires

 

The Development Requires

webhook robot incoming outgoing bearychat

27/08 2016

1.1.13

1.1.13.0 https://github.com/ElfSundae/BearyChat

A PHP package for sending message to the BearyChat with the Incoming Webhook, and creating response payload for the Outgoing Robot.

  Sources   Download

MIT

The Requires

 

webhook robot incoming outgoing bearychat

27/08 2016

1.1.12

1.1.12.0 https://github.com/ElfSundae/BearyChat

A PHP package for sending message to the BearyChat with the Incoming Webhook, and creating response payload for the Outgoing Robot.

  Sources   Download

MIT

The Requires

 

webhook robot incoming outgoing bearychat

22/07 2016

1.1.11

1.1.11.0 https://github.com/ElfSundae/BearyChat

A PHP package for sending message to the BearyChat with the Incoming Webhook, and creating response payload for the Outgoing Robot.

  Sources   Download

MIT

The Requires

 

webhook robot incoming outgoing bearychat

19/07 2016

1.1.10

1.1.10.0 https://github.com/ElfSundae/BearyChat

A PHP package for sending message to the BearyChat with the Incoming Webhook, and creating response payload for the Outgoing Robot.

  Sources   Download

MIT

The Requires

 

webhook robot incoming outgoing bearychat

19/07 2016

1.1.9

1.1.9.0 https://github.com/ElfSundae/BearyChat

A PHP package for sending message to the BearyChat with the Incoming Webhook, and creating response payload for the Outgoing Robot.

  Sources   Download

MIT

The Requires

 

webhook robot incoming outgoing bearychat

17/07 2016

1.1.8

1.1.8.0 https://github.com/ElfSundae/BearyChat

A PHP package for sending message to the BearyChat with the Incoming Webhook, and creating response payload for the Outgoing Robot.

  Sources   Download

MIT

The Requires

 

webhook robot incoming outgoing bearychat

10/07 2016

1.1.7

1.1.7.0 https://github.com/ElfSundae/BearyChat

A PHP package for sending message to the BearyChat with the Incoming Webhook.

  Sources   Download

MIT

The Requires

 

robot bearychat incoming webhook

09/07 2016

1.1.6

1.1.6.0 https://github.com/ElfSundae/BearyChat

A PHP package for sending message to the BearyChat with the Incoming Webhook.

  Sources   Download

MIT

The Requires

 

robot bearychat incoming webhook

09/07 2016

1.1.5

1.1.5.0 https://github.com/ElfSundae/BearyChat

A PHP package for sending message to the BearyChat with the Incoming Webhook.

  Sources   Download

MIT

The Requires

 

robot bearychat incoming webhook

07/07 2016

1.1.4

1.1.4.0 https://github.com/ElfSundae/BearyChat

A PHP package for sending message to the BearyChat with the Incoming Webhook.

  Sources   Download

MIT

The Requires

 

robot bearychat incoming webhook

05/07 2016

1.1.3

1.1.3.0 https://github.com/ElfSundae/BearyChat

A PHP package for sending message to the BearyChat with the Incoming Webhook.

  Sources   Download

MIT

The Requires

 

robot bearychat incoming webhook

03/07 2016

1.1.2

1.1.2.0 https://github.com/ElfSundae/BearyChat

A PHP package for sending message to the BearyChat with the Incoming Webhook.

  Sources   Download

MIT

The Requires

 

robot bearychat incoming webhook

03/07 2016

1.1.1

1.1.1.0 https://github.com/ElfSundae/BearyChat

A PHP package for sending message to the BearyChat with the Incoming Webhook.

  Sources   Download

MIT

The Requires

 

robot bearychat incoming webhook

03/07 2016

1.1.0

1.1.0.0 https://github.com/ElfSundae/BearyChat

A PHP package for sending message to BearyChat with the Incoming Webhook.

  Sources   Download

MIT

The Requires

 

laravel bearychat incoming webhook

01/07 2016

1.0.0

1.0.0.0 https://github.com/ElfSundae/BearyChat

A PHP package for sending message to BearyChat with the Incoming Webhook.

  Sources   Download

MIT

The Requires

 

laravel bearychat incoming webhook