Home > Back-end >  Why does a telegram bot every time send a photo to channel as many times as the Upload Photo button
Why does a telegram bot every time send a photo to channel as many times as the Upload Photo button

Time:12-26

If you press the Upload Photo button once, the bot will send the photo 1 time. But, if u press this button more than 1 times, photo will be sent to u as many times as this button was pressed earlier. Including the number of clicks before previous photo was sent

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')
    })
}

After pressing the button and sending the photo successfully, the next photo should occur only once

CodePudding user response:

At the end of uploadPhoto() insert: bot.removeAllListeners('photo'). This should fix it.

  • Related