private function onNight(Game $game) { $nightMsg = ":crescent_moon: :zzz: The sun sets and the villagers go to sleep."; $this->sendMessageToChannel($game, $nightMsg); $wolves = $game->getPlayersOfRole(Role::WEREWOLF); $wolfMsg = ":crescent_moon: It is night and it is time to hunt. Type !kill #channel @player to make your choice. "; foreach ($wolves as $wolf) { $this->client->getDMByUserId($wolf->getId())->then(function (DirectMessageChannel $channel) use($wolfMsg) { $this->client->send($wolfMsg, $channel); }); } $seerMsg = ":mag_right: Seer, select a player by saying !see #channel @username."; $seers = $game->getPlayersOfRole(Role::SEER); foreach ($seers as $seer) { $this->client->getDMByUserId($seer->getId())->then(function (DirectMessageChannel $channel) use($seerMsg) { $this->client->send($seerMsg, $channel); }); } $bodyGuardMsg = ":muscle: Bodyguard, you may guard someone once per night. That player cannot be eliminated. Type !guard #channel @user"; $bodyguards = $game->getPlayersOfRole(Role::BODYGUARD); foreach ($bodyguards as $bodyguard) { $this->client->getDMByUserId($bodyguard->getId())->then(function (DirectMessageChannel $channel) use($bodyGuardMsg) { $this->client->send($bodyGuardMsg, $channel); }); } }
/** * Connect to slack server */ protected function connect() { $this->client->connect()->then(function () { return $this->client->getAuthedUser(); })->then(function (User $user) { $this->botUser = $user; $this->logger->info("Connected as " . $this->botUser->getUsername()); }); }
public function emit($event, array $arguments = []) { parent::emit($event, $arguments); if ($arguments[0] instanceof Payload) { if (isset($arguments[0]['user']) and $arguments[0]['user'] === $this->pingu->getId()) { return; } // @todo: Convert to our payload? } call_user_func_array([$this->emitter, 'emit'], array_merge([$event], $arguments)); }
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(); }
/** * @param Game $game */ private function onNight(Game $game) { $client = $this->client; $nightMsg = ":moon: :zzz: The sun sets, and the hard rain makes it difficult to hear anything outside. Villagers bar their doors, take long pulls of :beer:, and try not to think of what might lurk beyond the feeble candlelight. "; $this->sendMessageToChannel($game, $nightMsg); $wolves = $game->getWerewolves(); $wolfMsg = ":moon: It is night and it is time to hunt. Type !kill #channel @player to make your choice. "; foreach ($wolves as $wolf) { $this->client->getDMByUserId($wolf->getId())->then(function (DirectMessageChannel $channel) use($client, $wolfMsg) { $client->send($wolfMsg, $channel); }); } $seerMsg = ":crystal_ball: Seer, select a player by saying !see #channel @username."; $seers = $game->getPlayersOfRole(Role::SEER); foreach ($seers as $seer) { $this->client->getDMByUserId($seer->getId())->then(function (DirectMessageChannel $channel) use($client, $seerMsg) { $client->send($seerMsg, $channel); }); } $bodyGuardMsg = ":shield: Bodyguard, you may guard someone once per with your grizzled ex-lawman skills. That player cannot be eliminated. Type !guard #channel @user"; $bodyguards = $game->getPlayersOfRole(Role::BODYGUARD); foreach ($bodyguards as $bodyguard) { $this->client->getDMByUserId($bodyguard->getId())->then(function (DirectMessageChannel $channel) use($client, $bodyGuardMsg) { $client->send($bodyGuardMsg, $channel); }); } $witches = $game->getPlayersOfRole(Role::WITCH); if (count($witches) > 0) { $witch_msg = ":wine_glass: You may poison someone once for the entire game. Type \"!poison #channel @user\" to poison someone \r\nor \"!poison #channel noone\" to do nothing. \r\n:warning: Night will not end until you make a decision."; if ($game->getWitchPoisonPotion() > 0) { foreach ($witches as $witch) { $this->client->getDMByUserId($witch->getId())->then(function (DirectMessageChannel $channel) use($client, $witch_msg) { $client->send($witch_msg, $channel); }); } } else { $game->setWitchPoisoned(true); } } }
/** * @param \T3Bot\Slack\Message|string $messageToSent * @param string $user * @param string $channel the channel id */ public function sendResponse($messageToSent, $user = null, $channel = null) { $data = []; if ($user !== null) { $this->client->apiCall('im.open', ['user' => $user])->then(function (Payload $response) use($messageToSent) { $channel = $response->getData()['channel']['id']; if ($messageToSent instanceof Message) { $data = []; $data['unfurl_links'] = false; $data['unfurl_media'] = false; $data['parse'] = 'none'; $data['text'] = $messageToSent->getText(); $data['channel'] = $channel; $attachments = $messageToSent->getAttachments(); if (count($attachments)) { $data['attachments'] = array(); } /** @var \T3Bot\Slack\Message\Attachment $attachment */ foreach ($attachments as $attachment) { $data['attachments'][] = Attachment::fromData(['title' => $attachment->getTitle(), 'title_link' => $attachment->getTitleLink(), 'text' => $attachment->getText(), 'fallback' => $attachment->getFallback(), 'color' => $attachment->getColor(), 'pretext' => $attachment->getPretext(), 'author_name' => $attachment->getAuthorName(), 'author_icon' => $attachment->getAuthorIcon(), 'author_link' => $attachment->getAuthorLink(), 'image_url' => $attachment->getImageUrl(), 'thumb_url' => $attachment->getThumbUrl()]); } $message = new \Slack\Message\Message($this->client, $data); $this->client->postMessage($message); } elseif (is_string($messageToSent)) { $data = []; $data['unfurl_links'] = false; $data['unfurl_media'] = false; $data['parse'] = 'none'; $data['text'] = $messageToSent; $data['channel'] = $channel; $data['as_user'] = true; $this->client->apiCall('chat.postMessage', $data); } }); } else { $channel = $channel !== null ? $channel : $this->payload->getData()['channel']; if ($messageToSent instanceof Message) { $data['unfurl_links'] = false; $data['unfurl_media'] = false; $data['parse'] = 'none'; $data['text'] = $messageToSent->getText(); $data['channel'] = $channel; $attachments = $messageToSent->getAttachments(); if (count($attachments)) { $data['attachments'] = array(); } /** @var \T3Bot\Slack\Message\Attachment $attachment */ foreach ($attachments as $attachment) { $data['attachments'][] = Attachment::fromData(['title' => $attachment->getTitle(), 'title_link' => $attachment->getTitleLink(), 'text' => $attachment->getText(), 'fallback' => $attachment->getFallback(), 'color' => $attachment->getColor(), 'pretext' => $attachment->getPretext(), 'author_name' => $attachment->getAuthorName(), 'author_icon' => $attachment->getAuthorIcon(), 'author_link' => $attachment->getAuthorLink(), 'image_url' => $attachment->getImageUrl(), 'thumb_url' => $attachment->getThumbUrl()]); } $message = new \Slack\Message\Message($this->client, $data); $this->client->postMessage($message); } elseif (is_string($messageToSent)) { $data['unfurl_links'] = false; $data['unfurl_media'] = false; $data['parse'] = 'none'; $data['text'] = $messageToSent; $data['channel'] = $channel; $data['as_user'] = true; $this->client->apiCall('chat.postMessage', $data); } } }
/** * 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(); }
public function __construct(LoopInterface $loop, $token, ClientInterface $httpClient = null) { parent::__construct($loop, $httpClient); $this->setToken($token); }