【问题标题】:Waiting for a message response using Telegraf/Node.js使用 Telegraf/Node.js 等待消息响应
【发布时间】:2020-08-28 05:39:57
【问题描述】:

我想等待用户发送消息,然后将新消息分配给变量。

如何在 Node.js 上使用 Telegraf 等待消息回复?

const Telegraph = require("telegraf");
const APIKEY = 'LADLADJLDAKJLAK;LAKS;LSAKLDA,MDALJDALJLDAJLDAJLDAJDALDAJLADJ'
const bot = new Telegraph(APIKEY);
const loginMessage = `
PLEASE ENTER YOUR EMAIL:
`;
bot.command("login", (ctx) => {
    ctx.reply("You Entered Login");
    ctx.reply(loginMessage);//login message:Please Enter EMAIL
    //wait for user to send message
    //ASSIGN MESSAGE TO VARIABLE
  });

bot.launch();

【问题讨论】:

    标签: javascript node.js telegram telegram-bot telegraf


    【解决方案1】:

    您应该考虑使用Scenes。但不仅仅是一个普通的场景,而是使用 WizardScene 更好地使用,特别是因为您希望您的机器人与用户进行类似采访以获取他们的信息。要存储用户数据,请使用Session。开箱即用,它将数据存储在内存中,我认为即使在重新启动后也能够保留您的数据,请使用 telegraf-session

    看看这个教程,它帮了我很多How to build OCR Bot

    【讨论】:

      【解决方案2】:

      有几种方法:

      1. 以任何方式(session middleware、object、db)存储带有状态的 chatID,例如 "123" : "login_step",并首先检查用户的状态。
      2. 使用ForceReply:添加到sendMessage的reply_markup。并检查reply_to_message 以获取您的命令

      【讨论】:

      • **请您在回答中强调一下谢谢@TAB_mk
      • 存储时您可以处理短信并检查用户是否在“登录”步骤',例如:bot.on('text', async (ctx) => { const step = /*get step from storage*/; if (step === 'login_step') { login(ctx.update.message.text) }})。与回复相同。从ctx.message.update.text 登录并从ctx.message.reply_to_message @AlwiMuhammad 步进
      猜你喜欢
      • 1970-01-01
      • 2021-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-21
      • 2015-12-06
      • 1970-01-01
      相关资源
      最近更新 更多