lygav/php-slackbot

简单易用,用于向Slack发送消息的PHP包。利用这个友好的API,快速发送美观、多彩的消息并附带丰富的附件。

0.0.4 2016-12-01 13:50 UTC

This package is auto-updated.

Last update: 2024-09-15 01:52:02 UTC


README

Build Status Scrutinizer Packagist Packagist Pre Release

简单易用,用于向Slack发送消息的PHP包。
利用这个友好的API,快速发送美观、多彩的消息并附带丰富的附件。

兼容PHP >= 5.3

安装

通过composer

"require": {
   "lygav/php-slackbot": "0.0.*"
}

不使用composer,通过PHAR

从命令行进入克隆的仓库目录,运行

php makephar

您会看到创建了一个名为"phpslackbot.phar"的新文件。然后在您的应用中

include 'path/to/phpslackbot.phar';

其余部分与使用'composer'安装相同

您的第一条消息

$bot = new Slackbot("https://hooks.slack.com/services/your/incoming/hook");
$bot->text("Hi")->send();

直接消息

$bot->text("Hi all!")
    ->from("username")
    ->toChannel("mychannel")
    ->send();

轻松创建美观、多彩的附件

$bot->attach(
    $bot->buildAttachment("fallback text")
    ->enableMarkdown()
    ->setText("We can have *mrkdwn* `code` _italic_ also in attachments")
    )
    ->toGroup("mygroup")
    ->send();

自由定制

$attachment = $bot->buildAttachment("fallback text"/* mandatory by slack */)
    ->setPretext("pretext line")
    ->setText("attachment body text")
    /*
      Human web-safe colors automatically
      translated into HEX equivalent
    */
    ->setColor("lightblue")
    ->setAuthor("me")
    ->addField("short field", "i'm inline", TRUE)
    ->addField("short field 2", "i'm also inline", TRUE)
    ->setImageUrl("http://my-website.com/path/to/image.jpg");

$bot->attach($attachment)->send();

设置/覆盖所有可能的设置

$options = array(
            'username' => 'my-bot-name',
            'icon_emoji' => ':icon name:',
            'icon_url' => 'http://someicon.com',
            'channel' => '#test-channel'
        );
        
$bot = new Slackbot($url, $options);
$bot->text("check out bot new icon")->send();

// Choose to override 'last minute' (ex. when dealing with multiple consequtive messages)
$bot->text("check out bot new icon")->send(array("username" => "other-bot-name"));

高级用法

使用自定义传输处理器

$handler = new MockHandler();
$bot     = new SlackBot($url, ['handler' => $handler]);
$bot->text("some text")
    ->from("my-test-bot")
    ->toGroup("bot-testing")
    ->send();