UNPKG

4.38 kBJavaScriptView Raw
1'use strict'
2
3class Response {
4 // Public: Responses are sent to matching listeners. Messages know about the
5 // content and user that made the original message, and how to reply back to
6 // them.
7 //
8 // robot - A Robot instance.
9 // message - A Message instance.
10 // match - A Match object from the successful Regex match.
11 constructor (robot, message, match) {
12 this.robot = robot
13 this.message = message
14 this.match = match
15 this.envelope = {
16 room: this.message.room,
17 user: this.message.user,
18 message: this.message
19 }
20 }
21
22 // Public: Posts a message back to the chat source
23 //
24 // strings - One or more strings to be posted. The order of these strings
25 // should be kept intact.
26 //
27 // Returns nothing.
28 send (/* ...strings */) {
29 const strings = [].slice.call(arguments)
30 this.runWithMiddleware.apply(this, ['send', { plaintext: true }].concat(strings))
31 }
32
33 // Public: Posts an emote back to the chat source
34 //
35 // strings - One or more strings to be posted. The order of these strings
36 // should be kept intact.
37 //
38 // Returns nothing.
39 emote (/* ...strings */) {
40 const strings = [].slice.call(arguments)
41 this.runWithMiddleware.apply(this, ['emote', { plaintext: true }].concat(strings))
42 }
43
44 // Public: Posts a message mentioning the current user.
45 //
46 // strings - One or more strings to be posted. The order of these strings
47 // should be kept intact.
48 //
49 // Returns nothing.
50 reply (/* ...strings */) {
51 const strings = [].slice.call(arguments)
52 this.runWithMiddleware.apply(this, ['reply', { plaintext: true }].concat(strings))
53 }
54
55 // Public: Posts a topic changing message
56 //
57 // strings - One or more strings to set as the topic of the
58 // room the bot is in.
59 //
60 // Returns nothing.
61 topic (/* ...strings */) {
62 const strings = [].slice.call(arguments)
63 this.runWithMiddleware.apply(this, ['topic', { plaintext: true }].concat(strings))
64 }
65
66 // Public: Play a sound in the chat source
67 //
68 // strings - One or more strings to be posted as sounds to play. The order of
69 // these strings should be kept intact.
70 //
71 // Returns nothing
72 play (/* ...strings */) {
73 const strings = [].slice.call(arguments)
74 this.runWithMiddleware.apply(this, ['play'].concat(strings))
75 }
76
77 // Public: Posts a message in an unlogged room
78 //
79 // strings - One or more strings to be posted. The order of these strings
80 // should be kept intact.
81 //
82 // Returns nothing
83 locked (/* ...strings */) {
84 const strings = [].slice.call(arguments)
85 this.runWithMiddleware.apply(this, ['locked', { plaintext: true }].concat(strings))
86 }
87
88 // Private: Call with a method for the given strings using response
89 // middleware.
90 runWithMiddleware (methodName, opts/* , ...strings */) {
91 const self = this
92 const strings = [].slice.call(arguments, 2)
93 const copy = strings.slice(0)
94 let callback
95
96 if (typeof copy[copy.length - 1] === 'function') {
97 callback = copy.pop()
98 }
99
100 const context = {
101 response: this,
102 strings: copy,
103 method: methodName
104 }
105
106 if (opts.plaintext != null) {
107 context.plaintext = true
108 }
109
110 function responseMiddlewareDone () {}
111 function runAdapterSend (_, done) {
112 const result = context.strings
113 if (callback != null) {
114 result.push(callback)
115 }
116 self.robot.adapter[methodName].apply(self.robot.adapter, [self.envelope].concat(result))
117 done()
118 }
119
120 return this.robot.middleware.response.execute(context, runAdapterSend, responseMiddlewareDone)
121 }
122
123 // Public: Picks a random item from the given items.
124 //
125 // items - An Array of items.
126 //
127 // Returns a random item.
128 random (items) {
129 return items[Math.floor(Math.random() * items.length)]
130 }
131
132 // Public: Tell the message to stop dispatching to listeners
133 //
134 // Returns nothing.
135 finish () {
136 this.message.finish()
137 }
138
139 // Public: Creates a scoped http client with chainable methods for
140 // modifying the request. This doesn't actually make a request though.
141 // Once your request is assembled, you can call `get()`/`post()`/etc to
142 // send the request.
143 //
144 // Returns a ScopedClient instance.
145 http (url, options) {
146 return this.robot.http(url, options)
147 }
148}
149
150module.exports = Response