Code Monkey home page Code Monkey logo

php-slack-bot's Introduction

PHP Slack Bot

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

Installation

With Composer

Create a new composer.json file and add the following

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

Then run

composer install

Usage

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

Example commands

Example commands are located in src/PhpSlackBot/Command/

Ping Pong Command

Type ping in a channel and the bot should answer "Pong" to you.

Count Command

Type count several times in a channel and the bot should answer with 1 then 2...

Date Command

Type date in a channel and the current date.

Planning Poker Command

https://en.wikipedia.org/wiki/Planning_poker

Type pokerp start in a public channel with your team in order to start a planning poker session.

Direct message the bot with pokerp vote number. The bot will record your vote.

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

Type pokerp end in a public channel and the bot will output each vote.

Load your own commands

You can load your own commands by implementing the \PhpSlackBot\Command\BaseCommand.

Then call PhpSlackBot\Bot::loadCommand method for each command you have to load.

"Catch All" command

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

This special command will be triggered on all events and all other commands will be ignored.

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.

Commands are triggered from users messages inside Slack and webhooks are triggered from web post requests.

Custom webhooks can be loaded using the PhpSlackBot\Bot::loadWebhook method.

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

To enable webhooks, use the enableWebserver method before the run method.

You can also set a secret token to prevent unauthorized requests.

$bot->enableWebserver(8080, 'secret'); // This will listen on port 8080
$bot->run();

Then, use the parameter "name" to trigger the corresponding 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

php-slack-bot's People

Contributors

jclg avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.