从输入到另一个通道的用户中复制值

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

我正在为学校的聊天机器人工作,可以解决学校的问题。所以基本上我被困在这部分上,因为我不知道该怎么做...

  • 我想检查句子中的某个值是否等于数组中的值。
  • 如果它等于我数组中的值,那么我希望我的聊天机器人将消息写到另一个包含该变量的通道中

所以可以说我有这句话:

嗨,我在教室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

javascript chatbot slack slack-api
1个回答
0
投票

这里是您的代码的有效示例。我自由地重组和重命名了函数和变量,以提高可读性。

原始代码中缺少的主要内容是一个遍历所有术语并进行比较的内部循环。

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进行现场演示。

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