Ru: http://phpprofi.ru/blogs/post/96
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.
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 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.
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:
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,
- 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 via our favourite Composer:
composer require alek13/slack
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.
// 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:
$client->send('Hello world!');
That's all!
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.
Top comments (3)
I just use curl. It's dead simple script.
Most things possible to write with native php. But people usually use ready libraries (ready for production).
If you use curl & if you will write with
PHP right way
, you would write something like a part ofguzzle
and then something like thisalek13/slack
package.But if you write for own goals and not for production project you can use just curl
Would you share that curl code with us?