Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

219
Views
send inline keyboard after all reply messages telegram/telegraf

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? ๐Ÿ˜€')
}
about 4 years ago ยท Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!