docs/adapters/development.md
All adapters inherit from the Adapter class in the src/Adapter.mjs file.
const Adapter = require('hubot/index.mjs').Adapter;
There are certain methods that you will want to override. Here is a basic stub of what an extended Adapter class would look like:
const Adapter = require('../adapter')
const User = require('../user')
const TextMessage = require('../message').TextMessage
class Sample extends Adapter {
constructor(robot) {
super(robot)
this.robot.logger.info('Constructor')
}
send(envelope, ...strings) {
this.robot.logger.info('Send')
}
reply(envelope, ...strings) {
this.robot.logger.info('Reply')
}
run() {
this.robot.logger.info('Run')
this.emit('connected') // The 'connected' event is required to trigger loading of Hubot scripts.
const user = new User(1001, 'Sample User')
const message = new TextMessage(user, 'Some Sample Message', 'MSG-001')
this.robot.receive(message)
}
}
exports.use = (robot) => new Sample(robot)
hubot-samplemkdir hubot-samplehubot-samplecd hubot-samplenpm init to create your package.jsonsrc/sample.js.gitignore to include node_modulessrc/sample.js file to include the above stub for your adapterpackage.json to add a peer dependency on hubot"dependencies": {
},
"peerDependencies": {
"hubot": ">=11"
}
npx hubot --create myhubothubot you created in step 7.npm link to add your adapter to hubotnpm link ../hubot-samplehubot -a sampleThere is a an open issue in the node community around npm linked peer dependencies not working. To get this working for our project you will need to do some minor changes to your code.
hubot-sample adapter, add the following codelet {Robot,Adapter,TextMessage,User} = {}
try {
{Robot,Adapter,TextMessage,User} = require('hubot')
} catch {
const prequire = require('parent-require')
{Robot,Adapter,TextMessage,User} = prequire('hubot')
}
hubot-sample folder, modify the package.json to include the following dependency so this custom import mechanism will work"dependencies": {
"parent-require": "^1.0.0"
}
hubot -a sample again and see that the imports are properly loaded.npm, you won't need the dependency in hubot anymore since the peer dependency should work as an official module.Another option is to load the file from local disk.
hubot-samplemkdir hubot-samplehubot-samplecd hubot-samplenpm init to create your package.jsonsrc/sample.js.gitignore to include node_modulessrc/sample.js file to include the above stub for your adapterpackage.json to add a peer dependency on hubot"dependencies": {
},
"peerDependencies": {
"hubot": ">=11"
}
npx hubot -p ./src -a sample.js