You are here

public function Slack::sendMessage in Slack 8

Sends a message to a Slack channel.

Parameters

string $message: The message sent to the channel.

string $channel: The channel in the Slack service to send messages.

string $username: The bot name displayed in the channel.

Return value

bool|object Slack response.

Throws

\GuzzleHttp\Exception\GuzzleException

Overrides SlackInterface::sendMessage

File

src/Slack.php, line 70

Class

Slack
Send messages to Slack.

Namespace

Drupal\slack

Code

public function sendMessage($message, $channel = '', $username = '') {
  $config = $this->config
    ->get('slack.settings');
  $webhook_url = $config
    ->get('slack_webhook_url');
  if (empty($webhook_url)) {
    $this->messenger
      ->addError($this
      ->t('You need to enter a webhook!'));
    return FALSE;
  }
  $this->logger
    ->get('slack')
    ->info('Sending message "@message" to @channel channel as "@username"', [
    '@message' => $message,
    '@channel' => $channel,
    '@username' => $username,
  ]);
  $config = $this
    ->prepareMessage($webhook_url, $channel, $username);
  $result = $this
    ->sendRequest($config['webhook_url'], $message, $config['message_options']);
  return $result;
}