Permalink
Browse files

Extra Adapter into it's own file and update adapters

  • Loading branch information...
1 parent 1455b62 commit b300e839ec9da7b33c85215c21efa1d1f2f72678 Tom Bell committed Nov 20, 2011
View
@@ -30,14 +30,15 @@ has written his own adapters.
2. Add `hubot` as a dependency to your `package.json` file
3. Add your main adapter file as the `main` file in `package.json`
-Below is an example of requiring hubot to extend `Robot.Adapter` and exporting
+Below is an example of requiring hubot to extend `Adapter` and exporting
a `use` function that will be used to load your adapter when used.
```coffeescript
-Robot = require("hubot").robot()
+Robot = require("hubot").robot()
+Adapter = require("hubot").adapter()
-class MyAdapter extends Robot.Adapter
+class MyAdapter extends Adapter
# You'll want to override the various methods see existing adapters
# ...
View
@@ -11,6 +11,9 @@ exports.loadBot = (adapterPath, adapterName, botName) ->
exports.robot = ->
require './src/robot'
+exports.adapter = ->
+ require './src/adapter'
+
# Loads the Hubot test harness.
exports.tests = ->
require './test/tests'
View
@@ -0,0 +1,104 @@
+Robot = require './robot'
+
+class Adapter
+ # An adapter is a specific interface to a chat source for robots.
+ #
+ # robot - A Robot instance.
+ constructor: (@robot) ->
+ @httpClient = require 'scoped-http-client'
+
+ # Public: Raw method for sending data back to the chat source. Extend this.
+ #
+ # user - A User instance.
+ # strings - One or more Strings for each message to send.
+ send: (user, strings...) ->
+
+ # Public: Raw method for building a reply and sending it back to the chat
+ # source. Extend this.
+ #
+ # user - A User instance.
+ # strings - One or more Strings for each reply to send.
+ reply: (user, strings...) ->
+
+ # Public: Raw method for setting a topic on the chat source. Extend this.
+ #
+ # user - A User instance
+ # strings - One more more Strings to set as the topic.
+ topic: (user, strings...) ->
+
+ # Public: Raw method for invoking the bot to run
+ # Extend this.
+ run: ->
+
+ # Public: Raw method for shutting the bot down.
+ # Extend this.
+ close: ->
+ @robot.brain.close()
+
+ # Public: Dispatch a received message to the robot.
+ #
+ # message - A TextMessage instance of the received message.
+ #
+ # Returns nothing.
+ receive: (message) ->
+ @robot.receive message
+
+ # Public: Get an Array of User objects stored in the brain.
+ users: ->
+ @robot.users
+
+ # Public: Get a User object given a unique identifier
+ userForId: (id, options) ->
+ @robot.userForId id, options
+
+ # Public: Get a User object given a name
+ userForName: (name) ->
+ @robot.userForName name
+
+ # Public: Get all users whose names match fuzzyName. Currently, match
+ # means 'starts with', but this could be extended to match initials,
+ # nicknames, etc.
+ #
+ usersForRawFuzzyName: (fuzzyName) ->
+ @robot.usersForRawFuzzyName fuzzyName
+
+ # Public: If fuzzyName is an exact match for a user, returns an array with
+ # just that user. Otherwise, returns an array of all users for which
+ # fuzzyName is a raw fuzzy match (see usersForRawFuzzyName).
+ #
+ usersForFuzzyName: (fuzzyName) ->
+ @robot.usersForFuzzyName fuzzyName
+
+ # Public: Creates a scoped http client with chainable methods for
+ # modifying the request. This doesn't actually make a request though.
+ # Once your request is assembled, you can call `get()`/`post()`/etc to
+ # send the request.
+ #
+ # url - String URL to access.
+ #
+ # Examples:
+ #
+ # res.http("http://example.com")
+ # # set a single header
+ # .header('Authorization', 'bearer abcdef')
+ #
+ # # set multiple headers
+ # .headers(Authorization: 'bearer abcdef', Accept: 'application/json')
+ #
+ # # add URI query parameters
+ # .query(a: 1, b: 'foo & bar')
+ #
+ # # make the actual request
+ # .get() (err, res, body) ->
+ # console.log body
+ #
+ # # or, you can POST data
+ # .post(data) (err, res, body) ->
+ # console.log body
+ #
+ # Returns a ScopedClient instance.
+ http: (url) ->
+ @httpClient.create(url)
+
+module.exports = Adapter
+
@@ -1,8 +1,10 @@
-Robot = require "../robot"
-HTTPS = require "https"
-EventEmitter = require("events").EventEmitter
+Robot = require '../robot'
+Adapter = require '../adapter'
-class Campfire extends Robot.Adapter
+HTTPS = require 'https'
+EventEmitter = require('events').EventEmitter
+
+class Campfire extends Adapter
send: (user, strings...) ->
if strings.length > 0
@bot.Room(user.room).speak strings.shift(), (err, data) =>
@@ -1,6 +1,7 @@
-Robot = require '../robot'
+Robot = require '../robot'
+Adapter = require '../adapter'
-class Shell extends Robot.Adapter
+class Shell extends Adapter
send: (user, strings...) ->
for str in strings
console.log str
@@ -1,12 +1,13 @@
-Robot = require('hubot').robot()
+Robot = require('hubot').robot()
+Adapter = require('hubot').adapter()
-ImapConnection = require('imap').ImapConnection
-EventEmitter = require("events").EventEmitter
-util = require('util')
+ImapConnection = require('imap').ImapConnection
+EventEmitter = require('events').EventEmitter
+util = require('util')
CRLF = "\r\n"
-class Email extends Robot.Adapter
+class Email extends Adapter
run: ->
self = @
@@ -1,7 +1,9 @@
-Robot = require "../robot"
-flowdock = require "flowdock"
+Robot = require('hubot').robot()
+Adapter = require('hubot').adapter()
-class Flowdock extends Robot.Adapter
+flowdock = require 'flowdock'
+
+class Flowdock extends Adapter
send: (user, strings...) ->
for str in strings
@bot.chatMessage(user.flow.subdomain, user.flow.name, str)
@@ -1,10 +1,11 @@
-Robot = require('hubot').robot()
+Robot = require('hubot').robot()
+Adapter = require('hubot').adapter()
-HTTP = require 'http'
-HTTPS = require 'https'
-URL = require 'url'
+HTTP = require 'http'
+HTTPS = require 'https'
+URL = require 'url'
-class GroupMe extends Robot.Adapter
+class GroupMe extends Adapter
send: (user, strings...) ->
strings.forEach (str) =>
if str.match(/(png|jpg)$/i)
@@ -1,8 +1,9 @@
-Robot = require('hubot').robot()
+Robot = require('hubot').robot()
+Adapter = require('hubot').adapter()
-Xmpp = require 'node-xmpp'
+Xmpp = require 'node-xmpp'
-class Gtalkbot extends Robot
+class Gtalkbot extends Adapter
run: ->
Xmpp.JID.prototype.from = -> @bare().toString()
@@ -1,9 +1,10 @@
-Robot = require('hubot').robot()
+Robot = require('hubot').robot()
+Adapter = require('hubot').adapter()
-HTTPS = require "https"
-Wobot = require("wobot").Bot
+HTTPS = require 'https'
+Wobot = require('wobot').Bot
-class HipChat extends Robot.Adapter
+class HipChat extends Adapter
send: (user, strings...) ->
for str in strings
@bot.message user.reply_to, str
@@ -1,8 +1,9 @@
-Robot = require('hubot').robot()
+Robot = require('hubot').robot()
+Adapter = require('hubot').adapter()
-Irc = require 'irc'
+Irc = require 'irc'
-class IrcBot extends Robot.Adapter
+class IrcBot extends Adapter
send: (user, strings...) ->
for str in strings
if user.room
@@ -1,11 +1,12 @@
Robot = require('hubot').robot()
+Adapter = require('hubot').adapter()
HTTPS = require 'https'
EventEmitter = require('events').EventEmitter
net = require('net')
tls = require('tls')
-class Talker extends Robot.Adapter
+class Talker extends Adapter
send: (user, strings...) ->
strings.forEach (str) =>
@bot.write user.room, {"type": "message", "content": str}
@@ -1,4 +1,5 @@
-Robot = require('robot').robot()
+Robot = require('hubot').robot()
+Adapter = require('hubot').adapter()
HTTPS = require 'https'
EventEmitter = require('events').EventEmitter
@@ -17,7 +18,7 @@ consumer = new oauth.OAuth "https://twitter.com/oauth/request_token",
"",
"HMAC-SHA1"
-class Twitter extends Robot.Adapter
+class Twitter extends Adapter
send: (user, strings...) ->
strings.forEach (str) =>
text = str
@@ -1,8 +1,9 @@
-Robot = require('hubot').robot()
+Robot = require('hubot').robot()
+Adapter = require('hubot').adapter()
-Xmpp = require 'node-xmpp'
+Xmpp = require 'node-xmpp'
-class XmppBot extends Robot.Adapter
+class XmppBot extends Adapter
run: ->
options =
username: process.env.HUBOT_XMPP_USERNAME
Oops, something went wrong.

0 comments on commit b300e83

Please sign in to comment.