jclg/php-slack-bot

用PHP编写的Slack机器人用户

0.0.7 2018-09-30 09:08 UTC

This package is not auto-updated.

Last update: 2024-09-15 23:12:38 UTC


README

一个简单的用PHP编写的Slack实时消息API(https://api.slack.com/rtm)的机器人用户 https://api.slack.com/rtm

安装

使用Composer

composer require jclg/php-slack-bot

使用方法

创建一个名为 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 POST请求触发。

可以使用 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();

然后,使用 "name" 参数触发相应的webhook

curl -X POST --data-urlencode 'auth=secret' --data-urlencode 'name=output' --data-urlencode 'payload={"type" : "message", "text": "This is a message", "channel": "#general"}' http://localhost:8080

主动消息

上面 "使用方法" 部分提供的示例将机器人设置为响应式。响应式机器人无法自己向任何用户发送消息。它必须接收到输入才能做出响应。换句话说,机器人只能对它收到的输入做出响应。

主动消息 意味着作为开发者,您将能够 在用户先向机器人发送消息之前向用户发送消息。这在您需要通知用户某些事情时很有用,例如一个可以检查用户的生日并祝福他们的机器人,或者每两小时告诉他们一次外面的天气,而无需他们每次都输入命令。还有很多其他用途。

您可以这样使用主动消息

$bot = new Bot();
$bot->setToken('TOKEN'); // Get your token here https://my.slack.com/services/new/bot
$bot->loadInternalCommands(); // This loads example commands
$bot->loadPushNotifier(function () {
	return [
		'channel' => '#general',
		'username' => '@slacker',
		'message' => "Happy Birthday!! Make sure you have fun today. :-)"
	];
});

$bot->loadPushNotifier(function () {
	return [
		'channel' => '@slacker',
		'username' => null,
		'message' => "Current UNIX timestamp is: " . time()
	];
}, 1800);
$bot->run();

在上面的示例中,我们设置了两个主动消息。

第一条消息 将在 "#general" 频道中发送以下消息

@slacker Happy Birthday!! Make sure you have fun today. :-)

它只会在启动脚本后的10秒内触发一次。到那时,Slack服务器通常会建立连接。

第二条消息是周期性消息。它将发送给团队中用户名为slacker的用户。消息不会提到任何人,并且每30分钟(1800秒)重复一次。消息应显示为

Current UNIX timestamp is: 1489145707
Current UNIX timestamp is: 1489147507

这应该发生在启动后1小时内。

注意:第一条消息将在启动后30分钟出现。

您使用loadPushNotifier添加的功能必须返回包含以下键的数组

  • channel:要发送消息的频道或用户的名称。频道名称应具有#前缀,而用户名必须具有@前缀。如果您没有设置前缀,则假定名称为频道名称。在此处使用前缀是推荐的。
  • username:要提前在消息中提到的任何用户名。您可以在此处指定不带@前缀的名称,尽管您可能希望使用前缀以保持代码中的统一性。
  • message:要发送的实际消息。