Slack. Send simple message.
Tags: php, webdev, opensource, slack
Published: 2018-02-19 22:32:43

---
title: Slack. Send simple message.
published: true
description: Sending a message to Slack with PHP
tags: php, webdev, opensource, slack
---

Ru: http://phpprofi.ru/blogs/post/96

----

Recently I wrote the post about [reincarnation of popular PHP package for Slack](https://dev.to/phpprofi/reincarnation-of-popular-php-package-for-slack-2p3i), in which it was told about fork of popular package and about the resumption of support of this package. But there is nothing about its usage. So, today I want to tell about how quickly implement sending of messages to Slack with PHP.

# Sending a message to Slack with PHP

First, let's take a look at the implementation in general. We need to do 3 small and simple things to send a message from PHP:
 - [Create incoming webhook](https://my.slack.com/services/new/incoming-webhook) in the Slack admin area, & copy the url
 - Install [the package](https://packagist.org/packages/alek13/slack) via Composer
 - Some simple code for creating and sending massage:
   - Create a client by passing the hook url (copied in 1 step)
   - Send the message

Actually, that's all. This information is enough to realize what was conceived, without reading further, but let's go through each point and reveal it in more detailed.

## Creating incoming webhook

The message initially sends to the Slack servers. And then these servers delivers the message to the end users. For Slack servers (and for the end user), the message you sent to these servers is an _incoming_. Therefore, it is called _incoming_, even though it is outgoing for you at the moment of sending. The main thing here is not to get confused. Look at this from the position of the end user, and everything falls into place.

To create an incoming webhook, go to https://my.slack.com/services/new/incoming-webhook. You will be redirected to your workspace - the last one, the admin panel of which you visited. If you do not have access to this functionality, ask your slack administrator to create an incoming webhook and send you an `url` to it.

If you have several workspaces, go to the desired one by using the menu in the upper right corner:

![](http://phpprofi.ru/resources/img/blogs/fb7486dd-706c-493d-892e-d6bdb7c3f484.jpeg)

Next, you will be asked to choose the channel the messages will be delivered to:

![](http://phpprofi.ru/resources/img/blogs/7056f524-c970-4416-b7f5-5c84c823e0c2.jpeg)

If you thought that for each channel (which you want to send) you need to create a hook, then the idea is logical, but it is not necessary. When sending a message from the code, you can specify the channel to which the message should be delivered. Or do not specify the channel and the message will be sent to the **default channel** that you selected in this step.

After that, a new webhook will be created. You will be transferred to a page with its settings, with the information disclosed how to use it, how to generate a message and a simple example of sending it through the curl command.

![](http://phpprofi.ru/resources/img/blogs/c93a51b3-401c-4a7d-b96e-ae072fa841a1.jpeg)

Here you can immediately copy `Webhook URL`, close the admin panel and proceed to install the package and code.

If you want to add a bit of beauty, then fold these instructions or scroll down to the hook settings:

![](http://phpprofi.ru/resources/img/blogs/98ef6c6d-af88-431e-8b30-f426f7be2950.jpeg)

Here you can change:
 - the default channel,
 - regenerate `url` (for example, if it was compromised, like this one),
 - set "user name" - more precisely, name of your application that sends messages,
 - select an icon or logo,
 - ... and see how the message will looks in the end.

A lot of words, but its done in two minutes. The Slack admin area is no longer needed, we can close it. Do not forget to **copy `hook_url`**.

## Installing the package

You can install [the package](https://packagist.org/packages/alek13/slack) via our favourite [Composer](http://getcomposer.org/):

```
composer require alek13/slack
```

![](http://phpprofi.ru/resources/img/blogs/436657fd-5dfa-438e-ae89-52caf2d31b79.jpeg)

That's all! The package is installed and ready to use.

## Sending a message with PHP

First, we need a client instance. To create it minimally what is required is that  `url` to incoming webhook, which we copied when it was created.

```php
// get from your config
$hookUrl = 'https://hooks.slack.com/services/Txxxxxxxx/Bxxxxxxxx/xxxxxxxxxxxxxxxxxxxxxxxx';

$client = new Maknz\Slack\Client($hookUrl);
```

Or you can change the default channel, user name, icon and some other settings:

```php
// get from your config
$hookUrl  = 'https://hooks.slack.com/services/Txxxxxxxx/Bxxxxxxxx/xxxxxxxxxxxxxxxxxxxxxxxx';
$settings = [
	'username' => 'Php Profi Bot',
	'channel' => '#general',
];

$client = new Maknz\Slack\Client($hookUrl, $settings);
```

More information about the settings, you can [see here](https://github.com/alek13/slack#settings).

And now just send a message:

```php
$client->send('Hello world!');
```

**That's all!**

You can see more about sending [here](https://github.com/alek13/slack#sending-messages).

Hope this article will simplify your life when you integrate your application with Slack. If something remains unclear or you just had a question, write in the our [Slack channel](https://join.slack.com/t/php-slack/shared_invite/enQtMjk1OTExNDkzMjg1LTk5ODg3MGE1OThlYzZlM2U0N2I5Y2FhM2NiYmFlMjE1MDNiOWRjMjc1ZDIwNWZlNzBkZWQ2ZWM1NzdmMjM5YzQ).