Permalink
Browse files

messageRoom function for the Robot class

This method is handy for sending a message to a room that is triggered
by something other than a received message like inbound HTTP requests.
  • Loading branch information...
1 parent ac7f5ac commit af6818603c748fe9d25437efa9e91d6ba63d18b9 @ejfinneran ejfinneran committed Dec 14, 2011
Showing with 20 additions and 3 deletions.
  1. +9 −0 src/robot.coffee
  2. +8 −1 test/robot_test.coffee
  3. +3 −2 test/tests.coffee
View
@@ -175,6 +175,15 @@ class Robot
send: (user, strings...) ->
@adapter.send user, strings...
+ # Public: A helper send function to message a room that the robot is in
+ #
+ # room - String designating the room to message
+ # strings - One or more Strings for each message to send.
+ messageRoom: (room, strings...) ->
+ user = @userForId @id, { room: room }
+ @adapter.send user, strings...
+
+
# Public: A helper reply function which delegates to the adapter's reply
# function.
#
@@ -22,9 +22,16 @@ server.listen 9001, ->
assert.equal 3, helper.sent.length
assert.ok helper.sent[2].match(/^(1|2)$/)
+ # Test that when we message a room, the 'recipient' is the robot user and the room attribute is set properly
+ helper.messageRoom "chat@example.com", "Hello room"
+ assert.equal 4, helper.sent.length
+ assert.equal "chat@example.com", helper.recipients[3].room
+ assert.equal helper.id, helper.recipients[3].id
+ assert.equal "Hello room", helper.sent[3]
+
# set a callback for when the next message is replied to
helper.cb = (msg) ->
- assert.equal 4, helper.sent.length
+ assert.equal 5, helper.sent.length
assert.equal 'fetched', msg
helper.close()
server.close()
View
@@ -35,10 +35,10 @@ class Helper extends Robot
constructor: (scriptPath) ->
super null, null, 'helper'
@load scriptPath
-
+ @id = 1
@Response = Helper.Response
@sent = []
-
+ @recipients = []
@adapter = new Danger @
stop: ->
@@ -50,6 +50,7 @@ class Helper extends Robot
class Danger extends Adapter
send: (user, strings...) ->
@robot.sent.push str for str in strings
+ @robot.recipients.push user for str in strings
@cb? strings...
reply: (user, strings...) ->

0 comments on commit af68186

Please sign in to comment.