What is a WebHook? We could define it as a method to make some web pages reactive to user input based on a simple HTTP POST method to support a user-defined HTTP callback. Still a bit obscure, isn't it? Let's put it this way: Slack has some endpoints, sensitive URLs; and when you post something through HTTP to endpoints, you actually communicate with Slack. What makes these WebHook interesting is that they are stateless, since they do not rely on a continuously open connection to the service; and you just ping Slack whenever you need to post or retrieve some pieces of information. Slack supports two different kinds of WebHooks:
- Incoming WebHook: This is the URL that we will be posting to when we want some messages to appear on our test channel
- Outgoing WebHook: This is the URL that Slack uses to notify us of some events in a channel
We will be using the Incoming WebHook for our notification script. So, what will we need? We will need the following things:
- Slack incoming WebHook linked to one of our channels.
- A JSON holding the message we want to post.
- An application, which will connect to the URL and post the JSON. It will be our script.
- Our first task will be to create a new incoming WebHook and link it to our test channel. As the administrator of the team login, we have to log in at https://my.slack.com/services/new/incoming-webhook/. And from the dropdown menu, we must select our test channel as shown here:
- Select the #test channel from the drop-down menu.
- Now, let's click on the Add Incoming WebHooks Integration button; and we are led to the Incoming WebHooks page, where we will find the URL that we have to call to send a message to our test channel.
- In the Incoming WebHooks page, you can find your newly created WebHook.
As explained earlier, we have two options to send a message through this WebHook:
- As a JSON string in the payload parameter of a POST request
- As a JSON string in the body of a POST request
So, JSON is something crucial for our messaging system, but what exactly is a JSON?