通过“node-telegram-bot-api”模块接收用户消息

Receive user Messages via `node-telegram-bot-api` module

为什么此代码在任何地方或我的意思是在任何按钮中获取用户消息。

使用此代码,我将接收用户消息,我排除了 buttonsprevious buttons 因为我只需要控制台日志用户真实消息(不是按钮标题), 我需要在 Contact us 按钮时获取用户消息。

bot.on('message', (msg) => {
  if (!locale.keyboards[msg.text] &&
    !msg.text.includes("Previous Buttons ")) {
    console.log("User Message is: " + msg.text)
    const opts = {
      reply_to_message_id: msg.message_id,
      reply_markup: JSON.stringify({
        keyboard: arrangeKeyboardWithOut(locale.keyboards["/start"].childs, 2, msg),
        resize_keyboard: true,
        one_time_keyboard: true
      })
    };
    bot.sendMessage(msg.chat.id, 'Received your message', opts);
  }
});

首先我做了这两个函数

var getmessage = async () => {
  // Listen for any kind of message. There are different kinds of messages.
  await new Promise((resolve, reject) => {
    bot.on('message', (msg) => {
      if (!locale.keyboards[msg.text] &&
        !msg.text.includes("previous button")) {
        console.log("user message is: " + msg.text)
        const opts = {
          reply_to_message_id: msg.message_id,
          reply_markup: JSON.stringify({
            keyboard: arrangeKeyboardWithOut(locale.keyboards["/start"].childs, 2, msg),
            resize_keyboard: true,
            one_time_keyboard: true
          })
        };
        bot.sendMessage(msg.chat.id, ' your message recieved', opts);
      }
      resolve(true);
    });
  });
  return
}

var getmessage1 = async () => {
  await getmessage();
}

然后我打电话给 else if

else if (msg.text == ["contact us"]) {
      const opts = {
        reply_to_message_id: msg.message_id,
        reply_markup: JSON.stringify({
          keyboard: arrangeKeyboard(locale.keyboards[msg.text].childs, 2, msg),
          resize_keyboard: true,
          one_time_keyboard: true
        })
      };
      bot.sendMessage(msg.chat.id, "Please Write your Messages:", opts);
      getmessage1();
    }