0

I'm creating a Telegram bot and try to implement module-like architecture for it. So, for handling inline queries, I'm created that class:

class inlineVacationsHandler {
    constructor(bot){
        this.bot = bot;
        this.bot.on('inline_query',this.handler);
    }


    handler(msg){
       console.log(this.bot);
    }
}


var bot = new TelegramBot(config.token_dev, {polling: true});

var inline = new inlineVacationsHandler(bot);

But problem is, that I can't use class field "bot" in eventHandler, because in this case "this" don't point to inlineVacationsHandler class. So, how I can get "bot" object from hadler function? Thanks. I'm using Node.js and ES2016.

Pter
  • 163
  • 1
  • 2
  • 7

1 Answers1

1
class inlineVacationsHandler {
    constructor(bot){
        this.bot = bot;
        this.bot.on('inline_query',this.handler.bind(this));
    }


    handler(msg){
       console.log(this.bot);
    }
}


var bot = new TelegramBot(config.token_dev, {polling: true});

var inline = new inlineVacationsHandler(bot);

Use this.bot.on('inline_query',this.handler.bind(this));

Piyush.kapoor
  • 6,715
  • 1
  • 21
  • 21