将用户输入的价值复制到另一个渠道
Copy value out of users input to another channel
我正在为学校开发一个聊天机器人来解决我学校的问题。所以基本上我卡在了这部分,因为我不知道该怎么做...
- 我想检查一个句子中的某个值是否等于我的数组中的一个值。
- 如果它等于我数组中的一个值,那么我希望我的聊天机器人将消息写入包含该变量的另一个通道
所以假设我有一句话:
Hi, I have a problem in classroom 108
值 "classroom 108" 等于我数组中的值 "classroom 108"。
var classroom= [
{
L108: ["classroom 108","L108"]
},{
L208: ["classroom 208","L208"]
}
];
所以现在我想向另一个包含变量 "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);
};
我对 JavaScript 没有太多经验,所以欢迎任何帮助...提前致谢!<3
这是您的代码的一个工作示例。我冒昧地重组和重命名函数和变量以提高可读性。
您的原始代码中缺少的主要内容是遍历所有项并进行比较的内部循环。
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)
}
请参阅 here 观看现场演示。
我正在为学校开发一个聊天机器人来解决我学校的问题。所以基本上我卡在了这部分,因为我不知道该怎么做...
- 我想检查一个句子中的某个值是否等于我的数组中的一个值。
- 如果它等于我数组中的一个值,那么我希望我的聊天机器人将消息写入包含该变量的另一个通道
所以假设我有一句话:
Hi, I have a problem in classroom 108
值 "classroom 108" 等于我数组中的值 "classroom 108"。
var classroom= [
{
L108: ["classroom 108","L108"]
},{
L208: ["classroom 208","L208"]
}
];
所以现在我想向另一个包含变量 "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);
};
我对 JavaScript 没有太多经验,所以欢迎任何帮助...提前致谢!<3
这是您的代码的一个工作示例。我冒昧地重组和重命名函数和变量以提高可读性。
您的原始代码中缺少的主要内容是遍历所有项并进行比较的内部循环。
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)
}
请参阅 here 观看现场演示。