编码电报机器人时出现Node.js错误

时间:2018-05-29 19:09:34

标签: node.js telegram-bot

http://prntscr.com/jo7mug

这就是错误的样子。我的机器人一直对我回答两次(但我不想要它)。 代码本身如下。首先,我要求用户在样式之间选择文件和链接。如果选择了一个样式,我会为用户提供一个新的键盘,但仍然按下它会使我的机器人双重发送消息。

// нажал SOUNDCLOUD
bot.onText(/\Link to Soundcloud/, (msg) => {
    bot.sendMessage(msg.chat.id, "Alright, " + msg.from.first_name + ". Let's choose a style and I'll guide You through the whole Soundcloud", {
        "reply_markup": {
            "keyboard": [["Dancy", "Chill"], ["Sensual", "Jazz"], ["Back to choose link or file"]],
            "resize_keyboard": true
        }
    });
    // нажал dancy после того, как выбрал ссылку
    bot.onText(/\Dancy/, (msg) => {
        bot.sendMessage(msg.chat.id, "Dancy link is here,  " + msg.from.first_name + ". Enjoy!" + dancy_collection[rand_dancy_collection], {
            "reply_markup": {
                "keyboard": [["More dancy"], ["Back to choose style"]],
                "resize_keyboard": true
            }
        });
    });
    // нажал chill после того, как выбрал ссылку
    bot.onText(/\Chill/, (msg) => {
        bot.sendMessage(msg.chat.id, "Chill link is here,  " + msg.from.first_name + ". Enjoy!" + chill_collection[rand_chill_collection], {
            "reply_markup": {
                "keyboard": [["More chill"], ["Back to choose style"]],
                "resize_keyboard": true
            }
        });
    });
    // нажал sensual после того, как выбрал ссылку
    bot.onText(/\Sensual/, (msg) => {
        bot.sendMessage(msg.chat.id, "Sensual link is here,  " + msg.from.first_name + ". Enjoy!" + sensual_collection[rand_sensual_collection], {
            "reply_markup": {
                "keyboard": [["More sensual"], ["Back to choose style"]],
                "resize_keyboard": true
            }
        });
    });
    // нажал jazz после того, как выбрал ссылку
    bot.onText(/\Jazz/, (msg) => {
        bot.sendMessage(msg.chat.id, "Jazz link is here,  " + msg.from.first_name + ". Enjoy!" + jazz_collection[rand_jazz_collection], {
            "reply_markup": {
                "keyboard": [["More jazz"], ["Back to choose style"]],
                "resize_keyboard": true
            }
        });
    });
    // нажал dancy ещё раз после того, как выбрал ссылку
    bot.onText(/\More dancy/, (msg) => {
        bot.sendMessage(msg.chat.id, "Another dancy track is here,  " + msg.from.first_name + ". Enjoy!" + dancy_collection[rand_dancy_collection], {
            "reply_markup": {
                "keyboard": [["More dancy"], ["Back to choose style"]],
                "resize_keyboard": true
            }
        });
    });
    // нажал chill ещё раз после того, как выбрал ссылку
    bot.onText(/\More chill/, (msg) => {
        bot.sendMessage(msg.chat.id, "Another chill is here,  " + msg.from.first_name + ". Enjoy!" + chill_collection[rand_chill_collection], {
            "reply_markup": {
                "keyboard": [["More chill"], ["Back to choose style"]],
                "resize_keyboard": true
            }
        });
    });
    // нажал sensual ещё раз после того, как выбрал ссылку
    bot.onText(/\More sensual/, (msg) => {
        bot.sendMessage(msg.chat.id, "Another sensual link is here,  " + msg.from.first_name + ". Enjoy!" + sensual_collection[rand_sensual_collection], {
            "reply_markup": {
                "keyboard": [["More sensual"], ["Back to choose style"]],
                "resize_keyboard": true
            }
        });
    });
    // нажал jazz ещё раз после того, как выбрал ссылку
    bot.onText(/\More jazz/, (msg) => {
        bot.sendMessage(msg.chat.id, "Another jazz link is here,  " + msg.from.first_name + ". Enjoy!" + jazz_collection[rand_jazz_collection], {
            "reply_markup": {
                "keyboard": [["More jazz"], ["Back to choose style"]],
                "resize_keyboard": true
            }
        });
    });
});

1 个答案:

答案 0 :(得分:0)

使用此正则表达式:

bot.onText(/^[/]Link to Soundcloud$/, (msg) => {
    bot.sendMessage(msg.chat.id, "Alright, " + msg.from.first_name + ". Let's choose a style and I'll guide You through the whole Soundcloud", {
        "reply_markup": {
            "keyboard": [["Dancy", "Chill"], ["Sensual", "Jazz"], ["Back to choose link or file"]],
            "resize_keyboard": true
        }
    });