这是我第一次与火箭聊天,hubot和docker合作。
我使用docker运行robotschat和hubot,它起作用了,但是当我尝试向文件夹 scripts 中添加一些自定义脚本时,我使用了 robot.adapter.api.get() 调用火箭聊天api,它会返回错误。
如果在使用async / await的自定义脚本中,该机器人无法启动。
我尝试通过console.log记录日志,并获取未定义的.api。
我克隆了源代码 hubot-rocketchat-boilerplate ,并将我的自定义脚本移至 scripts 文件夹,然后运行 yarn local ,它可以正常工作,但是我不知道如何使用此源到docker,我对此没有经验。
docker-composer.yml
hubot:
image: rocketchat/hubot-rocketchat:latest
restart: unless-stopped
environment:
- ROCKETCHAT_URL=rocketchat:3000
- RESPOND_TO_DM=true
- ROCKETCHAT_USER=bot
- ROCKETCHAT_PASSWORD=botpassword
- BOT_NAME=bot
- EXTERNAL_SCRIPTS=hubot-help,hubot-seen,hubot-links,hubot-diagnostics
depends_on:
- rocketchat
labels:
- "traefik.enable=false"
volumes:
- ./scripts:/home/hubot/scripts
ports:
- 3001:8080
我的自定义脚本
module.exports = (robot) => {
robot.respond(/\bis (.*) online$/i, function(res) {
const name = res.match[1]
robot.adapter.api.get(`users.getPresence?username=${name}`).then((result) => {
if (!result) {
res.reply('User not existed!!!')
return
}
res.reply((result.presence == 'online')
? `Yes, @${name} is online.`
: `No, @${name} does not online.`
)
})
})
}
ERROR TypeError: Cannot read property 'get' of undefined
at TextListener.callback (/home/hubot/scripts/isLogin.js:10:22)
at /home/hubot/node_modules/hubot/src/listener.coffee:65:12
at allDone (/home/hubot/node_modules/hubot/src/middleware.coffee:44:37)
at /home/hubot/node_modules/hubot/node_modules/async/lib/async.js:274:13
at Object.async.eachSeries (/home/hubot/node_modules/hubot/node_modules/async/lib/async.js:142:20)
at Object.async.reduce (/home/hubot/node_modules/hubot/node_modules/async/lib/async.js:268:15)
at /home/hubot/node_modules/hubot/src/middleware.coffee:49:13
at nextTickCallbackWith0Args (node.js:489:9)
at process._tickCallback (node.js:418:13)
ERROR Unable to load /home/hubot/scripts/isLogin: SyntaxError: Unexpected token function
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (/home/hubot/node_modules/coffee-script/lib/coffee-script/register.js:45:36)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Robot.loadFile (/home/hubot/node_modules/hubot/src/robot.coffee:355:18)
at Robot.load (/home/hubot/node_modules/hubot/src/robot.coffee:377:10)
at RocketChatBotAdapter.loadScripts (/home/hubot/node_modules/hubot/bin/hubot:97:13)
at RocketChatBotAdapter.g (events.js:260:16)
at emitNone (events.js:67:13)
at RocketChatBotAdapter.emit (events.js:166:7)
at /home/hubot/node_modules/hubot-rocketchat/src/rocketchat.js:268:26
at _fulfilled (/home/hubot/node_modules/hubot-rocketchat/node_modules/q/q.js:854:54)
at self.promiseDispatch.done (/home/hubot/node_modules/hubot-rocketchat/node_modules/q/q.js:883:30)
at Promise.promise.promiseDispatch (/home/hubot/node_modules/hubot-rocketchat/node_modules/q/q.js:816:13)
at /home/hubot/node_modules/hubot-rocketchat/node_modules/q/q.js:624:44
at runSingle (/home/hubot/node_modules/hubot-rocketchat/node_modules/q/q.js:137:13)
at flush (/home/hubot/node_modules/hubot-rocketchat/node_modules/q/q.js:125:13)
at nextTickCallbackWith0Args (node.js:489:9)
at process._tickCallback (node.js:418:13)