node-telegram-bot-apicallback_query 不起作用

问题描述 投票:0回答:1

我决定尝试在 NodeJS 中编写一个机器人,我已经到了处理回调查询的地步,但是当我单击内联键盘时,机器人没有捕获此事件。

模块版本:

"node-telegram-bot-api": "^0.66.0"
import TelegramBot from "node-telegram-bot-api";
import { config } from "dotenv";

config()
const API_KEY_BOT = process.env.TOKEN;

const bot = new TelegramBot(API_KEY_BOT, { polling: true })


const gameOption = {
  reply_markup: {
    inline_keyboard: [
      [{text: '7', callback_data:'7'},{text: '8', callback_data:'8'},{text: '9', callback_data:'9'}],
      [{text: '4', callback_data:'4'},{text: '5', callback_data:'5'},{text: '6', callback_data:'6'}],
      [{text: '1', callback_data:'1'},{text: '2', callback_data:'2'},{text: '3', callback_data:'3'}],
      [{text: '0', callback_data:'0'}]
    ]
  }
}
function start() {
  const chats ={}
  bot.setMyCommands([
    { command: '/start', description: 'Running a bot' },
    { command: '/game', description: 'Game' }
  ])

  bot.on('message', async msg => {
    const text = msg.text;
    const chatId = msg.chat.id;
   
    if (text === '/start') {
      return await bot.sendMessage(chatId, `You wrote to me ${text}`)
    }
    if (text === '/game') {
      await bot.sendMessage(chatId, 'Now I will guess a number from 0-9')
      const randomNumber = Math.floor(Math.random()*10);
      chats[chatId] = randomNumber;
      console.log(gameOption);
      
      return await bot.sendMessage(chatId,"Guess", gameOption)
    }

    return await bot.sendMessage(chatId, 'I dont understand you')
  })

  bot.on('callback_query', function onCallbackQuery(callbackQuery) {
    console.log(callbackQuery);
    
  });
}
start()
telegram-bot node-telegram-bot-api
1个回答
1
投票

我也尝试过,效果很好。

  bot.on('callback_query', function onCallbackQuery(callbackQuery) {
    console.log(callbackQuery);
    bot.answerCallbackQuery(callbackQuery.id, {
        text: "It's working"
    })
  });

也许您还应该尝试通过在机器人构造函数中的 allowed_updates 参数上指定来显式请求

callback_query

const bot = new TelegramBot(API_KEY_BOT, {
  polling: {
    params: {
      allowed_updates: ["message", "callback_query"], // any other update types
    }
  }
})

我可以建议的另一件事是,只需尝试撤销您的 Bot 令牌并使用新的令牌即可。

希望这有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.