How did I create the Telegram Bot
Why do I need the bot?
Well, if you are asking this question – probably you don't need a bot. In general, bots are great. I needed a way to get fast and free operational notifications to my cellphone. I could achieve it in several ways, the simplest one - was to get AWS SNS notification by e-mail. SNS is a great tool, but this is what you get out of the box:
Looks not so evident, and you need time to understand what is happening. I was too lazy to change anything in this, until COVID-19 made all of us stay at home. So during a weekend at home I thought that it would be greate to get fast notifications, instead of messy e-mails from AWS.
As an addition I wanted to manage some actions (like CDN invalidation) with simple commands from Telegram.
The design.
Telegram has API, where you can manage your bot with simple queries. To send a message you need only one GET request.
CodePipeline notifications going to SNS, that is using Lambda subscription. Lambda extracts the needed data from notification and sends the message to Telegram bot with HTTP call.
So receiving notifications is simple. On the other hand, I need to send back some commands – for example to invalidate the CDN paths (fully or partially).
For this task there is a possibility in Telegram to send web hooks, each time anything is posted to the chat. The webhook will be sent to API-Gateway, Lambda function will be used as a backend.
Lets implement!
First, you need to create a bot. Telegram has botfather for this. The first step is to create your own bot: send the following to botfather
Botfather will do everything for you in an easy and interactive way. As an output you will get the bot token. You need to save it securely.
Now it's high time to send the first message to your bot. Just type anything and send it to the bot. You need an existing chat for the next steps.
You need your ${CHAT_ID}
to send messages. To get the ${CHAT_ID}
you might use getUpdates API:
You need the chat entity from the output. Inside chat object, there is an id, that we need. Now we are ready to send our first message to the bot:
Magic happened! You got the message!
Now we need to implement the 2nd part of the magic – prepare the lambda function, that will notify us on changes in our pipeline. The function should be triggered by SNS topic, all required permissions should be set in IAM role.
=
=
= + + + + +
=
=
=
= + +
= +
=
As you probably noticed, telegram gets the Markdown syntax to format the messages.
One more part that should be implemented is the commands, that I would like to send to my AWS Account. There are different commands for different cases: help, clear CDN cache.
=
=
=
=
=
=
# current date and time
=
=
=
= + + + + +
=
= 0
=
= + + + + + + + +
+= 1
=
return
Now, if the message will contain one of the commands (defined in cmd_list
) – the relevant function will be called.
So, the /help command will return the help screen:
The other commands will call appropriate function.
And the final step is to secure your deployed API-Gateway. As simplest solution – the source IP addresses will be locked to Telegram API Sources (can be gathered here). The IP restriction can be set on API-Gateway with proper resource policy:
That's it! Enjoy!