Telegram Notification Setup for AWS SNS Message/Alarm
3 min readApr 3, 2024
Purpose :
Need to make the SNS message or cloud watch alarm from SNS to send to telegram group or messages using telegram bot.
Services Used :
- AWS SNS
- AWS Lambda
- Telegram Bot
- Python
Workflow :
- Once we create the telegram bot, we will have the token and chat ID for the bot, and telegram is given the rest API to send a message to the bot.
- So in this process, we need to set up one lambda which will call the telegram bot api to send the messages which is collected from sns.
- For that we need add this lambda in the sns subscription so that whenever new messages come, it will trigger the lambda.
Procedure :
1) CREATE TELEGRAM BOT :
- In the Telegram app, type and search for @BotFather. Next, press the Start button (alternatively, you may send the /start command). Once this is done, send the /newbot command and follow the few easy steps to create a new Telegram bot. The BotFather will generate an authorisation token for the new bot. This token is a string which resembles something like 123456789:ABCD1234efgh5678-IJKLM. This is required for sending requests to the Telegram Bot API.
- In the Telegram app, search the name of the bot that you had created. Then, press the Start button (you may also send the /start botusername command). Write down any text message to chat with your bot. For, e.g., write โHelloโ.
- Now, execute the Bot API call to retrieve the ID of your chat with the bot. In the given command, replace <token> with the value of the authorisation token that you had received from the BotFather.
curl โhttps://api.telegram.org/bot<token>/getUpdatesโ | python -m json.tool
The output of this will give your chat id. - Please refer the following sample output of the curl โhttps://api.telegram.org/bot<token>/getUpdatesโ | python -m json.tool
- curl output of get chat id telegram
{
"ok": true,
"result": [
{
"message": {
"chat": {
"first_name": "<first_name>",
"id": <Enter your ID>,
"last_name": "<last_name>",
"type": "private"
},
"date": 1661359090,
"entities": [
{
"length": 6,
"offset": 0,
"type": "bot_command"
}
],
"from": {
"first_name": "<first_name>",
"id": <Enter your ID>,
"is_bot": false,
"language_code": "en",
"last_name": "<last_name>"
},
"message_id": 3,
"text": "/start"
},
"update_id": <Update ID>
}
]
}
2) CREATE LAMBDA :
- We need to create one lambda function, which will trigger from SNS on publishing a message.
- When the lambda is triggered, it collects the message from SNS and call the telegram bot API to send a message to particular group.
- Need to have the chatbot id and API token of the telegram bot which we collected from STEP 1.
- Please refer the following sample lambda code :
3) Configure SNS :
- Now we need to go to our SNS topic which is publishing messages and alarms.
- Here we need to click Create Subscription.
- Parallelly copy the ARN from the created lambda in the new tab.
- In the Create Subscription page, fill in the details with copied lambda arn as shown below, and then click Create subscription.
- Now your aws SNS message and alarm to the telegram group is completed.
REFERENCES :
https://faun.pub/sending-cloudwatch-alarm-status-to-telegram-4599f5bf38de