Ejemplo n.º 1
0
 public function upload(ApiClientInterface $client)
 {
     $payload = new FilesUploadPayload();
     if ($this->options->has('file')) {
         $filename = $this->getFileName($this->options->get('file'));
         $type = Utils::parseFileExtension($filename);
         $content = $this->readFile($filename);
     } else {
         $filename = $this->options->get('filename');
         $type = $this->options->get('type', Utils::parseFileExtension($filename));
         if (is_null($filename)) {
             $filename = Utils::rand() . '.' . $type;
         }
         $content = $this->readInput();
     }
     if ($this->options->has('filename')) {
         $filename = $this->options->get('filename');
     }
     if ($this->options->has('title')) {
         $payload->setTitle($this->options->get('title'));
     }
     $payload->setFileType($type);
     $payload->setChannels([$this->options->get('channel')]);
     $payload->setContent($content);
     $payload->setFilename($filename);
     return $this->getResponse($client->send($payload));
 }
 /**
  * @param String $channel
  * @param String $message
  * @return bool True if the request was handled successfully, false otherwise
  */
 public function publish($channel, $message)
 {
     $payload = new ChatPostMessagePayload();
     $payload->setChannel($channel);
     $payload->setText($message);
     /** @var \CL\Slack\Payload\ChatPostMessagePayloadResponse $response */
     $response = $this->apiClient->send($payload);
     return $response->isOk();
 }
 public function getAll()
 {
     $payload = new UsersListPayload();
     $response = $this->apiClient->send($payload);
     if ($response->isOk()) {
         return array_map(function (User $user) {
             return $this->userBuilder->getUser($user);
         }, $response->getUsers());
     } else {
         throw new SlackApiException($response->getError());
     }
 }
 public function getMessagesFromAllChannels($sinceTimestamp = null)
 {
     $allMessages = [];
     $payload = new ChannelsListPayload();
     $channels = $this->apiClient->send($payload)->getChannels();
     foreach ($channels as $channel) {
         $channelPayload = new ChannelsHistoryPayload();
         $channelPayload->setChannelId($channel->getId());
         $channelPayload->setCount(100000);
         if (null !== $sinceTimestamp) {
             $channelPayload->setOldest($sinceTimestamp);
         }
         $messages = $this->apiClient->send($channelPayload)->getMessages();
         $allMessages = array_merge($allMessages, array_map(function (SimpleMessage $message) use($channel) {
             return $this->messageBuilder->getMessage(new SimpleMessageAdapter($message, $channel));
         }, $messages));
     }
     return $allMessages;
 }
Ejemplo n.º 5
0
 /**
  * {@inheritdoc}
  */
 public function notifySlackUser($from, $to, $message)
 {
     try {
         $payload = new ChatPostMessagePayload();
         $payload->setChannel("@{$to}");
         $payload->setUsername($from);
         $payload->setText($message);
         /** @var ChatPostMessagePayloadResponse $response */
         $response = $this->slack_client->send($payload);
         if (!$response->isOk()) {
             throw new NotifierException(sprintf("Can't send message to slack user: %s (%s)", $response->getError(), $response->getErrorExplanation()));
         }
         return true;
     } catch (SlackException $exception) {
         throw new NotifierException("Can't access to slack api, check credentials.", null, $exception);
     }
 }