/** * @return bool|AbstractCommand */ public function resolveCommand() { $message = $this->payload->getData()['text']; $parts = explode(':', $message); $command = ucfirst(strtolower($parts[0])); $commandClass = '\\T3Bot\\Commands\\' . $command . 'Command'; if (class_exists($commandClass)) { return new $commandClass($this->payload, $this->client); } $parts = explode(' ', $message); $command = ucfirst(strtolower($parts[0])); $commandClass = '\\T3Bot\\Commands\\' . $command . 'Command'; if (class_exists($commandClass)) { return new $commandClass($this->payload, $this->client); } $resultCommand = false; if (strpos($message, 'botty') !== false || strpos($message, $GLOBALS['config']['slack']['botId']) !== false) { $resultCommand = new BottyCommand($this->payload, $this->client); } return $resultCommand; }
/** * @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); } } }
public function __construct(ApiClient $client, Payload $payload) { $this->client = $client; parent::__construct($payload->getData()); }