RealPeha / telegram-keyboard

Simple and powerful keyboard builder for Telegram Bots
https://www.npmjs.com/package/telegram-keyboard
127 stars 13 forks source link

Unhandled error while processing update #13

Closed a-fatah closed 3 years ago

a-fatah commented 3 years ago

I am following simple keyboard given here: https://github.com/RealPeha/telegram-keyboard/blob/master/examples/telegraf/simple.js

I am deploying my bot on AWS lamda using serverless framework that's why I am using telegraf-aws module.

The webhook gets triggered when I send message to the bot but it ends up in an error.

The code looks very simple but still not working. Here is my code:

"use strict";

const token = process.env.BOT_TOKEN;

if (!token) {
  throw new Error("BOT_TOKEN must be provided!");
}

const { Telegraf } = require("telegraf");

const telegrafAWS = require("telegraf-aws");

const bot = new Telegraf(token, {
  telegram: {
    webhookReply: true,
  },
});

const handler = telegrafAWS(bot, {
  timeout: 3000,
});

const { Keyboard } = require("telegram-keyboard");

const mainMenuKeyboard = Keyboard.make([
  ["Main menu", "Inline Menu"],
  ["Help"],
]).reply();

bot.start(({ reply }) => {
  return reply("Simple Keyboard", mainMenuKeyboard);
});

module.exports = { handler };

And here is the error

2021-01-26 17:56:17.857 (+05:00)    ba407db1-3a49-4553-b3ff-1afb6dd8fcd1    ERROR   Unhandled error while processing {
  update_id: 158445257,
  message: {
    message_id: 150,
    from: {
      id: 935285759,
      is_bot: false,
      first_name: 'Abdul',
      last_name: 'Fatah',
      username: 'abdulfatah66',
      language_code: 'en'
    },
    chat: {
      id: 935285759,
      first_name: 'Abdul',
      last_name: 'Fatah',
      username: 'abdulfatah66',
      type: 'private'
    },
    date: 1611665763,
    text: '/start',
    entities: [ [Object] ]
  }
}

Please help. Thanks

RealPeha commented 3 years ago

Hi @a-fatah

This doesn't seem to be a problem with telegram-keyboard. If you do console.log(mainMenuKeyboard) you will see that reply_markup looks good. Does this error appear if you remove the use of telegram-keyboard? And what version of Telegraf?

a-fatah commented 3 years ago

Thanks @RealPeha

I figured it out. It wasn't really a problem with telegram-keyboard. I am new in nodejs world so I think I made a funny mistake. Anyway I changed my code a little bit and it worked :)

Before

bot.start(({ reply }) => {
  return reply("Simple Keyboard", mainMenuKeyboard);
});

After

bot.start(async (ctx) => {
  await ctx.reply("Simple Keyboard", mainMenuKeyboard);
})

I would appreciate if you educate me what makes it different from the previous one.

PS I am a java developer who just started learning nodejs :)

a-fatah commented 3 years ago

I just copy pasted the code from examples in this repository. I think you might want to update that file as well.

RealPeha commented 3 years ago

I could explain the reason if I wrote better in English 😄 But now I see that you are using telegraf@4.x version. So, in telegraf@4.x you cannot be destructured methods from context (you must always write like this: (ctx) => ctx.reply('some'). My examples written for telegraf@3.x where you can write as you like, for example ({ reply }) => reply('some')

RealPeha commented 3 years ago

I recommend using telegraf@3.38.0 because version 4 is currently unstable

install using npm

npm i telegraf@3.38.0 --save

or yarn

yarn add telegraf@3.38.0
a-fatah commented 3 years ago

Thank you so much for pointing me to right direction :) But I can't understand why we can't use destructing in latest version ?

RealPeha commented 3 years ago

When destructuring a method from the class, its execution context is lost and this is no longer referenced to the current instance. And in this line https://github.com/telegraf/telegraf/blob/develop/src/context.ts#L264 this is equal undefined

In telegraf@3.38.0 it was solved using bind: https://github.com/telegraf/telegraf/blob/3.38.0/context.js#L68

You can play around with this small example to figure out what's the matter

class Context {
    reply() {
        console.log('this', this)
    }
}

const ctx = new Context()

// 1
ctx.reply()

// 2
const { reply } = ctx
reply()

// 3
ctx.reply = ctx.reply.bind(ctx)
const { reply } = ctx
reply()