Telegram node.js AWS Lambda 502 error

#1

I have succeded with my first node app on Lambda using serverless. It works great when I open it from http endpoint after executing serverless deploy -v!

My target is to get my node.js telegram bot running on lambda. When I insert bot logic into export.module.hello , deploy in console, set telegram webhook, get no response after run /start command in my bot and then check same http endpoint webhook by opening api.telegram.org/botXXX/getWebhookinfo - what I get is “Wrong response from the webhook: 502 Bad Gateway”

this is my handler.js code:

'use strict';

module.exports.hello = async (context, cb) => {
  const TelegramBot = require('node-telegram-bot-api');

  const token = "token";
  const bot = new TelegramBot(token);
  const chatId = context.body.message.chat.id;

  const out = bot.sendMessage(chatId, 'Hello World!');
  return cb(null, out);
};

and this is serverless.yml (functions part of it)

functions:
  hello:
    handler: handler.hello
    events:
      - http: POST hello

I am comfortably running node.js code for telegram from a localhost, but seems cannot wrap this code inside lambda function… Otherwise what can be a problem causing 502 error, when I check getWebhookinfo?
Any advice will be much appreciated!

0 Likes

#2

Hi, if I’m not mistaken bot.sendMessage returns a promise, you use an async function, so I think you don’t need to use cb + you need to add await + return a correct message.

  const TelegramBot = require('node-telegram-bot-api');

  const { TOKEN } = process.env;
  const bot = new TelegramBot(TOKEN); 

module.exports.hello = async (event) => {
  const chatId = event.body.message.chat.id;
  const result = await bot.sendMessage(chatId, 'Hello World!');

  return { statusCode: 200, body : JSON.stringify(result) };
};

p.s. have you tried to call this url using postman or something similar?

0 Likes