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

时间:2017-05-02 09:46:52

标签: javascript node.js telegram-bot

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

使用此代码,我将收到用户消息,并且我排除了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); } }); read因为我需要Just console log Users真实消息(不是按钮标题), 我需要在我select按钮的时候收到用户留言。

epoll

1 个答案:

答案 0 :(得分:0)

首先我制作了这两个功能

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();
    }