检查功能不等待响应

问题描述 投票:0回答:1

我正在尝试创建一个电报机器人,此功能无法按预期功能工作,功能检查用户是否是新用户,如果是,则询问姓名和年龄,它应该等到用户输入姓名然后继续,但它没有't

async function check(chat_id) {
  const firstText =
    "Hi there! Before you start using the bot, you need to enter the following details: name, age";
  const res = await Subscription.findOne({ chatId: chat_id });

  if (!res) {
    let name;
    let age;

    await bot.sendMessage(chat_id, firstText);
    await bot.sendMessage(chat_id, "Name?");

    await bot.once("message", async (message) => {
      name = message.text.trim().toLowerCase();
    });

    await bot.sendMessage(chat_id, "Age?");
    bot.once("message", async (message) => {
      age = message.text.trim().toLowerCase();
    });

    try {
      // Create a new subscription document in the database
      await Subscription.create({
        chatId: chat_id,
        name: name,
        age: age,
      });

      // Now you can use the captured name and age for further processing
      await bot.sendMessage(
        chat_id,
        "Thanks for providing your details. You are now registered!"
      );
    } catch (error) {
      console.error("Error inserting data:", error.message);
      await bot.sendMessage(chat_id, "Error occurred. Please try again later.");
    }
  } else return;
}
javascript node.js telegram-bot
1个回答
0
投票

似乎每当用户输入内容时就会触发您的

once
事件。 首先是名字,然后是年龄。收到这两个数据后,您需要尝试创建订阅。

我希望下面的代码对你有用 -

async function check(chat_id) {
    const firstText =
      "Hi there! Before you start using the bot, you need to enter the following details: name, age";
    const res = await Subscription.findOne({ chatId: chat_id });
  
    if (!res) {
      let name;
      let age;
  
      await bot.sendMessage(chat_id, firstText);
      await bot.sendMessage(chat_id, "Name?");
    
      let msgCounter = 0;
      bot.once("message", async (message) => {
        if (msgCounter === 0) {
            name = message.text.trim().toLowerCase();
        } else if (msgCounter === 1) {
            age = message.text.trim().toLowerCase();
        } else {
            // Handle Unexpected case
        }
        msgCounter++;
        if (msgCounter === 1) {
            await bot.sendMessage(chat_id, "Age?");
        } else {
            // Both received
            await doSomething(chat_id, name, age);
        }

      });
    } else return;
  }


async function doSomething(chat_id, name, age) {
    try {
        // Create a new subscription document in the database
        await Subscription.create({
          chatId: chat_id,
          name: name,
          age: age,
        });
  
        // Now you can use the captured name and age for further processing
        await bot.sendMessage(
          chat_id,
          "Thanks for providing your details. You are now registered!"
        );
      } catch (error) {
        console.error("Error inserting data:", error.message);
        await bot.sendMessage(chat_id, "Error occurred. Please try again later.");
      }
}
© www.soinside.com 2019 - 2024. All rights reserved.