Code Monkey home page Code Monkey logo

Comments (7)

RealPeha avatar RealPeha commented on May 24, 2024 1

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?

from telegram-keyboard.

RealPeha avatar RealPeha commented on May 24, 2024 1

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

from telegram-keyboard.

RealPeha avatar RealPeha commented on May 24, 2024 1

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 [email protected] 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()

from telegram-keyboard.

a-fatah avatar a-fatah commented on May 24, 2024

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 :)

from telegram-keyboard.

a-fatah avatar a-fatah commented on May 24, 2024

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

from telegram-keyboard.

RealPeha avatar RealPeha commented on May 24, 2024

I recommend using [email protected] because version 4 is currently unstable

install using npm

npm i [email protected] --save

or yarn

from telegram-keyboard.

a-fatah avatar a-fatah commented on May 24, 2024

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

from telegram-keyboard.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.