-
Notifications
You must be signed in to change notification settings - Fork 1
/
conversation.coffee
69 lines (59 loc) · 1.77 KB
/
conversation.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Description:
# Extends robot adding conversation features
#
# Dependencies:
# None
#
# Configuration:
# None
#
# Commands:
# None
#
# Author:
# pescuma
module.exports = (robot) ->
robot.eatListeners = {}
# Public: Adds a Listener that receives the next message from the user and av
# further processing of it.
#
# user - The user name.
# callback - A Function that is called with a Response object. msg.match[1] w
# contain the message text without the bot name
#
# Returns nothing.
robot.eatOneResponse = (user, callback) ->
robot.eatListeners[user.id] = new Listener(robot, callback)
# Change default receive command, addind processing of eatListeners
robot.origReceive = robot.receive
robot.receive = (message) ->
if message.user? and robot.eatListeners[message.user.id]?
lst = robot.eatListeners[message.user.id]
delete robot.eatListeners[message.user.id]
if lst.call message
return
# Put back to process next message
robot.eatListeners[message.user.id] = lst
robot.origReceive(message)
# Public: Waits for the next message from the current user.
#
# callback - Called with the user response
#
# Returns nothing.
robot.Response.prototype.waitResponse = (callback) ->
robot.eatOneResponse this.message.user, callback
class Listener
constructor: (@robot, @callback) ->
if robot.enableSlash
@regex = new RegExp("^(?:\/|#{robot.name}:?)\\s*(.*?)\\s*$", 'i')
else
@regex = new RegExp("^#{robot.name}:?\\s*(.*?)\\s*$", 'i')
@matcher = (message) =>
if message.text?
message.text.match @regex
call: (message) =>
if match = @matcher message
@callback new @robot.Response(@robot, message, match)
return true
else
return false