Send updates to Telegram / Slack
by Miguel Crespo

    22 September 2021
    • tutorial
    • gist

The objective of this post is to show how easily you can automate messages without using any specific library. This tutorial will cover sending updates to Telegram and Slack (which are mainly the platforms I use to receive updates of my running processes). Let's go into more details!

Both platforms expose powerful APIs for creating bots, and usually you can find quite complex frameworks designed to operate them. But they could be considered over-engineering if your only use case is to send messages (without waiting for responses). Notice that using a framework forces you to stay only using one language or if you change it, to learn a new framework. The common-sense solution should be to reduce at the minimum point the necessary glue between your application and the message system.

You could be wondering: is it possible?. And the answer is yes!. You just need to make a POST request.

Telegram

To send messages from a program in Telegram you need to create a Bot account. You can follow this tutorial to obtain a bot token. Additionally, you will need the ID of the conversation to which you will send the updates (I recommend to create a group, invite the bot and use it to receive the messages). You can follow this tutorial.

Now you have everything that is needed. The next and final step is to make the POST request to the API's endpoint (e.g., httplib/urllib or requests in Python). In this example, we will use curl and encapsulate it to use in your terminal.

0 function notification_telegram() {
1 TOKEN=$TOKEN
2 ID=$CHAT_ID
3 curl -s -X POST "https://api.telegram.org/bot$TOKEN/sendMessage?chat_id=$ID&parse_mode=markdown&text=$1" > /dev/null 2> / dev/null
4 }

After filling in the previous snippet your personal variables, you can use as follows: >> notification_telegram "**Hello world!**". As a bonus, notice that you can use Markdown!.

This example can be extended to not only send a message, but also an image (which is quite useful if you work in computer graphics 😉).

0 function notification_telegram() {
1 TOKEN=$TOKEN
2 ID=$CHAT_ID
3 if [ "$#" -eq 2 ]; then
4 curl -s -X POST "https://api.telegram.org/bot$TOKEN/sendPhoto" -F "chat_id=$ID" -F "photo=@$2" -F "caption=$1" > /dev/null 2> /dev/null
5 else
6 curl -s -X POST "https://api.telegram.org/bot$TOKEN/sendMessage?chat_id=$ID&parse_mode=markdown&text=$1" > /dev/null 2> / dev/null
7 fi
8 }

You need to fill your personal variables as previously but now the function has different logic depending on the number of parameters: using only one, you send only a message with text. On the other hand, you can define both a text and the path to an image to send a captioned image as follows: >> notification_telegram "**Hello world!**" img.png. To send the image you need to encode it in Base64, but because curl is used, it is possible to leverage a feature and let it encode the image for us!.

Slack

In Slack, the procedure is similar. First, you need to create a new App and connect it with your workspace. Then you need to create a new webhook for the channel in which you are sending the messages. Then you can use a POST request (in this case CURL again) to send the info.

0 function notification_slack() {
1 HOOK=$SLACK_WEBHOOK
2 DATE=$(date +"%D")
3 HOUR=$(date +"%H:%M")
4 DATA="{'text': '$1', 'blocks':[{'type':'section','text':{'type':'mrkdwn','text':'$1'}},{'type': 'context', 'elements': [ {'type': 'mrkdwn', 'text': '⏱ $HOUR 🗓 $DATE'}]}]}"
5 curl -X POST -H 'Content-type: application/json' --data $DATA $HOOK > /dev/null 2> /dev/null
6 }

As previously, you can use markdown!. Sadly, you cannot send an image without URL using webhooks.