发送一次消息并停止重复发送消息

Sending msg once and stop Repeat sending message

我想获取用户消息并通过此 module 将其发送给管理员,第一次没问题,但在每个 /start 命令中都会向管理员发送重复的消息。 例如,在第一个 /start 命令中,一切正常,我的输入是 A,我的 输出是 A Your Message Saved, Can I Send it? 发送后: A User Message to Admin Your message has been successfully sent

第二个 /start 我的输入是 B 我的输出是;:

B
Your Message Saved, Can I Send it?

发送后:

B 
User Message to Admin
B 
User Message to Admin
Your message has been successfully sent
Your message has been successfully sent

和 在第三个 /start 中,我的输入是 C,我的输出是;: 以上输出但 3 重复消息 C

我该如何解决?

代码:

let iMsg = null; // iMsg is null
bot.onText(/\/start/, async (msg) => {
  const opts = {
    reply_markup: JSON.stringify({
      remove_keyboard: true,
    }),
  };
  await bot.sendMessage(msg.chat.id, 'Please send your message:', opts);

  // Get User Message:
  await bot.once('message', async (msg) => { // listen once to msg
    const opts = {
      reply_markup: JSON.stringify({
        keyboard: [
          ['Send'],
        ],
        resize_keyboard: true,
        one_time_keyboard: true,
      }),
    };
    iMsg = msg.text; // iMsg has a Value
    await bot.sendMessage(msg.chat.id, `${iMsg}\n\n Your Message Saved, Can I Send it?`, opts);
  });
  await bot.onText(/Send/, async (msg) => {
    const opts = {
      reply_markup: JSON.stringify({
        keyboard: StartKeyboard,
        resize_keyboard: true,
        one_time_keyboard: true,
      }),
    };
    await bot.sendMessage('AdminId', `${iMsg}\n\n'User Message to Admin.'`, opts);
    await bot.sendMessage(msg.chat.id, 'Your message has been successfully sent', opts);
  });

});

已解决✔️

//Keyboards
const StartKeyboard = [
  ['a', 'b'],
  ['Contact Us']
]
//Main Script
bot.onText(/\/start/, (msg) => {
  const opts = {
    reply_to_message_id: msg.message_id,
    reply_markup: JSON.stringify({
      keyboard: StartKeyboard,
      resize_keyboard: true,
      one_time_keyboard: true
    })
  };
  bot.sendMessage(msg.chat.id, `Hello`, opts);
});

bot.onText(/Contact Us/, (msg) => {
  const opts = {
    reply_to_message_id: msg.message_id,
    reply_markup: JSON.stringify({
      keyboard: StartKeyboard,
      resize_keyboard: true,
      one_time_keyboard: true
    })
  };
  bot.sendMessage(msg.chat.id, `Please Write Your Message:`, opts);
  getmessage1();
});
// Functions
var getmessage = async () => {
  await new Promise((resolve, reject) => {
    bot.once('message', (msg) => {
      console.log("User Message Is: " + msg.text)
      const opts = {
        reply_to_message_id: msg.message_id,
        reply_markup: JSON.stringify({
          keyboard: StartKeyboard,
          resize_keyboard: true,
          one_time_keyboard: true
        })
      };
      bot.sendMessage(msg.chat.id, 'Thanks, Your Message Received', opts);
      resolve(true);
    });
  });
  return
}

var getmessage1 = async () => {
  await getmessage();
}
//END
//https://github.com/saeedhei