Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
240 views
in Technique[技术] by (71.8m points)

javascript - 从用户输入中复制价值到另一个渠道(Copy value out of users input to another channel)

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...

(所以基本上我会停留在这部分上,因为我不知道该怎么做...)

  • I want to check if a certain value in a sentence is equal to a value in my array.

    (我想检查句子中的某个值是否等于数组中的值。)

  • If it is equal to a value in my array, then I want my chatbot to write a message to another channel containing that variable

    (如果它等于我数组中的值,那么我希望我的聊天机器人将一条消息写入另一个包含该变量的通道)

So lets say I have the sentence:

(所以说我有这句话:)

Hi, I have a problem in classroom 108

(嗨,我在教室108有问题)

The value "classroom 108" is equal to the value "classroom 108" in my array.

(在我的数组中,值“教室108”等于值“教室108”。)

   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".

(因此,现在我想向另一个包含变量“ 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

(我没有JavaScript的丰富经验,因此欢迎您提供任何帮助...在此先感谢!<3)

  ask by Martijn_- translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

(观看这里的现场演示。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...