I am working on a chatbot for school that solves problems in my school. So basically I'm stuck on this part because I have no idea how to do this...
So lets say I have the sentence:
Hi, I have a problem in classroom 108
The value "classroom 108" is equal to the value "classroom 108" in my array.
var classroom= [
{
L108: ["classroom 108","L108"]
},{
L208: ["classroom 208","L208"]
}
];
So now I want to write a message to another channel containing the variable "L108".
function handleMessage(message) {
classroom.forEach((value, index) => {
if (message.includes(classroom)) {
classroom();
console.log(message);
};
})
};
function classroom(message) {
const params = {
icon_emoji: ':smiley:'
}
textchannel = probleemoplosser + ", een docent heeft een probleem met het " + probleem + " in ",classroom, params;
reactie = "Top, ik heb het doorgegeven aan " + naam;
bot.postMessageToChannel( otherchannel,textchannel,params);
bot.postMessageToChannel('general',reactie, params);
};
I don't have much experience with JavaScript so any help is welcome...thanks in advance!<3
Here is a working example of your code. I took the liberty to restructure and rename functions and variables to improve readability.
The main thing missing in your original code was an inner loop that goes through all terms and compares them.
var classrooms = {
L108: ["classroom 108","L108"],
L208: ["classroom 208","L208"]
};
// returns classroom ID if classroom is mentioned in message string
// else returns false
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) break;
}
return found
};
// sends notification to problem solver channel about a classroom
function notifyProblemSolver(classroomId) {
// TODO: dummy function to be replaced with bot code
console.log('We need help with classroom ' + classroomId)
};
// example message
var message = 'Hi, I have a problem in classroom 108'
var classroomId = findClassroomMention(message)
if (classroomId) {
notifyProblemSolver(classroomId)
}
See here for a live demo.