Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add ability to listen for topic changes

Adapters must specifically implement receiving TopicMessage when they handle a
changing topic.

Closes #349
  • Loading branch information...
commit 10645fdbd1f8c4e8a2ac8f8ea133bc8e609dfe96 1 parent 49d235e
@tombell tombell authored
View
15 index.coffee
@@ -1,10 +1,10 @@
-User = require './src/user'
-Brain = require './src/brain'
-Robot = require './src/robot'
-Adapter = require './src/adapter'
-Response = require './src/response'
-{Listener,TextListener} = require './src/listener'
-{TextMessage,EnterMessage,LeaveMessage,CatchAllMessage} = require './src/message'
+User = require './src/user'
+Brain = require './src/brain'
+Robot = require './src/robot'
+Adapter = require './src/adapter'
+Response = require './src/response'
+{Listener,TextListener} = require './src/listener'
+{TextMessage,EnterMessage,LeaveMessage,TopicMessage,CatchAllMessage} = require './src/message'
module.exports = {
User
@@ -17,6 +17,7 @@ module.exports = {
TextMessage
EnterMessage
LeaveMessage
+ TopicMessage
CatchAllMessage
}
View
10 src/adapters/campfire.coffee
@@ -1,9 +1,9 @@
HTTPS = require 'https'
{EventEmitter} = require 'events'
-Robot = require '../robot'
-Adapter = require '../adapter'
-{TextMessage,EnterMessage,LeaveMessage} = require '../message'
+Robot = require '../robot'
+Adapter = require '../adapter'
+{TextMessage,EnterMessage,LeaveMessage,TopicMessage} = require '../message'
class Campfire extends Adapter
send: (envelope, strings...) ->
@@ -55,6 +55,10 @@ class Campfire extends Adapter
unless bot.info.id == author.id
self.receive new LeaveMessage(author, null, id)
+ bot.on "TopicChangeMessage", withAuthor (id, created, room, user, body, author) ->
+ unless bot.info.id == author.id
+ self.receive new TopicMessage(author, body, id)
+
bot.Me (err, data) ->
bot.info = data.user
bot.name = bot.info.name
View
8 src/message.coffee
@@ -42,6 +42,13 @@ class EnterMessage extends Message
# id - A String of the message ID.
class LeaveMessage extends Message
+# Represents an incoming topic change notification.
+#
+# user - A User instance for the user who changed the topic.
+# text - A String of the new topic
+# id - A String of the message ID.
+class TopicMessage extends Message
+
class CatchAllMessage extends Message
# Represents a message that no matchers matched.
#
@@ -53,5 +60,6 @@ module.exports = {
TextMessage
EnterMessage
LeaveMessage
+ TopicMessage
CatchAllMessage
}
View
14 src/robot.coffee
@@ -102,13 +102,25 @@ class Robot
leave: (callback) ->
@listeners.push new Listener(@, ((msg) -> msg instanceof LeaveMessage), callback)
+ # Public: Adds a Listener that triggers when anyone changes the topic.
+ #
+ # callback - A Function that is called with a Response object.
+ #
+ # Returns nothing.
+ topic: (callback) ->
+ @listeners.push new Listener(@, ((msg) -> msg instanceof TopicMessage), callback)
+
# Public: Adds a Listener that triggers when no other text matchers match.
#
# callback - A Function that is called with a Response object.
#
# Returns nothing.
catchAll: (callback) ->
- @listeners.push new Listener(@, ((msg) -> msg instanceof CatchAllMessage), ((msg) -> msg.message = msg.message.message; callback msg))
+ @listeners.push new Listener(
+ @,
+ ((msg) -> msg instanceof CatchAllMessage),
+ ((msg) -> msg.message = msg.message.message; callback msg)
+ )
# Public: Passes the given message to any interested Listeners.
#
Please sign in to comment.
Something went wrong with that request. Please try again.