2017 © Pedro Peláez
 

library php-slack-bot

Slack bot user written in PHP

image

alex-equity/php-slack-bot

Slack bot user written in PHP

  • Monday, March 20, 2017
  • by alex-equity
  • Repository
  • 1 Watchers
  • 0 Stars
  • 34 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 55 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

PHP Slack Bot

A simple bot user written in PHP using the Slack Real Time Messaging API https://api.slack.com/rtm, (*1)

Installation

With Composer, (*2)

Create a new composer.json file and add the following, (*3)

{
    "minimum-stability" : "dev",
    "require": {
        "jclg/php-slack-bot": "dev-master"
    }
}

Then run, (*4)

composer install

Usage

Create a php file called bot.php with the following content, (*5)

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

Then run php bot.php from the command line (terminal)., (*6)

Example commands

Example commands are located in src/PhpSlackBot/Command/ and can be loaded with $bot->loadInternalCommands();, (*7)

Ping Pong Command

Type ping in a channel and the bot should answer "Pong" to you., (*8)

Count Command

Type count several times in a channel and the bot should answer with 1 then 2..., (*9)

Date Command

Type date in a channel and the current date., (*10)

Planning Poker Command

https://en.wikipedia.org/wiki/Planning_poker, (*11)

Type pokerp start in a public channel with your team in order to start a planning poker session., (*12)

Direct message the bot with pokerp vote number. The bot will record your vote., (*13)

Type pokerp status to see the current status of the session (who has voted)., (*14)

Type pokerp end in a public channel and the bot will output each vote., (*15)

Load your own commands

You can load your own commands by implementing the \PhpSlackBot\Command\BaseCommand., (*16)

Then call PhpSlackBot\Bot::loadCommand method for each command you have to load., (*17)

"Catch All" command

If you need to execute a command when an event occurs, you can set up a "catch all" command., (*18)

This special command will be triggered on all events., (*19)

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

Incoming webhooks

The bot can also listen for incoming webhooks., (*20)

Commands are triggered from users messages inside Slack and webhooks are triggered from web post requests., (*21)

Custom webhooks can be loaded using the PhpSlackBot\Bot::loadWebhook method., (*22)

This is useful if you need to control the bot from an external service. For example, with IFTTT https://ifttt.com/maker, (*23)

To enable webhooks, use the enableWebserver method before the run method., (*24)

You can also set a secret token to prevent unauthorized requests., (*25)

$bot->loadInternalWebhooks(); // Load the internal "output" webhook
$bot->enableWebserver(8080, 'secret'); // This will listen on port 8080
$bot->run();

Altered in this fork:

Use the parameter "webhook" to trigger the corresponding webhook. In the example case above, the "webhook" value is "output"., (*26)

The input format can be either JSON or a POST with a json encoded payload, as described here in https://api.slack.com/incoming-webhooks. The webserver differentiates between the two using the Content-Type header., (*27)

JSON:, (*28)

curl -X POST -H 'Content-Type: application/json; charset=utf8' --data '{"webhook":
 "output", "type" : "message", "text": "This is a message", "channel": "#general"}' http://localhost:8080

POST Fields (json encoded payload):, (*29)

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

Also, the response from a webhook is now json encoded. The return value from your custom webhook's execute() method will be inside the "data" property of a successful request. Otherwise, an "error" property will be populated., (*30)

The Versions

20/03 2017

dev-master

9999999-dev https://github.com/alex-equity/php-slack-bot

Slack bot user written in PHP

  Sources   Download

MIT

The Requires

 

by Avatar jclg

05/12 2016

0.0.3

0.0.3.0 https://github.com/jclg/php-slack-bot

Slack bot user written in PHP

  Sources   Download

MIT

The Requires

 

by Avatar jclg

23/11 2016

0.0.2

0.0.2.0 https://github.com/jclg/php-slack-bot

Slack bot user written in PHP

  Sources   Download

The Requires

 

by Avatar jclg