如何使用电报机器人实现自动回复功能

问题描述 投票:0回答:1

所以我正在构建一个机器人,它将使用这样的命令接受日期字符串

/add-date 1997

当用户发送没有日期的命令时,我也想正确处理它,就像这样

/add-date
。我一直在寻找一种方法来复制其他一些机器人(例如@vkmusic_bot)的策略,它们通过以下方式处理这种情况:

1,收到

/song
命令后,机器人会发送此消息

您在寻找什么歌曲? Blockquote

2,它会自动在底部字段准备回复,标记为机器人的第一个回复。

enter image description here

现在给定一个歌曲标题,它将执行与用户发送的相同的操作

/song [song title]
。 手动回复
What song are you looking for?
文字也有同样的效果。

这只适用于团体。

我不明白他们是如何做到这一点的。希望得到任何帮助🙏

此外,我正在使用

node.js
node-telegram-bot-api

上构建机器人
node.js telegram telegram-bot
1个回答
0
投票

你的问题有点不清楚。 我假设您想在机器人上实现类似的行为,以便它以两种方式处理 /date 命令:

  • 如果提供了日期(例如/date 08/05/2000),请立即处理。
  • 如果未给出日期(仅/日期),请在处理之前询问用户日期。

这是代码:


// 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) {
    // ...
}

希望这有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.