PhpSlackBot\Bot::run PHP Method

run() public method

public run ( )
    public function run()
    {
        if (!isset($this->params['token'])) {
            throw new \Exception('A token must be set. Please see https://my.slack.com/services/new/bot');
        }
        $this->init();
        $logger = new \Zend\Log\Logger();
        $writer = new \Zend\Log\Writer\Stream("php://output");
        $logger->addWriter($writer);
        $loop = \React\EventLoop\Factory::create();
        $client = new \Devristo\Phpws\Client\WebSocket($this->wsUrl, $loop, $logger);
        $client->on("request", function ($headers) use($logger) {
            $logger->notice("Request object created!");
        });
        $client->on("handshake", function () use($logger) {
            $logger->notice("Handshake received!");
        });
        $client->on("connect", function () use($logger, $client) {
            $logger->notice("Connected!");
        });
        $client->on("message", function ($message) use($client, $logger) {
            $data = $message->getData();
            $logger->notice("Got message: " . $data);
            $data = json_decode($data, true);
            if (count($this->catchAllCommands)) {
                foreach ($this->catchAllCommands as $command) {
                    $command->setClient($client);
                    $command->setContext($this->context);
                    $command->executeCommand($data, $this->context);
                }
            }
            $command = $this->getCommand($data);
            if ($command instanceof Command\BaseCommand) {
                $command->setClient($client);
                $command->setChannel($data['channel']);
                $command->setUser($data['user']);
                $command->setContext($this->context);
                $command->executeCommand($data, $this->context);
            }
        });
        $client->open();
        /* Webserver */
        if (null !== $this->webserverPort) {
            $logger->notice("Listening on port " . $this->webserverPort);
            $socket = new \React\Socket\Server($loop);
            $http = new \React\Http\Server($socket);
            $http->on('request', function ($request, $response) use($client) {
                $request->on('data', function ($data) use($client, $request, $response) {
                    parse_str($data, $post);
                    if ($this->authentificationToken === null || $this->authentificationToken !== null && isset($post['auth']) && $post['auth'] === $this->authentificationToken) {
                        if (isset($post['name']) && is_string($post['name']) && isset($this->webhooks[$post['name']])) {
                            $hook = $this->webhooks[$post['name']];
                            $hook->setClient($client);
                            $hook->setContext($this->context);
                            $hook->executeWebhook(json_decode($post['payload'], true), $this->context);
                            $response->writeHead(200, array('Content-Type' => 'text/plain'));
                            $response->end("Ok\n");
                        } else {
                            $response->writeHead(404, array('Content-Type' => 'text/plain'));
                            $response->end("No webhook found\n");
                        }
                    } else {
                        $response->writeHead(403, array('Content-Type' => 'text/plain'));
                        $response->end("");
                    }
                });
            });
            $socket->listen($this->webserverPort);
        }
        $loop->run();
    }

Usage Example

Beispiel #1
0
<?php

use gries\Pokebot\Command\PokemathCommand;
use PhpSlackBot\Bot;
require_once __DIR__ . '/../vendor/autoload.php';
$bot = new Bot();
$bot->setToken('token');
$bot->loadCommand(new PokemathCommand());
$bot->run();