从用户输入中复制价值到另一个渠道

时间:2019-11-28 18:19:04

标签: javascript chatbot slack slack-api

我正在为学校使用聊天机器人,该机器人可以解决学校中的问题。所以基本上我会停留在这部分上,因为我不知道该怎么做...

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

所以说我有这句话:

  

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

1 个答案:

答案 0 :(得分: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