首页 > 解决方案 > 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()方法不做任何事情。

我做了什么:

但是:它很脏,我想依赖子类中的继承和方法覆盖。

我不是 JavaScript / Meteor / Rocket.Chat 专家,所以也许我遗漏了一些细节。如何实现工作继承?

有关版本的信息:

标签: javascriptmeteorhubotrocket.chat

解决方案


推荐阅读