我应该使用什么模式在 Node/JavaScript 中制作监听机器人

What pattern should I use for making a listen bot in Node/JavaScript?

本文关键字:JavaScript 机器人 监听 Node 什么 模式 我应该      更新时间:2023-09-26

我目前正在使用 Node 制作我的第一个机器人,但我对如何将其变为现实有点困惑。问题是,对于这种东西,我应该使用的最佳模式和模式名称是什么?

基本上,一个人可以听一个主题和演讲者。

var test = person("ask_name","hallo person you are special");
console.log(test); // should return thanks!
var test = person("ask_name","hallo person you are dumb as the bird");
console.log(test); // should return i hate you!
function person(ability, body) {
  console.log(ability,body);
  var ability_name = "ability_" + ability;
  console.log(ability_name,typeof ability_name); // ignore all of this, trying something
  if (typeof ability_name) {};
  // ability list array
  var ability = [];
  // Search for ability
  // not done
  var say_bad = function() {
    this.listen_subject = 'ask_name';
    this.listen_body = 'you are dumb';
    return "i hate you!"
  }
  var say_good = function() {
    this.listen_subject = 'ask_name';
    this.listen_body = 'you are special';
    return "thanks!"
  }
}

抱歉,没有完成代码,但这是我能走的最远。

如果你在谈论对话逻辑,责任链似乎是要走的路。话虽如此,这仅适用于实现响应树。您可能有不同的需求。

可以工作的方式是:逻辑树中的所有节点都将具有此方法;

bool consider(utterence, history);

如果节点或其子节点之一已处理这种情况,则此方法将返回 true。

我会实现几个主题检测器和一个通用的"我无法想出一个好的答案",以及一堆专门的响应者。

逻辑是这样的:

(天气检测器考虑话语,历史)(确定主题是天气)(传递给处理天气对话的对象链)

(天气检测器考虑话语,历史)(确定主题不是天气)(传递到主题检测器链中的下一个)(通用FunnyResponder考虑话语,历史)(随机选择一个古怪的答案)