微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。