Recently I wrote the post about reincarnation of popular PHP package for Slack, 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.
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 in the Slack admin area, & copy the url
- Install the package 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.
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:
Next, you will be asked to choose the channel the messages will be delivered to:
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.
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:
Here you can change:
- the default channel,
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
composer require alek13/slack
That's all! The package is installed and ready to use.
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.
// 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:
// 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.
And now just send a message:
You can see more about sending here.
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.