How to respond to "inline_query" in Telegraf.js?


node.js telegraf.js telegram Tutorials

In this post, I will show you how to respond to “inline_query” in Telegram bot using the Telegraf.js framework.

Prerequisites

Listen inline_query

To listen inline_query we need to create a simple handler:

const Telegraf = require('telegraf')
const bot = new Telegraf(process.env.BOT_TOKEN)

bot.on('inline_query', async (obj) => {
   console.log(res);
})

bot.launch()

This code will log every time when the user sends an inline query.

Get an inline query value

To get the value from the inline query we need to destructure context object. You can do this in the following way:

bot.on('inline_query', async ({ inlineQuery }) => {})

Send an answer to an inline query

To send an answer to inline_query we need to use answerInlineQuery the method. We have this method in the context object in Telegraf.js library.

bot.on('inline_query', async ({ inlineQuery, answerInlineQuery }) => {})

answerInlineQuery takes one argument - it’s a JSON array of InlineQueryResult. For example:

const Telegraf = require('telegraf')
const bot = new Telegraf(process.env.BOT_TOKEN)

bot.on('inline_query', async ({ inlineQuery, answerInlineQuery }) => {
    return answerInlineQuery({
        type: 'article',
        id: 'someID',
        title: 'someTitle',
        description: 'someDesc',
        thumb_url: 'img_url',
        url: 'url'
    })
});
comments powered by Disqus