javascript - Hubot:Rocket.Chat / Meteor / JavaScript 中没有覆盖类的方法
问题描述
Rocket.Chat 已弃用并删除了 Internal Hubot 功能,但在我们公司,我们有一些脚本目前无法移动到外部 bot,因此我们希望自己维护 Internal Hubot 功能(因为我们已经有自定义 fork因为 LDAP 相关的更改)。
我几乎可以让它工作。内部 Hubot 已初始化,脚本已加载。但是调用一些命令返回Meteor code must always run within a Fiber (...)
错误。
经过一些调试,这里的核心问题似乎是 Hubot 机器人的类继承,并且适配器不起作用。有一些自定义类扩展了 Hubot 的包类:
export class InternalHubotRobot extends Hubot.Robot {
constructor(name, alias) {
super(null, 'shell', false, name, alias);
this.hear = bind(this.hear);
this.respond = bind(this.respond);
this.enter = bind(this.enter);
this.leave = bind(this.leave);
this.topic = bind(this.topic);
this.error = bind(this.error);
this.catchAll = bind(this.catchAll);
this.user = Meteor.users.findOne({ username: this.name }, { fields: { username: 1 } });
this.adapter = new RocketChatAdapter(this);
new HubotScripts(this);
console.log(`InternalHubotRobot initialized as '${ this.name }'`);
}
loadAdapter() { return false; }
hear(regex, callback) { return super.hear(regex, Meteor.bindEnvironment(callback)); }
respond(regex, callback) { return super.respond(regex, Meteor.bindEnvironment(callback)); }
enter(callback) { return super.enter(Meteor.bindEnvironment(callback)); }
leave(callback) { return super.leave(Meteor.bindEnvironment(callback)); }
topic(callback) { return super.topic(Meteor.bindEnvironment(callback)); }
error(callback) { return super.error(Meteor.bindEnvironment(callback)); }
catchAll(callback) { return super.catchAll(Meteor.bindEnvironment(callback)); }
}
和
export class RocketChatAdapter extends Hubot.Adapter {
constructor(robot) {
super(robot);
console.log('RocketChatAdapter initialized');
}
send(envelope, ...strings) {
this.robot.logger.info('[ROBOT → adapter → send()]');
// ...
}
// Many more overrides
}
内部 Hubot 已初始化,InternalHubot = new InternalHubotRobot(RocketChat.settings.get('InternalHubot_Username'), 'Custom Alias');
我在日志中看到:
I20190318-11:28:59.866(1)? RocketChatAdapter initialized
I20190318-11:29:00.209(1)? [Mon Mar 18 2019 11:29:00 GMT+0100 (CET)] WARNING A script has tried registering a HTTP route while the HTTP server is disabled with --disabled-httpd.
I20190318-11:29:00.211(1)? Loaded help.coffee
I20190318-11:29:00.211(1)? InternalHubotRobot initialized as 'Custom Alias'
所以两个构造函数都被正确调用了。但是所有对机器人和适配器方法的方法调用都是在 Hubot 包中的基类上调用的,而不是从InternalHubotRobot
/中调用的RocketChatAdapter
。
这就是问题所在:由于这些方法没有按应有的方式被覆盖/调用,因此存在 Fiber 错误并且没有发送响应,因为默认的 Hubot 适配器的send()
方法不做任何事情。
我做了什么:
- 我能够通过包装来摆脱 Fiber 错误
robot.respond /(help)+(?:\s+(.*))?$/i, Meteor.bindEnvironment((incomingMessage)
(这基本上是InternalHubotRobot
构造函数应该通过包装方法来做的事情bind()
- 我能够
send
通过原型覆盖来发送响应:Hubot.Robot.prototype.send = function(envelope, ...args) { return sendHelper(this, envelope, args, (string) => RocketChat.sendMessage(InternalHubot.user, { msg: string }, { _id: envelope.room }) ); };
但是:它很脏,我想依赖子类中的继承和方法覆盖。
我不是 JavaScript / Meteor / Rocket.Chat 专家,所以也许我遗漏了一些细节。如何实现工作继承?
有关版本的信息:
- Rocket.Chat 0.73.1 / 0.74.3(都带有定制的内部 Hubot,在我们的定制分叉中恢复)。在生产中,我们有 0.72.3 并且内部 Hubot 工作正常
- 哈博特 3.3.1
- 流星 1.8.0.2
解决方案
推荐阅读
- reactjs - 在反应中将卡片的内容从一页传递到另一页
- javascript - JavaScript/Express Promise.all() 问题
- here-api - 逐向导航中的测量单位
- flutter - 如何为大型项目编写 injection_container(服务注入器)
- python - tesseract-ocr个位数识别如何提高?
- flutter - Flutter - 如何隐藏全局底部导航栏?
- gremlin - 如何在 Amazon Neptune 中使用 gremlin 计算 PageRank 和最短路径算法?
- angular - Angular-Ionic:视频标签中的视频之间的无缝过渡?
- reactjs - 材质 ui 中的 ScopedCssBaseline 覆盖组件的 box-sizing
- sql - 多个插入/更新的 SQL 前缀