Telegram Bot with AWS Lambda: part 1
Introduction
During last time I launched different Telegram Bots, to solve my problems, for example Habit Bot (to control my habits) and I recognized that I’m solving similar problems from bot to bot. Futhermore when I started develop bots in Go, I recognized that there is no good tutorial about bot building in Go. Thus I decided to explain how I’m building Telegram Bots with Go and AWS Lambda.
Problem definition
To make this post more interesting, let’s create not just “echo bot”, but bot which solves real problem, for example:
Bot should send new Hacker News posts every morning at 7am EST and bot shouldn’t costs money for a deployment.
Problem decomposition
For solve our problem bot should:
- Read Hacker News posts.
- Automatically triggers at 7am EST and send Hacker News posts.
- Bot should register new users to send news.
- Bot should be deployed at AWS Lambda, because free tier covers all bot cases, thus we will not pay money for it.
Solution
- Telegram bot created with Bot Father registers webhook for which Telegram will send updates
- Go code deployed as AWS Lambda and handles updates from Telegram
Architecture
- Telegram servers will send updates throughout WebHook, which will launch a bot
- Throughout this channel of communication bot will register new users for receive a news
- AWS Event Bridge will send scheduled event every day at 7am EST to send news
- Bot will send news to all registered users
- Bot will be deployed as AWS lambda and it will be launched on every trigger event: Telegram update or scheduled event
- DynamoDB will store information about registered users
Key decisions
- Use AWS Lambda - this AWS service will results in free bot deployment or very cheap costs per month
- DynamoDB - for hobby project it will be free per month, thus we can use it for or hobby projects. If we will use some RDS, it will costs us at least 10$ per month, while DynamoDB will be a free option.
- AWS Event Bridge - bot should be triggered once per day at 7am EST, so there is no another option except external scheduler, because
in memory scheduler
will make our lambda decision useless and we will have a bot with costs at least 10$ per month for cheap EC2 machine.
Business flows
User registration flow
- User sends Telegram Bot command
/start
in HTTP request throughout web hook - AWS Lambda starts Bot code
- AWS Lambda wraps HTTP request in AWS event
- AWS Lambda sends event with
/start
command to bot - Bot retrieves information about current
chat_id
which bot will use for sending news - Bot saves chat id to DynamoDB
- DynamoDB returns success response
- Bot returns success response for user
Send news to users
- AWS Event Bridge sends scheduled event
- AWS Lambda starts bot
- AWS Lambda sends scheduled event to started bot
- Bot requests latest news from HackerNews
- HackerNews returns latest news
- Bot requests all users from DynamoDB
- DynamoDB returns list of users to the bot
- Bot sends notification with news for first user
- Bot sends notification with news for second user and sends this notification for all next users
- Bot finishes sending notifications
Resources
- Telegram Bot API
- Getting started with Lambda
- Tutorial: Schedule AWS Lambda functions using EventBridge
- HackerNews API
Conclusions
In this part I explained a problem statement, architecture and business flows for problem solution. In next part I will show a code in Go and explain how to configure AWS for this bot.