【问题标题】:Copy value out of users input to another channel将用户输入中的值复制到另一个渠道
【发布时间】:2019-11-28 18:19:04
【问题描述】:

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

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

所以假设我有句子:

您好,我在 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 没有太多经验,因此欢迎任何帮助...提前致谢!

【问题讨论】:

  • 嘿,这是你之前问题的更新版本吗?如果有,请删除旧版本。谢谢 (stackoverflow.com/questions/59084029/…)
  • 您的代码有哪些具体问题?什么不工作?您是否收到错误消息?
  • 问题是我不知道如何将用户输入中的单个值复制到另一个通道。如果这是可能的当然
  • 所以有一个主渠道,用户在其中说出他的问题。我想要的是从该消息中获取某个值并将其发送到另一个频道。

标签: javascript chatbot slack slack-api


【解决方案1】:

这是您的代码的工作示例。我冒昧地重组和重命名函数和变量以提高可读性。

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

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

【讨论】:

  • 所以我没有得到的是部分:var found = false。为什么是假的?
  • 而且 //example 消息不是实际代码的一部分吗?
  • 正确。示例消息不是解决方案的一部分。只是为了证明它有效
  • find的逻辑是:函数会返回found的值。 found 将为 false,除非术语匹配并找到设置为教室 ID。换句话说。如果该函数没有找到匹配项,它将返回 false。
  • 如果您想回答,您还有一个问题。我正在尝试将函数 messageProblemSolver 链接到函数 findClassroomMention,但它不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-24
  • 2018-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-29
  • 1970-01-01
相关资源
最近更新 更多