仅运行一次功能

时间:2017-05-03 11:06:28

标签: javascript node.js async-await telegram-bot

如何在1 rime和rhat kill rhe函数之后调用和使用此函数? 调用函数:

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

这是我的功能,我不想在循环中运行,我需要发送消息并终止函数。

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

1 个答案:

答案 0 :(得分:2)

您只需将bot.on更改为bot.once

即可

这样,bot对象只会侦听一次事件,然后从侦听器范围中分离出来。