See also: Slack\Message\MessageInterface
Inheritance: implements MessageInterface
Example #1
0
 public function testGetUser()
 {
     $userId = $this->faker->uuid;
     $message = new Message($this->client, ['id' => $this->faker->uuid, 'user' => $userId]);
     $this->mockResponse(200, null, ['ok' => true, 'user' => ['id' => $userId]]);
     $this->watchPromise($message->getUser()->then(function (User $user) use($userId) {
         $this->assertEquals($userId, $user->getId());
     }));
 }
Example #2
0
 /**
  * @param Message $message
  * @return string
  */
 public function execute(Message $message)
 {
     $commandName = explode(' ', $message->getText())[0];
     if ($this->isCommandNative($commandName)) {
         return $this->processNativeCommands($commandName);
     }
     if ($this->isCommandExists($commandName)) {
         $command = $this->commandList[$commandName];
         return $command->execute($message);
     } else {
         return sprintf("Command '%s' is not exists.", $commandName);
     }
 }
Example #3
0
 /**
  * @covers Slack\Notifier::notify
  */
 public function testNotify()
 {
     $client = $this->getMockBuilder('\\Slack\\Client')->disableOriginalConstructor()->getMock(array('post'));
     $request = $this->getMockBuilder('\\Guzzle\\Http\\Message\\Request')->disableOriginalConstructor()->getMock(array('send'));
     $message = new Message('Hello world');
     $message->setChannel('#test')->setIconEmoji(':ghost:')->setUsername('slack-php');
     $attachment = new \Slack\Message\MessageAttachment();
     $field = new \Slack\Message\MessageField();
     $field->setTitle('foo')->setValue('bar')->setShort(false);
     $attachment->addField($field);
     $message->addAttachment($attachment);
     $expectedDatas = json_encode(array('text' => $message->getText(), 'channel' => $message->getChannel(), 'username' => 'slack-php', 'icon_emoji' => ':ghost:', 'attachments' => array(array('fields' => array(array('title' => 'foo', 'value' => 'bar', 'short' => false))))));
     $client->expects($this->once())->method('post')->with($this->equalTo('/services/hooks/incoming-webhook'), $this->anything(), $this->equalTo($expectedDatas), $this->anything())->will($this->returnValue($request));
     $notifier = new Notifier($client);
     $notifier->notify($message);
 }
 /**
  * {@inheritDoc}
  */
 public function postMessage(Message $message)
 {
     if (!$this->connected) {
         return Promise\reject(new ConnectionException('Client not connected. Did you forget to call `connect()`?'));
     }
     // We can't send attachments using the RTM API, so revert to the web API
     // to send the message
     if ($message->hasAttachments()) {
         return parent::postMessage($message);
     }
     $data = ['id' => ++$this->lastMessageId, 'type' => 'message', 'channel' => $message->data['channel'], 'text' => $message->getText()];
     $this->websocket->send(json_encode($data));
     // Create a deferred object and add message to pending list so when a
     // success message arrives, we can de-queue it and resolve the promise.
     $deferred = new Promise\Deferred();
     $this->pendingMessages[$this->lastMessageId] = $deferred;
     return $deferred->promise();
 }
Example #5
0
 /**
  * Posts a message.
  *
  * @param \Slack\Message\Message $message The message to post.
  *
  * @return \React\Promise\PromiseInterface
  */
 public function postMessage(Message $message)
 {
     $options = ['text' => $message->getText(), 'channel' => $message->data['channel'], 'as_user' => true];
     if ($message->hasAttachments()) {
         $options['attachments'] = json_encode($message->getAttachments());
     }
     return $this->apiCall('chat.postMessage', $options);
 }