运行 一次函数
Run A Function just for one Time
如何调用和使用此函数仅 1 次并在 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();
}
您只需要将 bot.on
更改为 bot.once
这样,bot 对象将仅侦听一次事件,然后脱离侦听器范围。
如何调用和使用此函数仅 1 次并在 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();
}
您只需要将 bot.on
更改为 bot.once
这样,bot 对象将仅侦听一次事件,然后脱离侦听器范围。