Tutorial: PoC Telegram Bot running in AWS Lambda
My favorite “SMS Alternative” app is Telegram Messenger. It’s simple, easy to use, has decent encryption (from what I understand), is very multi-platform, and has a lot of fun features. It is my sole method of communicating with some people and was recently used for a 15 person group text at Outside Lands where it worked flawlessly. Needless to say when they announced a bot platform (that was quite well thought out), I was eager to give it a try. My first thought was to write a bot in NodeJS. My second thought was “I wonder if I could do it in Lambda? Maybe the API Gateway would work for the webhooks?”. My third thought was “Let’s try it!”.
What I’ve written is a very simple “proof of concept” Telegram Bot. It uses the documented API to register itself for webhook messages. This way every time a user messages your Bot, the content gets posted (in JSON) to an AWS API gateway. That data is in turn fed to AWS Lambda which stores the data (in DynamoDB) and then responds by posting back to the sendMessage Telegram API. This is the core workflow of all Telegram Bots, the only difference in my case is I’m using “AWS Lambda” instead of “NodeJS and Express on a server”.
If you’d like to give the bot a test first, you can message it on Telegram as AWSLambdaBot. What follows is my setup instructions for replicating my PoC. I’m assuming you’ve used Lambda/DynamoDB/API Gateway a little bit in the past. If you haven’t, check out my Tutorial: AWS API Gateway to Lambda to DynamoDB as it meticulously documents every step.
Section 1 – Get the code
Either visit ShakataGaNai/poc-telegram-bot-aws-lambda on GitHub or download the full zip file. The README.md file includes a more terse version of these instructions, for those of you who know what you’re doing in AWS.
Section 2 – Register a Telegram Bot
|Step 1 – Message the BotFather
Additional details available from telegram
|Step 2 – Save the API key.
It looks something like “99999999:ASDFKEYKEYKEYASDFSAVETHEKEY”
|Step 3 – Disable group join
This is a PoC bot that only does echo. Group joins would be bad.
|Step 4 (Optional) – Set a user picture|
Section 3 – Create the Lambda Functions
Section 4 – Create a DynamoDB table
Section 5 – Create the API Gateways
Section 6 – Set Webhook & Test
While this looks really lengthy, it’s actually all quite easy. If everything goes according to plan, your bot should now respond to every message you send it by echoing back what you sent. However, it only supports text messages (that includes emoji). If you send it pictures, stickers, voices, files, or anything else – it will respond with “undefined”. You’ll also note the blue “reply quote” in the messages. This is because I chose to include the ‘reply_to_message_id’ variable which you can drop from Lambda, if you’d like.
I’ve left AWSLambdaBot online and running for now, if you’d like to test it out without doing all the work above. The README.md file on GitHub also includes a FAQ. Please feel free to fork/comment/test as you see fit. If you use this as a starter for your own bot, let me know because I’d love to see what comes out of this PoC.