Closed a-fatah closed 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?
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 :)
I just copy pasted the code from examples in this repository. I think you might want to update that file as well.
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')
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
Thank you so much for pointing me to right direction :) But I can't understand why we can't use destructing in latest version ?
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()
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:
And here is the error
Please help. Thanks