Nathan Bernard
Nathan Bernard

Reputation: 71

Chaining with Telegram Bot API (like TriviaBot)

I am creating a TriviaBot style bot for telegram and am using Node.js to do so. At the moment I am having trouble capturing the users response to my quiz to determine whether the user got the question right or wrong. Below is some code:

bot.onText(/\/quiz/, function (msg) {

  var chatId = msg.chat.id;
  var text = quizdata.one.msgtxt;
  var opts = {
    reply_to_message_id: msg.message_id,
    reply_markup: JSON.stringify({
      keyboard: quizdata.one.keyboard,
      one_time_keyboard: true
    })
  };
  bot.sendMessage(chatId, text, opts);

  //NEED TO CAPTURE THE USER RESPONSE AND REPLY TO THEIR MESSAGE ACCORDINGLY

});

Upvotes: 1

Views: 877

Answers (1)

Agiel
Agiel

Reputation: 46

NOTE : Telegram would cut of any asynchronous function, you should make separated module for listening any incoming interaction with button. You could use global Array for to store small data to be able getting returned for other module you need.

Putting all of your command in the index js not good idea tho.

if you want to listen the from keyboard callback_data. Just create a new line to listen any incoming clicked button.

bot.on("callback_query", (msg) => {
  if (msg.data === "your_keyboard_callback_data") {
    // do whatever you want
  }
})

For more clearance node telegram api

Sorry if my answer is too late for this but hope mine can help other people 🙂

Upvotes: 0

Related Questions