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

时间:2018-02-02 22:49:02

标签: javascript node.js telegram-bot

我希望获取用户消息并通过此module将其发送给管理员,这在首次使用,但在每个/start命令中向管理员发送重复消息。 例如,在First /start命令中,每个东西都是okey,我的输入是A,而我的输出是 A Your Message Saved, Can I Send it? 发送后: A User Message to Admin Your message has been successfully sent

在Secend /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);
  });

});

1 个答案:

答案 0 :(得分:0)

解决了✔️

//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