0

下面是代码。当我的代码第一次运行时,它就像一个魅力,但是当我再次输入 URL 并单击任何按钮时,它会显示 2 张图片(1 张来自上一个结果),第 3 次显示 3图片(2张图片来自之前的结果)等等。所以,当我再次输入 URL 时,它应该只工作一次。通过以下屏幕截图,您可以更好地理解我的问题。

代码第一次运行时

当它再次使用相同的 URL 运行时

当它以不同的 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')
        }
    }
})
4

0 回答 0