/** * Prepare onMessage handler */ protected function prepareOnMessage() { $this->client->on('message', function (Payload $data) { /** * Skip self messages */ if ($data['user'] === $this->botUser->getId()) { return; } $this->logger->info(sprintf("Message received: %s", json_encode($data->getData()))); $message = new Message($this->client, $data->getData()); $invokeResult = $this->invoker->execute($message); $this->sendMessageByDestId($invokeResult, $data['channel']); $this->logger->info(sprintf("Response sent: %s", $invokeResult)); }); }
public function run() { /* * Create the event loop */ $eventLoop = Factory::create(); /* * Create our Slack client */ $client = new RealTimeClient($eventLoop); $client->setToken(getenv('BOT_TOKEN')); /* * Setup command bindings */ $commandBindings = ['help' => HelpCommand::class, 'start' => StartCommand::class, 'end' => EndCommand::class, 'see' => SeeCommand::class, 'vote' => VoteCommand::class, 'kill' => KillCommand::class, 'guard' => GuardCommand::class]; /* * Create the game manager */ $gameManager = new GameManager($client, $commandBindings); /* * Route incoming Slack messages */ $client->on('message', function ($data) use($client, $gameManager) { $gameManager->input(new Message($data)); }); /* * Connect to Slack */ echo "Connecting...\r\n"; $client->connect()->then(function () { echo "Connected.\n"; }, function (ConnectionException $e) { echo $e->getMessage(); exit; }); /* * Start the event loop */ $eventLoop->run(); }
/** * Run the bot process. * * @return void */ public function run() { $loop = \React\EventLoop\Factory::create(); $client = new RealTimeClient($loop); $client->setToken($this->token); // Respond to RTM messages $client->on('message', function ($payload) use($client) { // Output debug $this->debug('Message received', $payload->getData()); // Create a message entity from payload $message = new Message($client, $payload); // Certain message types are blocked if (!$message->isValidForReaction()) { return; } // Execute relevant command $this->react($message); }); $this->debug('Connecting to RTM socket'); $client->connect()->then(function () { $this->debug('Connected to RTM socket'); }); $loop->run(); }