marcuspi/slacker

一个用于自定义Slack集成的超级简单库

1.0 2016-04-26 11:45 UTC

This package is auto-updated.

Last update: 2024-08-29 04:36:52 UTC


README

一个简单的PHP库,用于Slack Webhooks

安装

您可以下载一个版本并使用require_once('Slacker.php'),或者将"marcuspi/slacker": "dev-master"添加到您的composer.json文件中。

用法

入站Webhooks和命令

<?php

$token = "MY_TOKEN';
$trigger = 'MY_TRIGGER';
$command = 'COMMAND';

try {
    $slack = new Slacker\SlackRequest([$token]); # Look for a request in POST data
} catch catch(Slacker\InvalidRequestException $e) {
    die(echo $e->getMessage());
} catch(Slacker\InvalidTokenException $e) {
    die($e->getMessage() . '(got: *' . $e->getToken() . '*)');
}

# Responding to a trigger
if ($slack->isWebhook() && ($slack->triggerWord() == $trigger)) {
    $response = new Slacker\SlackResponse($slack);
    $response->setText('I\'m responding to the *trigger word* `' . $trigger . '` in the message `' . $slack->text() . '`');
    $response->respond();
}

# Responding to commands
if ($slack->isCommand() && ($slack->command() == $command)) {
    $response = new Slacker\SlackResponse($slack);
    $response->setText('I\'m responding to the *command* `' . $command . '` with the arguments `' . implode('`, `', $slack->args() . '`');
    $response->respond();
}

添加附件

使用SlackResponse->addAttachment($att)Slacker\SlackAttachment注入到响应中。

<?php

$att = new Slacker\SlackAttachment();
$att->setColor('good');
$att->setTitle('Everything is fine');
$att->setText($out);
$att->markdown(true, true, false);
$someResponse->addAttachment($att);

入站Webhooks

<?php

$channel = 'CHANNEL';
$username = 'USERNAME';
$webhookUrl = 'GET YOUR OWN TOKEN';

$incoming = new Slacker\SlackMessage($webhookUrl);
    $incoming->setText(':star2: Hello :star2:');
    $incoming->setChannel($channel);
    $incoming->setUsername($username);

$att = new Slacker\SlackAttachment();
    $att->setText('This is an attachment text!');
    $att->setColor('warning');
    $incoming->addAttachment($att);

$incoming->send(); # sends the request

查看代码以获取更多文档.