index.coffee | |
---|---|
The main | {EventEmitter} = require 'events'
http = require 'http' |
Factory method for creating a new | exports.createBot = (name) ->
new Bot name |
The | exports.Bot = class Bot extends EventEmitter |
Creates a new | constructor: (@name) ->
@handlers = []
@interfaces = []
@descriptions = {} |
Adds a description of a piece of the robots functionality, this is used
for the robots builtin
Example | desc: (phrase, functionality) =>
@descriptions[phrase] = functionality |
Add a
Example | hear: (pattern, callback) =>
@handlers.push [pattern, callback] |
Add an interface to the robot. This is allows the 'bot to communicate
with the outside world. See the | use: (interface) ->
@interfaces.push interface
interface.on 'message', @dispatch |
Dispatches an incoming message to any handlers that match the message
body. This can be used to fake message to the bot, useful for testing
the bot. Takes a | dispatch: (message) =>
for pair in @handlers
[ pattern, handler ] = pair
handler.call(@, message) if message.match = message.body.match(pattern) |
Start the bot up, calls listen on the registered interfaces. This registers
the help phrase that the bot will always repond to. Emits a | start: ->
@hear /help/, @help
@interfaces.forEach (i) -> i.listen()
@emit 'start' |
Reset the bot's | reset: (callback) ->
@handlers = []
@descriptions = []
callback?() |
Helper method for making a | get: (path, body, callback) ->
@request('GET', path, body, callback) |
Helper method for making a | post: (path, body, callback) ->
@request('POST', path, body, callback) |
Handler for the default help action, gathers all of the registered descriptions and sends a message describing each action. | help: (message) ->
if Object.keys(@descriptions).length is 0
return message.say "I do not have any actions yet."
message.say "I listen for the following…", =>
for phrase, functionality of @descriptions
if functionality
output = phrase + ": " + functionality
else
output = phrase
message.say output |
Helper to make http requests, tries to automatically handle JSON input and output. | request: (method, path, body, callback) ->
if match = path.match(/^(https?):\/\/([^\/]+?)(\/.+)/)
headers = { Host: match[2], 'Content-Type': 'application/json', 'User-Agent': @name }
port = if match[1] == 'https' then 443 else 80
client = http.createClient(port, match[2], port == 443)
path = match[3]
if typeof(body) is 'function' and not callback
callback = body
body = null
if method is 'POST' and body
body = JSON.stringify body if typeof body isnt 'string'
headers['Content-Length'] = body.length
req = client.request(method, path, headers)
req.on 'response', (response) ->
if response.statusCode is 200
data = ''
response.setEncoding('utf8')
response.on 'data', (chunk) ->
data += chunk
response.on 'end', ->
if callback
try
body = JSON.parse(data)
catch e
body = data
callback body
else if response.statusCode is 302
request(method, path, body, callback)
else
console.log "#{response.statusCode}: #{path}"
response.setEncoding('utf8')
response.on 'data', (chunk) ->
console.log chunk.toString()
process.exit(1)
req.write(body) if method is 'POST' and body
req.end() |
Command line interface. | Cli = require './cli'
exports.cli = ->
new Cli |
Campfire interface. | Campfire = require './campfire'
exports.campfire = (args...) ->
new Campfire args... |
XMPP interface. | Xmpp = require './xmpp'
exports.xmpp = (args...) ->
new Xmpp args...
|