alex-equity / php-slack-bot
用 PHP 编写的 Slack 机器人
Requires
- devristo/phpws: dev-master
- react/http: ^0.4
This package is not auto-updated.
Last update: 2024-09-29 02:03:25 UTC
README
一个简单的用 PHP 编写的 Slack 实时消息 API 机器人 https://api.slack.com/rtm
安装
使用 Composer
创建一个新的 composer.json 文件并添加以下内容
{
"minimum-stability" : "dev",
"require": {
"jclg/php-slack-bot": "dev-master"
}
}
然后运行
composer install
用法
创建一个名为 bot.php
的 PHP 文件,内容如下
require 'vendor/autoload.php'; use PhpSlackBot\Bot; // Custom command class MyCommand extends \PhpSlackBot\Command\BaseCommand { protected function configure() { $this->setName('mycommand'); } protected function execute($message, $context) { $this->send($this->getCurrentChannel(), null, 'Hello !'); } } $bot = new Bot(); $bot->setToken('TOKEN'); // Get your token here https://my.slack.com/services/new/bot $bot->loadCommand(new MyCommand()); $bot->loadInternalCommands(); // This loads example commands $bot->run();
然后在命令行(终端)中运行 php bot.php
示例命令
示例命令位于 src/PhpSlackBot/Command/
目录中,可以通过 $bot->loadInternalCommands();
加载
Ping Pong 命令
在频道中输入 ping
,机器人应该会回复 "Pong"。
计数命令
在频道中多次输入 count
,机器人应该会依次回复 1、2...
日期命令
在频道中输入 date
,机器人会显示当前日期。
计划扑克命令
https://en.wikipedia.org/wiki/Planning_poker
在公共频道中输入 pokerp start
,以便与您的团队开始计划扑克会议。
通过 pokerp vote number
向机器人发送私信进行投票。机器人会记录您的投票。
输入 pokerp status
以查看当前会议的状态(谁已经投票)。
在公共频道中输入 pokerp end
,机器人将输出每个投票。
加载自定义命令
您可以通过实现 \PhpSlackBot\Command\BaseCommand 来加载自己的命令。
然后为要加载的每个命令调用 PhpSlackBot\Bot::loadCommand 方法。
"捕获所有" 命令
如果您需要在事件发生时执行命令,可以设置一个 "捕获所有" 命令。
这个特殊命令将在所有事件上触发。
require 'vendor/autoload.php'; use PhpSlackBot\Bot; // This special command executes on all events class SuperCommand extends \PhpSlackBot\Command\BaseCommand { protected function configure() { // We don't have to configure a command name in this case } protected function execute($data, $context) { if ($data['type'] == 'message') { $channel = $this->getChannelNameFromChannelId($data['channel']); $username = $this->getUserNameFromUserId($data['user']); echo $username.' from '.($channel ? $channel : 'DIRECT MESSAGE').' : '.$data['text'].PHP_EOL; } } } $bot = new Bot(); $bot->setToken('TOKEN'); // Get your token here https://my.slack.com/services/new/bot $bot->loadCatchAllCommand(new SuperCommand()); $bot->run();
入站 webhook
机器人还可以监听入站 webhook。
命令是由 Slack 中的用户消息触发的,而 webhook 是由 Web 发起请求触发的。
可以使用 PhpSlackBot\Bot::loadWebhook 方法加载自定义 webhook。
如果您需要从外部服务控制机器人,这很有用。例如,使用 IFTTT https://ifttt.com/maker
要启用 webhook,请在 run 方法之前使用 enableWebserver 方法。
您还可以设置一个密钥令牌以防止未经授权的请求。
$bot->loadInternalWebhooks(); // Load the internal "output" webhook $bot->enableWebserver(8080, 'secret'); // This will listen on port 8080 $bot->run();
在此分支中更改
使用参数 "webhook" 触发相应的 webhook。在上面的示例中,"webhook" 的值是 "output"。
输入格式可以是 JSON 或带有 JSON 编码有效载荷的 POST 请求,如 https://api.slack.com/incoming-webhooks 中所述。Web 服务器使用 Content-Type 标头区分这两种格式。
JSON
curl -X POST -H 'Content-Type: application/json; charset=utf8' --data '{"webhook":
"output", "type" : "message", "text": "This is a message", "channel": "#general"}' https://:8080
POST 字段(JSON 编码有效载荷)
curl -X POST --data-urlencode 'webhook=output' --data-urlencode 'payload={"type" : "message", "text": "This is a message", "channel": "#general"}' https://:8080
此外,webhook 的响应现在是 JSON 编码的。您的自定义 webhook 的 execute() 方法返回值将位于成功请求的 "data" 属性中。否则,将填充 "error" 属性。