内联键盘点击不会调用callback_query,为什么?

问题描述 投票:0回答:2
bot.onText(/(.+)$/, function (msg, match) {
    const opts = {
    reply_markup: {
      inline_keyboard: [
        [
          {
            text: 'Edit Text',
            callback_data: 'edit'
          }
        ]
      ]
    }
  };
  bot.sendMessage(msg.from.id, 'Original Text', opts);
});

bot.on("callback_query", function(callbackQuery) {
    // 'callbackQuery' is of type CallbackQuery
    console.log(callbackQuery);
});

我一直在寻找这个问题的答案,尝试了call_back上所有可用的资源。例如 通过 Node.JS 的 Telegram 机器人内联键盘 Telegram 内联键盘和键盘 如何在机器人之父中为电报机器人创建菜单?

javascript telegram-bot
2个回答
0
投票

这样尝试一下怎么样?

bot.on('callback_query', function onCallbackQuery(callbackQuery) {
        const action = callbackQuery.data;
        const msg = callbackQuery.message;
        // do your stuff
        if (action === 'adress') {
        // do something if callback data is "adress", you can have multiple if statements for various cases
        }
});

这就是我的工作原理,希望对你有帮助!


0
投票

我也有同样的问题。这似乎是一个持续存在的错误,与开发文档有关。让我们看看这个问题是老问题并且与语言或代码版本无关。有无数报告称人们遇到同样的问题。

解决方案之一是必须将内联键盘的返回视为文本。对于某些人来说有效,而对于另一些人则无效。

我花了 2 天尝试解决这个问题,直到以下代码对我有用:

const TelegramBot = require('node-telegram-bot-api');
const texts = require('./langs/en');

const token = 'xxxx';
const bot = new TelegramBot(token, { polling: true });

// User state mapping
const userStates = {};

const networkSelectionMessage = 'Select item:';
const networkButtons = [
    [{ text: 'Item 1', callback_data: 'item1' }],
    [{ text: 'Item 2', callback_data: 'item2' }],
    [{ text: 'Item 3', callback_data: 'item3' }],
];

// Handler para o comando /start
bot.onText(/\/start/, (msg) => {
    const chatId = msg.chat.id;
    console.log('Received /start command from user:', chatId);

    userStates[chatId] = 'register';
    bot.sendMessage(chatId, 'Clicked: /start', {
        reply_markup: {
            inline_keyboard: [
                [{ text: '-->', callback_data: 'register' }]
            ]
        }
    });
});


// Handler for callback buttons
bot.on('callback_query', (callbackQuery) => {
    const chatId = callbackQuery.message.chat.id;
    const action = callbackQuery.data;
    console.log('Received callback query:', action, 'from user:', chatId);

    switch (action) {
        case 'register':

            userStates[chatId] = 'network';
            bot.sendMessage(chatId, networkSelectionMessage, {
                reply_markup: {
                    inline_keyboard: networkButtons
                }
            });
            break;
        default:
            if (userStates[chatId] === 'network') {
                bot.sendMessage(chatId, `Selected "${action}"`);
                // Update user state
                userStates[chatId] = 'contract';
            }
            break;
    }
});


bot.on('message', (msg) => {
    const chatId = msg.chat.id;
    const currentState = userStates[chatId];

    // Checks if the user is in contract state
    if (currentState === 'contract') {
        const varTest = msg.text;
        console.log('Received:', chatId, ':', varTest);

        bot.sendMessage(chatId, `"${varTest}"`);
        
        delete userStates[chatId];
    } else {
         // Process the text message normally
         // Here you can add logic to process other text messages
    }
});

// Handler for text editing callback
bot.on("callback_query", function(callbackQuery) {
    console.log(callbackQuery);
});
© www.soinside.com 2019 - 2024. All rights reserved.