So I have this simple telegraf bot that works when I run it locally with node. I also managed to create some simple Lambda functions, but I can’t figure out how to actually run the bot on the lambda. I tried this:
const { Telegraf } = require("telegraf");
exports.handler = async (event) => {
const bot = new Telegraf(<token goes here>);
bot.start((ctx) => ctx.reply("๐"));
bot.launch();
const response = {
statusCode: 200,
body: JSON.stringify('OK'),
};
return response;
};
But I’m sure this is not the way it is supposed to be implemented
2
Answers
I’m not that familiar with telegraf, however, if you must have it on Lambda function, you can add its package as Lambda layer.
See what Lambda layer and how to leverage it in the following aws docs.
By the way, like everyone mentioned above in the comment section, Lambda has limited resource such 15 minutes execution time as well as Lambda is one of expensive AWS resources compare to EC2.
Also if telegraf is a heavy package or its process takes longer than 15 minutes, the Lambda function will be failed and you will have to configure DLQ.
Maybe for these reasons, people don’t recommend you to run telegraf on Lambda function.
Idealy, you can find more architecture using Fargate or EC2.
You should use
bot.handleUpdate(update)
but don’t forget to parse message before, because it could be a string, and even more โ base64 encoded string, e.g.: