所以我正在构建一个机器人,它将使用这样的命令接受日期字符串
/add-date 1997
。
当用户发送没有日期的命令时,我也想正确处理它,就像这样
/add-date
。我一直在寻找一种方法来复制其他一些机器人(例如@vkmusic_bot)的策略,它们通过以下方式处理这种情况:
1,收到
/song
命令后,机器人会发送此消息
2,它会自动在底部字段准备回复,标记为机器人的第一个回复。
现在给定一个歌曲标题,它将执行与用户发送的相同的操作
/song [song title]
。
手动回复What song are you looking for?
文字也有同样的效果。
这只适用于团体。
我不明白他们是如何做到这一点的。希望得到任何帮助🙏
此外,我正在使用
node.js
在
node-telegram-bot-api
上构建机器人
你的问题有点不清楚。 我假设您想在机器人上实现类似的行为,以便它以两种方式处理 /date 命令:
这是代码:
// Create RegEx for the format of your choice. The following will accept DD/MM/YYYY
const dateRegex = /\d{1,2}\/\d{1,2}\/\d{2,4}/
bot.on("message", (msg) => {
/// Ignore if the message is not /date command
if (!msg.text?.startsWith("/date")) return;
// Checks if the command is exactly "/date" without the date specified.
if (msg.text == "/date") {
bot.sendMessage(msg.chat.id, "Send me a date to continue.", {
reply_markup: {
force_reply: true, // Automatically goes to reply to this particular message
}
});
return;
}
/// If the command contains a date, extract it with the specified regex and process it
const match = dateRegex.match(msg.text);
const date = match[0];
if (date) {
processDate(date);
}
});
// Process incoming date message (this will be executed when the user sends the date alone - also as a reply to the bot's message)
bot.onText(dateRegex, (msg) => {
const match = dateRegex.match(msg.text);
const date = match[0];
processDate(date);
})
// A method to process the date
function processDate(date) {
// ...
}
希望这有帮助!