I'm using telegraf library to create a Telegram bot that would send gif messages upon request. I use inline keyboard for this and when i press an option of gif message to be received i need to await API response, receive 2-3 gifs and show inline keybord again for next choice. The problem is that the inline keyboard is shown right after the option choice and it is not waiting for all the response messages. Tried to use call back functions and settimeout but the first option didn't work, the second option is not good because you never know how long to wait for the messages to be loaded on a device. Any suggestions?
bot.start(ctx => sendOptionsKeyboard(ctx, bot, 'What shall I send you today? ' ))
bot.action('dog', ctx => showSpecificGif(ctx, bot))
bot.action('cat', ctx => showSpecificGif(ctx, bot))
const sendOptionsKeyboard = (ctx, bot, questionMessage) => {
bot.telegram.sendMessage(ctx.chat.id, questionMessage, {
reply_markup: {
inline_keyboard: [
[
{ text: 'show dogs', callback_data: 'dog' },
{ text: 'show cats', callback_data: 'cat' },
]
],
},
})
}
const showSpecificGif = async (ctx, bot) => {
try {
ctx.reply('Loading...')
const offset = Math.floor(Math.random() * 4500)
const res = await axios.get(
`https://api.giphy.com/v1/gifs/search?api_key=${process.env.GIPHY_API_KEY}&q=${ctx.update.callback_query.data}&limit=3&offset=${offset}&rating=g&lang=en`
)
const gifArray = res.data.data
gifArray.forEach(gif => ctx.replyWithVideo(gif.images.downsized_medium.url))
} catch (err) {
ctx.reply('You got error')
}
sendOptionsKeyboard(ctx, bot, 'Send again? ')
}