callback_query在嵌入式键盘中多次工作

问题描述

下面是代码。 当我的代码第一次运行时,它像一个超级按钮一样工作,但是当我再次输入URL并单击任何按钮时,它显示2张图片(其中1张来自上一个结果),第三次显示3张图片图片(2张图片来自之前的结果),依此类推。 因此,当我再次输入URL时,它应该只能运行一次。 使用以下屏幕截图,您可以更好地了解我的问题。

When codes run for the first time

When it runs again with the same URL

When it runs for 3rd time with different URL

因此,要解决此问题,最好的选择是什么

require('dotenv').config();
const TelegramBot = require('node-telegram-bot-api');
const fetch = require("node-fetch");
const token = process.env.TOKEN;

const bot = new TelegramBot(token,{
    polling: true
})
bot.onText(/\/yt/,async (message,match) => {       //ontext takes regex
    if (match.input === "/yt") {
        await bot.sendMessage(message.chat.id,"No input")
    } else {
        const url = match.input.split(' ')[1];
        if (url.match(/v=(\w|-|_)+/g)) {
            var idOfVid = url.match(/v=(\w|-|_)+/g);
            idOfVid = idOfVid[0].slice(2);
            fetch(`${accessYoutubeApi}${idOfVid}`)
                .then(res => res.json())
                .then(async (data) => {
                    titleOfVideo = data.items[0].snippet.title;
                    channelOfVideo = data.items[0].snippet.channelTitle;
                    if (url != undefined) {
                        if (/(www.youtube.com)/gm.test(url)) {
                            await bot.sendMessage(message.chat.id,`${titleOfVideo}
${channelOfVideo}
Select the photo size :`,{
                                reply_markup: {
                                    inline_keyboard: [
                                        [{
                                                text: "120x90",callback_data: "default"
                                            },{
                                                text: "320x180",callback_data: 'medium'
                                            },{
                                                text: "480x360",callback_data: 'high'
                                            },{
                                                text: "640x480",callback_data: 'standard'
                                            },{
                                                text: "1280x720",callback_data: 'maxres'
                                            }
                                        ]
                                    ]
                                }
                            })


                            bot.on("callback_query",function callback(callBackQuery) {
                                callBackData = callBackQuery.data;
                                thumbnails = data.items[0].snippet.thumbnails;
                                if (callBackData == "default") {
                                    bot.sendPhoto(message.chat.id,thumbnails.default.url,{
                                        caption: "120x90"
                                    });
                                } else if (callBackData == "medium") {
                                    bot.sendPhoto(message.chat.id,thumbnails.medium.url,{
                                        caption: "320x180"
                                    })
                                } else if (callBackData == "high") {
                                    bot.sendPhoto(message.chat.id,thumbnails.high.url,{
                                        caption: "480x360"
                                    })
                                } else if (callBackData == 'standard') {
                                    bot.sendPhoto(message.chat.id,thumbnails.standard.url,{
                                        caption: "640x480"
                                    })
                                } else if (callBackData == 'maxres') {
                                    bot.sendPhoto(message.chat.id,thumbnails.maxres.url,{
                                        caption: "1280x720"
                                    })
                                }
                                /*bot.answerCallbackQuery(callBackQuery.id)
                                    .then((e) => {
                                        if (callBackData.command == idOfVid) {
                                            bot.sendMessage(message.chat.id,"Wait for few seconds,may take some time");
                                            bot.sendPhoto(message.chat.id,thumbnails[callBackData].url)

                                        }
                                    })*/
                            })
                        } else {
                            bot.sendMessage(message.chat.id,`Invalid URL
If you are providing the URL of youtube video make sure while pasting the URL before /yt don't write any character.
If you have provided the perfect URL,please try again.`)
                        }
                    }
                })
        } else {
            bot.sendMessage(message.chat.id,'Invalid URL')
        }
    }
})

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...