/slackbot

A PHP Slack Bot using the new Event API

Primary LanguagePHP

PHP SlackBot

Example usage

/** @var SlackBot $slackBot */
$slackBot = app(SlackBot::class);
$slackBot->initialize(env('SLACK_TOKEN'));

// Listen to simple commands
$slackBot->hears('Hello', function (SlackBot $bot) {
    $bot->respond('Hi there!');
});

// Include regular expression matches
$slackBot->hears('Call me {name} the {attribute}', function (SlackBot $bot, $name, $attribute) {
    $bot->respond('Hi '.$name.'! You truly are '.$attribute);
});

// Use conversations
$slackBot->hears('order pizza', function (SlackBot $bot, $matches) {
    $bot->startConversation(new OrderPizzaConversation());
});

Conversation Syntax

use Mpociot\SlackBot\Conversation;

class OrderPizzaConversation extends Conversation
{

    protected $size;
    
    protected $toppings;
    
    public function askSize()
    {
        $this->ask('Which size do you want?', function($answer) {
            $this->reply('Got you - your pizza needs to be '.$answer);
            $this->size = $answer;
            
            $this->askTopping();
        });
    }
    
    public function askTopping()
    {
        $this->ask('What toppings do you want?', function($answer) {
            $this->reply('Okay, I\'ll put some '.$answer.' on your pizza');
            $this->toppings = $answer;
            
        });
    }
    
    public function run()
    {
        $this->askSize();
    }
}

License

SlackBot is free software distributed under the terms of the MIT license.