Telegraf with firebase realtime database not working in AWS Lambda

Viewed 296

Context

  • Telegraf.js Version: 3.36
  • Node.js Version: 12.16.1

Expected Behavior

Bot in AWS Lambda with firebase and telegraf should send the value of the realtime database only once when asked.

Current Behavior

The bot is sending the value from the firebase realtime database indefinitely without stopping.

Steps to Reproduce

  1. Create Lambda function (ZIP node_modules and all necessary, files like index.js) and set all environment variables. 1.2 index.js must be:
require('dotenv').config();
const Telegraf = require('telegraf');
const bot = new Telegraf(process.env.TOKEN);

const firebase = require('firebase');

const app = firebase.initializeApp({
  apiKey: process.env.APIKEY,
  authDomain: process.env.AUTHDOMAIN,
  databaseURL: process.env.DATABASEURL,
  projectId: process.env.PROJECTID,
  storageBucket: process.env.STORAGEBUCKET,
  messagingSenderId: process.env.MESSAGINGSENDERID,
  appId: process.env.APPID
});

bot.command('value', (ctx) => {
  const rootRef = firebase.database().ref();
  let ordersRef = rootRef.child("last");

  ordersRef.once('value', function (dataSnapshot) {
    let data = dataSnapshot.val();
    ctx.reply(data);
  })
})

exports.handler = (event, context, callback) => {
  const tmp = JSON.parse(event.body); // get data passed to us
  bot.handleUpdate(tmp); // make Telegraf process that data
  return callback(null, { // return something for webhook, so it doesn't try to send same stuff again
    statusCode: 200,
    body: '',
  });
};
  1. Create API Gateway -> Add method POST -> Lambda Function -> Deploy -> Copy invoke url
  2. Set invoke URL as webhook

Maybe the return value of the handler is not being sended to Telegram Servers so they keep sending the same message?

I have opened an issue in the GitHub repository of Telegraf, but they haven't found what's wrong either.


Update

I notice something.While this code works just fine:

const Telegraf = require('telegraf');
require('dotenv').config();
const bot = new Telegraf(process.env.TOKEN);

const firebase = require('firebase');
const app = firebase.initializeApp({
  apiKey: process.env.APIKEY,
  authDomain: process.env.AUTHDOMAIN,
  databaseURL: process.env.DATABASEURL,
  projectId: process.env.PROJECTID,
  storageBucket: process.env.STORAGEBUCKET,
  messagingSenderId: process.env.MESSAGINGSENDERID,
  appId: process.env.APPID
});

bot.command('value', (ctx) => {
//  const rootRef = firebase.database().ref();
  ctx.reply("Hello")
})

exports.handler = (event, context, callback) => {
  const tmp = JSON.parse(event.body);
  bot.handleUpdate(tmp);
  return callback(null, {
    statusCode: 200,
    body: '',
  });
};

Just by uncommenting the reference, it starts failing, replying "Hello" a lot if times again.

The WebhookInfo is saying "last_error_message": "Wrong response from the webhook: 502 Bad Gateway".

0 Answers
Related