首页 > 解决方案 > Telegraf:等待用户回复并立即使用

问题描述

我希望我的机器人在输入特定命令后等待用户的消息,然后像这样使用它:


const { Telegraf } = require('telegraf')
const app = new Telegraf(process.env.BOT_TOKEN);

app.command('request', async(ctx) => {
   ctx.reply("Hi user, what is your request?");
   //Wait for the user response and use it
   ctx.reply("Your answer was: ${user_answer}");
});

标签: javascriptnode.jsasync-awaittelegramtelegraf

解决方案


您需要像下面的代码一样使用 callback_query。

   app.on('callback_query', async (ctx) => {
    ctx.reply(`Your answer was: ${ctx.update.callback_query.data}`);
   })

推荐阅读