我目前正在为学校项目制作聊天机器人。
因此,基本上,我想要的是聊天机器人将特定消息的副本从用户输入发送到另一个通道。但是,当我运行代码时,它将返回未定义的值。我的代码如下所示:

bot.on('message', data => {
    if (data.type !== 'message' || data.subtype === 'bot_message') {
        return;
    }
    findClassroomMention(data.text);
});

var classrooms =
    {
        L108: ["L108","108"],
        L208: ["L208","208"]
    };


function findClassroomMention(message) {
    var found = false
    for(var ClassroomId in classrooms) {
        for(var term of classrooms[ClassroomId]) {
            if(message.includes(term)) {
                found = ClassroomId;
                break;
            }
        }
        if (found) {
            notifyProblemSolver();
            break;
        }
    }
    return found
};

function notifyProblemSolver(ClassroomId) {
    const params = {
        icon_emoji: ':smiley:'
    }
    bot.postMessageToChannel('caris','We have a problem in ' + ClassroomId, params);
};


例如,用户的输入是:


  嗨,我在L108教室遇到问题


然后,我希望聊天机器人将包含值L108的消息发送到问题求解器,如下所示:


  L108有问题


但是,当我运行代码时,它将发送未定义的L108:


  我们有一个不确定的问题

最佳答案

您忘记了通过ClassroomId中的notifyProblemSolver()

问题

if (found) {
    // You forgot to pass the ClassroomId
    notifyProblemSolver();
    break;
}




if (found) {
    // Add the ClassroomId
    notifyProblemSolver(ClassroomId);
    break;
}

关于javascript - 文字未定义-JavaScript,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59171498/

10-12 21:41