createMessage() public method

Create a new message with defaults.
public createMessage ( ) : Message
return Message
Example #1
0
 /**
  * Sends the log message to slack.
  */
 public function export()
 {
     $error = ArrayHelper::getValue($this->messages[0], 0, 'Unknown Error');
     $message = $this->client->createMessage();
     $text = is_object($error) ? $error->__toString() : $error;
     $message->attach(['title' => ArrayHelper::getValue($this->messages[0], 2, 'Site Error'), 'text' => $text, 'fallback' => $text, 'color' => $this->color, 'fields' => $this->getContextAttachment()]);
     $message->send();
 }
Example #2
0
 /**
  * Send a Slack message.
  *
  * @param string $payload
  */
 public function send($payload)
 {
     $message = $this->client->createMessage();
     if (!is_string($payload)) {
         $text = null;
         if (is_array($payload)) {
             $text = $payload['message'];
         } elseif ($payload instanceof \Exception) {
             $text = $payload->getMessage();
         }
         return $message->attach($this->buildAttachment($payload))->setText($text)->send();
     }
     return $message->setText($payload)->send();
 }
Example #3
0
 public function testCreateMessage()
 {
     $defaults = ['channel' => '#random', 'username' => 'Archer', 'icon' => ':ghost:'];
     $client = new Client('http://fake.endpoint', $defaults);
     $message = $client->createMessage();
     $this->assertInstanceOf('Maknz\\Slack\\Message', $message);
     $this->assertSame($client->getDefaultChannel(), $message->getChannel());
     $this->assertSame($client->getDefaultUsername(), $message->getUsername());
     $this->assertSame($client->getDefaultIcon(), $message->getIcon());
 }
 public function testMessageWithAttachmentsAndFields()
 {
     $attachmentArray = ['fallback' => 'Some fallback text', 'text' => 'Some text to appear in the attachment', 'pretext' => null, 'color' => 'bad', 'mrkdwn_in' => [], 'image_url' => 'http://fake.host/image.png', 'thumb_url' => 'http://fake.host/image.png', 'title' => 'A title', 'title_link' => 'http://fake.host/', 'author_name' => 'Joe Bloggs', 'author_link' => 'http://fake.host/', 'author_icon' => 'http://fake.host/image.png', 'fields' => [['title' => 'Field 1', 'value' => 'Value 1', 'short' => false], ['title' => 'Field 2', 'value' => 'Value 2', 'short' => false]]];
     $expectedHttpData = ['username' => 'Test', 'channel' => '#general', 'text' => 'Message', 'link_names' => false, 'unfurl_links' => false, 'unfurl_media' => true, 'mrkdwn' => true, 'attachments' => [$attachmentArray]];
     $client = new Client('http://fake.endpoint', ['username' => 'Test', 'channel' => '#general']);
     $message = $client->createMessage()->setText('Message');
     $attachment = new Attachment($attachmentArray);
     $message->attach($attachment);
     $payload = $client->preparePayload($message);
     $this->assertEquals($expectedHttpData, $payload);
 }
 /**
  * @param string $message
  * @param string $type
  * @param array $attachment
  */
 public function post($message, $type, $attachment = [])
 {
     $type = \Config::get('slack.types.' . strtolower($type), \Config::get('slack.default', []));
     $webHookUrl = \Config::get('slack.webHookUrl');
     $client = new Client($webHookUrl, ['username' => array_get($type, 'username', 'FamarryBot'), 'channel' => array_get($type, 'channel', '#random'), 'link_names' => true, 'icon' => array_get($type, 'icon', ':smile:')]);
     $messageObj = $client->createMessage();
     if (!empty($attachment)) {
         $attachment = new Attachment(['fallback' => array_get($attachment, 'fallback', ''), 'text' => array_get($attachment, 'text', ''), 'pretext' => array_get($attachment, 'pretext', ''), 'color' => array_get($attachment, 'color', 'good'), 'fields' => array_get($attachment, 'fields', [])]);
         $messageObj->attach($attachment);
     }
     $messageObj->setText($message)->send();
 }
Example #6
0
 public function testMessageWithAttachmentsAndActions()
 {
     $now = new DateTime();
     $attachmentInput = ['fallback' => 'Some fallback text', 'text' => 'Some text to appear in the attachment', 'pretext' => null, 'color' => 'bad', 'footer' => 'Footer', 'footer_icon' => 'https://platform.slack-edge.com/img/default_application_icon.png', 'timestamp' => $now, 'mrkdwn_in' => [], 'image_url' => 'http://fake.host/image.png', 'thumb_url' => 'http://fake.host/image.png', 'title' => 'A title', 'title_link' => 'http://fake.host/', 'author_name' => 'Joe Bloggs', 'author_link' => 'http://fake.host/', 'author_icon' => 'http://fake.host/image.png', 'fields' => [], 'actions' => [['name' => 'Name 1', 'text' => 'Text 1', 'style' => 'default', 'type' => 'button', 'value' => 'Value 1', 'confirm' => ['title' => 'Title 1', 'text' => 'Text 1', 'ok_text' => 'OK Text 1', 'dismiss_text' => 'Dismiss Text 1']], ['name' => 'Name 2', 'text' => 'Text 2', 'style' => 'default', 'type' => 'button', 'value' => 'Value 2', 'confirm' => ['title' => 'Title 2', 'text' => 'Text 2', 'ok_text' => 'OK Text 2', 'dismiss_text' => 'Dismiss Text 2']]]];
     $attachmentOutput = ['fallback' => 'Some fallback text', 'text' => 'Some text to appear in the attachment', 'pretext' => null, 'color' => 'bad', 'footer' => 'Footer', 'footer_icon' => 'https://platform.slack-edge.com/img/default_application_icon.png', 'ts' => $now->getTimestamp(), 'mrkdwn_in' => [], 'image_url' => 'http://fake.host/image.png', 'thumb_url' => 'http://fake.host/image.png', 'title' => 'A title', 'title_link' => 'http://fake.host/', 'author_name' => 'Joe Bloggs', 'author_link' => 'http://fake.host/', 'author_icon' => 'http://fake.host/image.png', 'fields' => [], 'actions' => [['name' => 'Name 1', 'text' => 'Text 1', 'style' => 'default', 'type' => 'button', 'value' => 'Value 1', 'confirm' => ['title' => 'Title 1', 'text' => 'Text 1', 'ok_text' => 'OK Text 1', 'dismiss_text' => 'Dismiss Text 1']], ['name' => 'Name 2', 'text' => 'Text 2', 'style' => 'default', 'type' => 'button', 'value' => 'Value 2', 'confirm' => ['title' => 'Title 2', 'text' => 'Text 2', 'ok_text' => 'OK Text 2', 'dismiss_text' => 'Dismiss Text 2']]]];
     $client = new Client('http://fake.endpoint', ['username' => 'Test', 'channel' => '#general']);
     $message = $client->createMessage()->setText('Message');
     $attachment = new Attachment($attachmentInput);
     $message->attach($attachment);
     $payload = $client->preparePayload($message);
     $expectedHttpData = ['username' => 'Test', 'channel' => '#general', 'text' => 'Message', 'link_names' => 0, 'unfurl_links' => false, 'unfurl_media' => true, 'mrkdwn' => true, 'attachments' => [$attachmentOutput]];
     $this->assertEquals($expectedHttpData, $payload);
 }
Example #7
0
 /**
  * Send slack report.
  *
  * @return mixed
  */
 public function send()
 {
     return $this->client->createMessage()->attach($this->buildPayload())->send();
 }
 /**
  * Create a new message with defaults
  *
  * @return \Maknz\Slack\Message 
  * @static 
  */
 public static function createMessage()
 {
     return \Maknz\Slack\Client::createMessage();
 }
Example #9
0
 /**
  * @param Release $release
  * @param string $channel
  * @param string $title
  * @param array $message
  */
 protected function sendNotification(Release $release, $channel, $title, array $message)
 {
     $message = $this->slack->createMessage()->to($channel)->attach($message + ['fallback' => $title, 'fields' => $this->describeCommitFields($release, $release->commit())]);
     $this->slack->send($message);
 }