Development adapter
Adapter Basics
All adapters inherit from the Adapter class in the src/adapter.coffee file. There are certain methods that
you will want to override. Here is a basic stub of what an extended Adapter class would look like:
class Sample extends Adapter
constructor: ->
super
@robot.logger.info "Constructor"
send: (envelope, strings...) ->
@robot.logger.info "Send"
reply: (envelope, strings...) ->
@robot.logger.info "Reply"
run: ->
@robot.logger.info "Run"
@emit "connected"
user = new User 1001, name: 'Sample User'
message = new TextMessage user, 'Some Sample Message', 'MSG-001'
@robot.receive message
exports.use = (robot) ->
new Sample robot
Setting Up Your Development Environment
- Create a new folder for your adapter
hubot-samplemkdir hubot-sample
- Change your working directory to
hubot-samplecd hubot-sample
- Run
npm initto create your package.json- make sure the entry point is
src/sample.coffee
- make sure the entry point is
- Add your
.gitignoreto includenode_modules - Edit the
src/sample.coffeefile to include the above stub for your adapter - Edit the
package.jsonto add a peer dependency onhubot
"dependencies": {
},
"peerDependencies": {
"hubot": ">=2.0"
},
"devDependencies": {
"coffee-script": ">=1.2.0"
}
- Generate your Hubot using the
yo hubotcommand - Change working directories to the
hubotyou created in step 7. - Now perform an
npm linkto add your adapter tohubotnpm link ../hubot-sample
- Run
hubot -a sample
Gotchas
There 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.
- For the import in your
hubot-sampleadapter, add the following code
try
{Robot,Adapter,TextMessage,User} = require 'hubot'
catch
prequire = require('parent-require')
{Robot,Adapter,TextMessage,User} = prequire 'hubot'
- In your
hubot-samplefolder, modify thepackage.jsonto include the following dependency so this custom import mechanism will work
"dependencies": {
"parent-require": "^1.0.0"
}
- Now try running
hubot -a sampleagain and see that the imports are properly loaded. - Once this is working properly, you can build out the functionality of your adapter as you see fit. Take a
look at some of the other adapters to get some ideas for your implementation.
- Once packaged and deployed via
npm, you won't need the dependency inhubotanymore since the peer dependency should work as an official module.
- Once packaged and deployed via