You are here

protected function Slack::sendRequest in Slack 8

Send message to the Slack with more options.

Parameters

string $webhook_url: Webhook for Slack basic functions.

string $message: The message sent to the channel.

array $message_options: An associative array, it can contain:

  • channel: The channel in the Slack service to send messages;
  • username: The bot name displayed in the channel;
  • icon_emoji: The bot icon displayed in the channel;
  • icon_url: The bot icon displayed in the channel.

Return value

\Psr\Http\Message\ResponseInterface|bool Can contain: success fail fail

  • data: ok No hooks Invalid channel specified
  • status message: OK Not found Server Error
  • code: 200 404 500
  • error: - Not found Server Error

Throws

\GuzzleHttp\Exception\GuzzleException

1 call to Slack::sendRequest()
Slack::sendMessage in src/Slack.php
Sends a message to a Slack channel.

File

src/Slack.php, line 164

Class

Slack
Send messages to Slack.

Namespace

Drupal\slack

Code

protected function sendRequest($webhook_url, $message, array $message_options = []) {
  $headers = [
    'Content-Type' => 'application/x-www-form-urlencoded',
  ];
  $message_options['text'] = $this
    ->processMessage($message);
  $sending_data = 'payload=' . urlencode(json_encode($message_options));
  $logger = $this->logger
    ->get('slack');
  try {
    $response = $this->httpClient
      ->request('POST', $webhook_url, [
      'headers' => $headers,
      'body' => $sending_data,
    ]);
    $logger
      ->info('Message was successfully sent!');
    return $response;
  } catch (ServerException $e) {
    $logger
      ->error('Server error! It may appear if you try to use unexisting chatroom.');
    watchdog_exception('slack', $e);
    return FALSE;
  } catch (RequestException $e) {
    $logger
      ->error('Request error! It may appear if you entered the invalid Webhook value.');
    watchdog_exception('slack', $e);
    return FALSE;
  }
}