NodeJS 为什么电报机器人每次发送照片到频道的次数与按下上传照片按钮的次数一样多?

fcg9iug3  于 2022-12-29  发布在  Node.js
关注(0)|答案(1)|浏览(99)

如果你按下上传照片按钮一次,机器人将发送照片1次。但是,如果你按下这个按钮超过1次,照片将发送给你多次按下这个按钮之前。包括点击的次数之前,以前的照片被发送

const keyboard = {
    reply_markup:{
        keyboard: [
            [{text: 'About Bot'}, {text: 'Upload Photo'}]
        ]
    }
}

bot.onText(/\/start/, async msg => {
    await bot.sendMessage(h.id(msg), `Choose any`,keyboard)
})

bot.on('message', msg => {
    switch (msg.text){
        case 'Upload Photo':
            uploadPhoto(h.id(msg))
            break
        case 'About Bot':
            break
    }
})

const uploadPhoto = async (chatId) => {
    await bot.sendMessage(chatId, 'Send me a photo')
    bot.on('photo', async (msg) => {
        const photo = msg.photo[2].file_id
        await bot.sendPhoto(channelId, photo)
        await bot.sendMessage(chatId, 'Photo is sent')
    })
}

按下按钮并成功发送照片后,下一张照片应仅出现一次

esyap4oy

esyap4oy1#

uploadPhoto()末尾插入:bot.removeAllListeners('photo')。这应该可以修复它。

相关问题