This project demonstrates the barebones code and infrastructure needed to create a basic serverless, event-driven, Telegram echo bot on AWS.
By using Telegram’s webhook approach in a serverless environment, you benefit from an event-driven model, meaning the bot only runs when a new message is 'pushed' from telegram. And there is no need to keep polling the telegram API waiting for new messages. This reduces costs by charging only for actual invocations (rather than a continuously running server).
This project utilises Telegram’s webhook functionality to trigger a single AWS Lambda function using its Function URL. Whenever a user sends a message to the bot, Telegram delivers the update to that URL. The Lambda function then uses the Telegram API to send a response back to the sender. The Terraform configuration sets up all required AWS resources (permissions, roles, and the Lambda) and configures the Telegram webhook to point to the Function URL automatically after every deployment using Telegrams's API.
This project was built on Ubuntu Linux and should work on most modern Linux distributions or macOS without changes. On Windows, you can use the Windows Subsystem for Linux (WSL) for a smoother experience. If you prefer running everything natively on Windows, be aware you may need to make a few minor adjustments.
You will need to setup a telegram bot and obtain its bot token.
An authenticated AWS CLI session is required to build AWS resources using terraform. To download AWS CLI and login, follow the tutorials linked.
This project was built using python 3.10. However it should work with most modern versions of python 3. If you use a different version, ensure to edit the lambda resource in terraform/main.tf to match the version you're using to avoid issues.
Tutorial on how to install python
Clone the repo
git clone https://github.com/SLClayton/telegram-echo-bot.git
Go to the terraform directory
cd telegram-echo-bot/terraform
Initialise the terraform project
terraform init
Apply the terraform project, it will ask you to input your bot token, and then ask you to confirm the deployment by typing 'yes'.
terraform apply
You can alternativley setup a .tfvars file to manage the variables and input the telegram token there before applying with the file as an argument. For example, if you create a file named myvars.tfvars. The you would use.
terraform apply -var-file=myvars.tfvars
Add your bot on telegram using the username you created with the Botfather, and send it a message.
The destroy command will remove the whole project.
terraform destroy
