Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 501 lines (438 sloc) 15.258 kb
a4d606c @technoweenie remove redis brain (it will return in hubot-scripts)
technoweenie authored
1 Fs = require 'fs'
2 Url = require 'url'
3 Path = require 'path'
4 EventEmitter = require('events').EventEmitter
b253e94 @atmos fuck a color history
atmos authored
5
6 class Robot
3726dbe @technoweenie document the core robot methods better
technoweenie authored
7 # Robots receive messages from a chat source (Campfire, irc, etc), and
8 # dispatch them to matching listeners.
9 #
10 # path - String directory full of Hubot scripts to load.
a3a58c0 @tombell Attempt at updating tests for new adapter class
tombell authored
11 constructor: (adapterPath, adapter, name = "Hubot") ->
be6b76b @atmos enable '/' instead of hubot as a cli opt
atmos authored
12 @name = name
a4d606c @technoweenie remove redis brain (it will return in hubot-scripts)
technoweenie authored
13 @brain = new Robot.Brain
d8c60d4 @atmos Merge branch 'master' into core-cleanup
atmos authored
14 @alias = false
15 @adapter = null
be6b76b @atmos enable '/' instead of hubot as a cli opt
atmos authored
16 @commands = []
17 @Response = Robot.Response
18 @listeners = []
19 @loadPaths = []
20 @enableSlash = false
e2f7b6f @tombell Update loading path condition
tombell authored
21
d9497ad @tombell update danger adapter
tombell authored
22 @loadAdapter adapterPath, adapter if adapter?
792567b @tombell Update Campfire, IRC and Shell adapters
tombell authored
23
3726dbe @technoweenie document the core robot methods better
technoweenie authored
24 # Public: Adds a Listener that attempts to match incoming messages based on
25 # a Regex.
26 #
27 # regex - A Regex that determines if the callback should be called.
28 # callback - A Function that is called with a Response object.
29 #
30 # Returns nothing.
b253e94 @atmos fuck a color history
atmos authored
31 hear: (regex, callback) ->
9968309 @eventualbuddha Allow handling Campfire enter/leave messages.
eventualbuddha authored
32 @listeners.push new TextListener(@, regex, callback)
b253e94 @atmos fuck a color history
atmos authored
33
bbf0c75 @tombell Update formatting
tombell authored
34 # Public: Adds a Listener that attempts to match incoming messages directed
35 # at the robot based on a Regex. All regexes treat patterns like they begin
36 # with a '^'
be5fce6 @atmos hubot listen or be addressed in chat. fixes #4
atmos authored
37 #
38 # regex - A Regex that determines if the callback should be called.
39 # callback - A Function that is called with a Response object.
40 #
41 # Returns nothing.
42 respond: (regex, callback) ->
43 re = regex.toString().split("/")
18beda1 @atmos handle slashes in regexes properly
atmos authored
44 re.shift() # remove empty first item
45 modifiers = re.pop() # pop off modifiers
46
6780d7a @tombell Update equality checks to use 'is'
tombell authored
47 if re[0] and re[0][0] is "^"
ce149d9 @atmos warn about anchors not working with respond, fixes #10
atmos authored
48 console.log "\nWARNING: Anchors don't work well with respond, perhaps you want to use 'hear'"
49 console.log "WARNING: The regex in question was #{regex.toString()}\n"
50
18beda1 @atmos handle slashes in regexes properly
atmos authored
51 pattern = re.join("/") # combine the pattern back again
a39b823 @Vrtak-CZ Add --alias (--enable-slash is deprecated)
Vrtak-CZ authored
52 if @alias
53 alias = @alias.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&") # escape alias for regexp
54 newRegex = new RegExp("^(?:#{alias}|#{@name}[:,]?)\\s*(?:#{pattern})", modifiers)
be6b76b @atmos enable '/' instead of hubot as a cli opt
atmos authored
55 else
fc33244 @lautis Allow comma when addressing Hubot
lautis authored
56 newRegex = new RegExp("^#{@name}[:,]?\\s*(?:#{pattern})", modifiers)
18beda1 @atmos handle slashes in regexes properly
atmos authored
57
2b432ff @atmos make hubot easier for first timers
atmos authored
58 console.log newRegex.toString()
9968309 @eventualbuddha Allow handling Campfire enter/leave messages.
eventualbuddha authored
59 @listeners.push new TextListener(@, newRegex, callback)
60
61 # Public: Adds a Listener that triggers when anyone enters the room.
62 #
63 # callback - A Function that is called with a Response object.
64 #
65 # Returns nothing.
66 enter: (callback) ->
67 @listeners.push new Listener(@, ((msg) -> msg instanceof Robot.EnterMessage), callback)
68
69 # Public: Adds a Listener that triggers when anyone leaves the room.
70 #
71 # callback - A Function that is called with a Response object.
72 #
73 # Returns nothing.
74 leave: (callback) ->
75 @listeners.push new Listener(@, ((msg) -> msg instanceof Robot.LeaveMessage), callback)
be5fce6 @atmos hubot listen or be addressed in chat. fixes #4
atmos authored
76
3726dbe @technoweenie document the core robot methods better
technoweenie authored
77 # Public: Passes the given message to any interested Listeners.
78 #
79 # message - A Robot.Message instance.
80 #
81 # Returns nothing.
b253e94 @atmos fuck a color history
atmos authored
82 receive: (message) ->
56a1115 @tombell Update forEach calls to be comprehensions
tombell authored
83 for lst in @listeners
f37292a @eventualbuddha Give some indication that a script listener threw an exception.
eventualbuddha authored
84 try
85 lst.call message
86 catch ex
87 console.log "error while calling listener: #{ex}"
b253e94 @atmos fuck a color history
atmos authored
88
3726dbe @technoweenie document the core robot methods better
technoweenie authored
89 # Public: Loads every script in the given path.
90 #
91 # path - A String path on the filesystem.
92 #
93 # Returns nothing.
b253e94 @atmos fuck a color history
atmos authored
94 load: (path) ->
a5b8d5d @atmos make hubot-scripts development easier
atmos authored
95 Path.exists path, (exists) =>
96 if exists
97 @loadPaths.push path
df4912b @tombell Update to use for x in y instead of forEach
tombell authored
98 for file in Fs.readdirSync(path)
a5b8d5d @atmos make hubot-scripts development easier
atmos authored
99 @loadFile path, file
100
101 # Public: Loads a file in path
102 #
103 # path - A String path on the filesystem.
104 # file - A String filename in path on the filesystem.
105 #
106 # Returns nothing.
107 loadFile: (path, file) ->
108 ext = Path.extname file
109 full = Path.join path, Path.basename(file, ext)
6780d7a @tombell Update equality checks to use 'is'
tombell authored
110 if ext is '.coffee' or ext is '.js'
a5b8d5d @atmos make hubot-scripts development easier
atmos authored
111 require(full) @
845e37e @atmos parse help on a per-script basis
atmos authored
112 @parseHelp "#{path}/#{file}"
113
17c81a4 @tombell Update adapter loading code
tombell authored
114 # Load the adapter Hubot is going to use.
115 #
116 # path - A String of the path to adapter if local.
117 # adapter - A String of the adapter name to use.
118 #
119 # Returns nothing.
91d45e3 @tombell Begin updating the core moving adapters out
tombell authored
120 loadAdapter: (path, adapter) ->
17c81a4 @tombell Update adapter loading code
tombell authored
121 try
122 path = if adapter in [ "campfire", "shell" ]
123 "#{path}/#{adapter}"
124 else
125 "hubot-#{adapter}"
126
127 @adapter = require("#{path}").use(@)
128 catch err
129 console.log "Can't load adapter '#{adapter}', try installing the package"
91d45e3 @tombell Begin updating the core moving adapters out
tombell authored
130
845e37e @atmos parse help on a per-script basis
atmos authored
131 # Public: Help Commands for Running Scripts
132 #
133 # Returns an array of help commands for running scripts
134 helpCommands: () ->
135 @commands.sort()
136
137 # Private: load help info from a loaded script
138 #
139 # path - The path to the file on disk
140 #
141 # Returns nothing
142 parseHelp: (path) ->
143 Fs.readFile path, "utf-8", (err, body) =>
144 throw err if err
145 for i, line of body.split("\n")
1d1b69d @jasford Updated the help generator to allow for .js scripts to include help l…
jasford authored
146 break if !(line[0] == '#' or line.substr(0, 2) == '//')
845e37e @atmos parse help on a per-script basis
atmos authored
147 continue if !line.match('-')
148 @commands.push line[2..line.length]
b253e94 @atmos fuck a color history
atmos authored
149
3a9d1b5 @tombell Add missing tomdoc comments to functions
tombell authored
150 # Public: Get an Array of User objects stored in the brain.
792567b @tombell Update Campfire, IRC and Shell adapters
tombell authored
151 users: ->
152 @brain.data.users
153
432e818 @tombell Update to fix the robot name not getting passed through
tombell authored
154 # Public: Get a User object given a unique identifier.
792567b @tombell Update Campfire, IRC and Shell adapters
tombell authored
155 userForId: (id, options) ->
156 user = @brain.data.users[id]
157 unless user
158 user = new Robot.User id, options
159 @brain.data.users[id] = user
160 user
161
432e818 @tombell Update to fix the robot name not getting passed through
tombell authored
162 # Public: Get a User object given a name.
792567b @tombell Update Campfire, IRC and Shell adapters
tombell authored
163 userForName: (name) ->
164 result = null
165 lowerName = name.toLowerCase()
166 for k of (@brain.data.users or { })
167 if @brain.data.users[k]['name'].toLowerCase() is lowerName
168 result = @brain.data.users[k]
169 result
170
52bcdd3 @russelldavis Move fuzzy matching to its own function.
russelldavis authored
171 # Public: Get all users whose names match fuzzyName. Currently, match
172 # means 'starts with', but this could be extended to match initials,
173 # nicknames, etc.
174 #
175 usersForRawFuzzyName: (fuzzyName) ->
176 lowerFuzzyName = fuzzyName.toLowerCase()
177 user for key, user of (@brain.data.users or {}) when (
178 user.name.toLowerCase().lastIndexOf(lowerFuzzyName, 0) == 0)
179
180 # Public: If fuzzyName is an exact match for a user, returns an array with
181 # just that user. Otherwise, returns an array of all users for which
182 # fuzzyName is a raw fuzzy match (see usersForRawFuzzyName).
183 #
184 usersForFuzzyName: (fuzzyName) ->
f3d86be @tombell Merge branch 'matchPartialNames' of https://github.com/russelldavis/h…
tombell authored
185 matchedUsers = @usersForRawFuzzyName(fuzzyName)
52bcdd3 @russelldavis Move fuzzy matching to its own function.
russelldavis authored
186 lowerFuzzyName = fuzzyName.toLowerCase()
187 # We can scan matchedUsers rather than all users since usersForRawFuzzyName
188 # will include exact matches
189 for user in matchedUsers
190 return [user] if user.name.toLowerCase() is lowerFuzzyName
191
192 matchedUsers
193
792567b @tombell Update Campfire, IRC and Shell adapters
tombell authored
194 run: ->
195 @adapter.run()
196
197 class Robot.Adapter
3a9d1b5 @tombell Add missing tomdoc comments to functions
tombell authored
198 # An adapter is a specific interface to a chat source for robots.
199 #
200 # robot - A Robot instance.
792567b @tombell Update Campfire, IRC and Shell adapters
tombell authored
201 constructor: (@robot) ->
202
3726dbe @technoweenie document the core robot methods better
technoweenie authored
203 # Public: Raw method for sending data back to the chat source. Extend this.
204 #
205 # user - A Robot.User instance.
206 # strings - One or more Strings for each message to send.
b253e94 @atmos fuck a color history
atmos authored
207 send: (user, strings...) ->
208
3726dbe @technoweenie document the core robot methods better
technoweenie authored
209 # Public: Raw method for building a reply and sending it back to the chat
210 # source. Extend this.
211 #
212 # user - A Robot.User instance.
213 # strings - One or more Strings for each reply to send.
b253e94 @atmos fuck a color history
atmos authored
214 reply: (user, strings...) ->
215
7c9e8a8 @markstory Adding topic support.
markstory authored
216 # Public: Raw method for setting a topic on the chat source. Extend this.
217 #
218 # user - A Robot.User instance
219 # strings - One more more Strings to set as the topic.
220 topic: (user, strings...) ->
221
3726dbe @technoweenie document the core robot methods better
technoweenie authored
222 # Public: Raw method for invoking the bot to run
b253e94 @atmos fuck a color history
atmos authored
223 # Extend this.
3726dbe @technoweenie document the core robot methods better
technoweenie authored
224 run: ->
b253e94 @atmos fuck a color history
atmos authored
225
f43d021 @technoweenie get the tests actually running
technoweenie authored
226 # Public: Raw method for shutting the bot down.
227 # Extend this.
228 close: ->
792567b @tombell Update Campfire, IRC and Shell adapters
tombell authored
229 @robot.brain.close()
f43d021 @technoweenie get the tests actually running
technoweenie authored
230
3a9d1b5 @tombell Add missing tomdoc comments to functions
tombell authored
231 # Public: Dispatch a received message to the robot.
232 #
233 # message - A TextMessage instance of the received message.
234 #
235 # Returns nothing.
792567b @tombell Update Campfire, IRC and Shell adapters
tombell authored
236 receive: (message) ->
237 @robot.receive message
238
3a9d1b5 @tombell Add missing tomdoc comments to functions
tombell authored
239 # Public: Get an Array of User objects stored in the brain.
792567b @tombell Update Campfire, IRC and Shell adapters
tombell authored
240 users: ->
241 @robot.users
1875856 @atmos robot brain in redis
atmos authored
242
243 # Public: Get a User object given a unique identifier
244 userForId: (id, options) ->
792567b @tombell Update Campfire, IRC and Shell adapters
tombell authored
245 @robot.userForId id, options
1875856 @atmos robot brain in redis
atmos authored
246
247 # Public: Get a User object given a name
248 userForName: (name) ->
792567b @tombell Update Campfire, IRC and Shell adapters
tombell authored
249 @robot.userForName name
250
f3d86be @tombell Merge branch 'matchPartialNames' of https://github.com/russelldavis/h…
tombell authored
251 # Public: Get all users whose names match fuzzyName. Currently, match
252 # means 'starts with', but this could be extended to match initials,
253 # nicknames, etc.
254 #
255 usersForRawFuzzyName: (fuzzyName) ->
256 @robot.usersForRawFuzzyName fuzzyName
257
258 # Public: If fuzzyName is an exact match for a user, returns an array with
259 # just that user. Otherwise, returns an array of all users for which
260 # fuzzyName is a raw fuzzy match (see usersForRawFuzzyName).
261 #
262 usersForFuzzyName: (fuzzyName) ->
263 @robot.usersForFuzzyName fuzzyName
1875856 @atmos robot brain in redis
atmos authored
264
23e72da @tombell Small refactor of twilio adapter
tombell authored
265 # Public: Creates a scoped http client with chainable methods for
266 # modifying the request. This doesn't actually make a request though.
267 # Once your request is assembled, you can call `get()`/`post()`/etc to
268 # send the request.
269 #
270 # url - String URL to access.
271 #
272 # Examples:
273 #
274 # res.http("http://example.com")
275 # # set a single header
276 # .header('Authorization', 'bearer abcdef')
277 #
278 # # set multiple headers
279 # .headers(Authorization: 'bearer abcdef', Accept: 'application/json')
280 #
281 # # add URI query parameters
282 # .query(a: 1, b: 'foo & bar')
283 #
284 # # make the actual request
285 # .get() (err, res, body) ->
286 # console.log body
287 #
288 # # or, you can POST data
289 # .post(data) (err, res, body) ->
290 # console.log body
291 #
292 # Returns a ScopedClient instance.
293 http: (url) ->
294 @httpClient.create(url)
295
f3d86be @tombell Merge branch 'matchPartialNames' of https://github.com/russelldavis/h…
tombell authored
296
53d76c3 @technoweenie add real user/message objects
technoweenie authored
297 class Robot.User
3726dbe @technoweenie document the core robot methods better
technoweenie authored
298 # Represents a participating user in the chat.
299 #
300 # id - A unique ID for the user.
301 # options - An optional Hash of key, value pairs for this user.
1875856 @atmos robot brain in redis
atmos authored
302 constructor: (@id, options = { }) ->
303 for k of (options or { })
304 @[k] = options[k]
305
f43d021 @technoweenie get the tests actually running
technoweenie authored
306 # http://www.the-isb.com/images/Nextwave-Aaron01.jpg
a4d606c @technoweenie remove redis brain (it will return in hubot-scripts)
technoweenie authored
307 class Robot.Brain extends EventEmitter
1875856 @atmos robot brain in redis
atmos authored
308 # Represents somewhat persistent storage for the robot.
309 #
8354140 @technoweenie Robot.RedisBrain extends Robot.Brain
technoweenie authored
310 # Returns a new Brain with no external storage. Extend this!
311 constructor: () ->
312 @data =
313 users: { }
314
2230dfd @technoweenie allow configurable save interval for the brain
technoweenie authored
315 @resetSaveInterval 5
a4d606c @technoweenie remove redis brain (it will return in hubot-scripts)
technoweenie authored
316
3a9d1b5 @tombell Add missing tomdoc comments to functions
tombell authored
317 # Emits the 'save' event so that 'brain' scripts can handle persisting.
318 #
319 # Returns nothing.
a4d606c @technoweenie remove redis brain (it will return in hubot-scripts)
technoweenie authored
320 save: ->
321 @emit 'save', @data
322
3a9d1b5 @tombell Add missing tomdoc comments to functions
tombell authored
323 # Emits the 'close' event so that 'brain' scripts can handle closing.
324 #
325 # Returns nothing.
8354140 @technoweenie Robot.RedisBrain extends Robot.Brain
technoweenie authored
326 close: ->
a4d606c @technoweenie remove redis brain (it will return in hubot-scripts)
technoweenie authored
327 clearInterval @saveInterval
328 @save()
329 @emit 'close'
8354140 @technoweenie Robot.RedisBrain extends Robot.Brain
technoweenie authored
330
3a9d1b5 @tombell Add missing tomdoc comments to functions
tombell authored
331 # Reset the interval between save function calls.
332 #
333 # seconds - An Integer of seconds between saves.
334 #
335 # Returns nothing.
2230dfd @technoweenie allow configurable save interval for the brain
technoweenie authored
336 resetSaveInterval: (seconds) ->
337 clearInterval @saveInterval if @saveInterval
338 @saveInterval = setInterval =>
339 @save()
340 , seconds * 1000
341
8354140 @technoweenie Robot.RedisBrain extends Robot.Brain
technoweenie authored
342 # Merge keys loaded from a DB against the in memory representation
343 #
344 # Returns nothing
345 #
346 # Caveats: Deeply nested structures don't merge well
347 mergeData: (data) ->
348 for k of (data or { })
349 @data[k] = data[k]
2b99aa2 @atmos whitespace
atmos authored
350
95e85fd @codec Also emit the @data
codec authored
351 @emit 'loaded', @data
8354140 @technoweenie Robot.RedisBrain extends Robot.Brain
technoweenie authored
352
53d76c3 @technoweenie add real user/message objects
technoweenie authored
353 class Robot.Message
3726dbe @technoweenie document the core robot methods better
technoweenie authored
354 # Represents an incoming message from the chat.
355 #
356 # user - A Robot.User instance that sent the message.
9968309 @eventualbuddha Allow handling Campfire enter/leave messages.
eventualbuddha authored
357 constructor: (@user) ->
358
359 class Robot.TextMessage extends Robot.Message
360 # Represents an incoming message from the chat.
361 #
362 # user - A Robot.User instance that sent the message.
3726dbe @technoweenie document the core robot methods better
technoweenie authored
363 # text - The String message contents.
364 constructor: (@user, @text) ->
9968309 @eventualbuddha Allow handling Campfire enter/leave messages.
eventualbuddha authored
365 super @user
53d76c3 @technoweenie add real user/message objects
technoweenie authored
366
3726dbe @technoweenie document the core robot methods better
technoweenie authored
367 # Determines if the message matches the given regex.
368 #
369 # regex - The Regex to check.
370 #
371 # Returns a Match object or null.
53d76c3 @technoweenie add real user/message objects
technoweenie authored
372 match: (regex) ->
373 @text.match regex
374
9968309 @eventualbuddha Allow handling Campfire enter/leave messages.
eventualbuddha authored
375 # Represents an incoming user entrance notification.
376 #
377 # user - A Robot.User instance for the user who entered.
378 class Robot.EnterMessage extends Robot.Message
379
380 # Represents an incoming user exit notification.
381 #
382 # user - A Robot.User instance for the user who left.
383 class Robot.LeaveMessage extends Robot.Message
384
b253e94 @atmos fuck a color history
atmos authored
385 class Listener
bbf0c75 @tombell Update formatting
tombell authored
386 # Listeners receive every message from the chat source and decide if they
387 # want to act on it.
3726dbe @technoweenie document the core robot methods better
technoweenie authored
388 #
389 # robot - The current Robot instance.
9968309 @eventualbuddha Allow handling Campfire enter/leave messages.
eventualbuddha authored
390 # matcher - The Function that determines if this listener should trigger the
3726dbe @technoweenie document the core robot methods better
technoweenie authored
391 # callback.
392 # callback - The Function that is triggered if the incoming message matches.
9968309 @eventualbuddha Allow handling Campfire enter/leave messages.
eventualbuddha authored
393 constructor: (@robot, @matcher, @callback) ->
b253e94 @atmos fuck a color history
atmos authored
394
3726dbe @technoweenie document the core robot methods better
technoweenie authored
395 # Public: Determines if the listener likes the content of the message. If
396 # so, a Response built from the given Message is passed to the Listener
397 # callback.
398 #
399 # message - a Robot.Message instance.
400 #
401 # Returns nothing.
b253e94 @atmos fuck a color history
atmos authored
402 call: (message) ->
9968309 @eventualbuddha Allow handling Campfire enter/leave messages.
eventualbuddha authored
403 if match = @matcher message
1e4ae57 @technoweenie google images tests
technoweenie authored
404 @callback new @robot.Response(@robot, message, match)
b253e94 @atmos fuck a color history
atmos authored
405
9968309 @eventualbuddha Allow handling Campfire enter/leave messages.
eventualbuddha authored
406 class TextListener extends Listener
407 # TextListeners receive every message from the chat source and decide if they want
408 # to act on it.
409 #
410 # robot - The current Robot instance.
411 # regex - The Regex that determines if this listener should trigger the
412 # callback.
413 # callback - The Function that is triggered if the incoming message matches.
414 constructor: (@robot, @regex, @callback) ->
415 @matcher = (message) =>
416 if message instanceof Robot.TextMessage
417 message.match @regex
418
1e4ae57 @technoweenie google images tests
technoweenie authored
419 class Robot.Response
3726dbe @technoweenie document the core robot methods better
technoweenie authored
420 # Public: Responses are sent to matching listeners. Messages know about the
421 # content and user that made the original message, and how to reply back to
422 # them.
423 #
424 # robot - The current Robot instance.
425 # message - The current Robot.Message instance.
426 # match - The Match object from the successful Regex match.
b253e94 @atmos fuck a color history
atmos authored
427 constructor: (@robot, @message, @match) ->
428
3726dbe @technoweenie document the core robot methods better
technoweenie authored
429 # Public: Posts a message back to the chat source
b253e94 @atmos fuck a color history
atmos authored
430 #
431 # strings - One or more strings to be posted. The order of these strings
432 # should be kept intact.
433 #
434 # Returns nothing.
435 send: (strings...) ->
792567b @tombell Update Campfire, IRC and Shell adapters
tombell authored
436 @robot.adapter.send @message.user, strings...
b253e94 @atmos fuck a color history
atmos authored
437
7c9e8a8 @markstory Adding topic support.
markstory authored
438 # Public: Posts a topic changing message
439 #
2b99aa2 @atmos whitespace
atmos authored
440 # strings - One or more strings to set as the topic of the
7c9e8a8 @markstory Adding topic support.
markstory authored
441 # room the bot is in.
442 #
443 # Returns nothing.
444 topic: (strings...) ->
792567b @tombell Update Campfire, IRC and Shell adapters
tombell authored
445 @robot.adapter.topic @message.user, strings...
7c9e8a8 @markstory Adding topic support.
markstory authored
446
3726dbe @technoweenie document the core robot methods better
technoweenie authored
447 # Public: Posts a message mentioning the current user.
b253e94 @atmos fuck a color history
atmos authored
448 #
449 # strings - One or more strings to be posted. The order of these strings
450 # should be kept intact.
451 #
452 # Returns nothing.
453 reply: (strings...) ->
792567b @tombell Update Campfire, IRC and Shell adapters
tombell authored
454 @robot.adapter.reply @message.user, strings...
b253e94 @atmos fuck a color history
atmos authored
455
3726dbe @technoweenie document the core robot methods better
technoweenie authored
456 # Public: Picks a random item from the given items.
457 #
458 # items - An Array of items (usually Strings).
459 #
460 # Returns a random item.
b253e94 @atmos fuck a color history
atmos authored
461 random: (items) ->
462 items[ Math.floor(Math.random() * items.length) ]
463
158e963 @technoweenie use scoped-http-client
technoweenie authored
464 # Public: Creates a scoped http client with chainable methods for
465 # modifying the request. This doesn't actually make a request though.
466 # Once your request is assembled, you can call `get()`/`post()`/etc to
467 # send the request.
468 #
469 # url - String URL to access.
470 #
471 # Examples:
f796552 @atmos cleanup shitty variable usage
atmos authored
472 #
158e963 @technoweenie use scoped-http-client
technoweenie authored
473 # res.http("http://example.com")
474 # # set a single header
475 # .header('Authorization', 'bearer abcdef')
476 #
477 # # set multiple headers
478 # .headers(Authorization: 'bearer abcdef', Accept: 'application/json')
479 #
480 # # add URI query parameters
481 # .query(a: 1, b: 'foo & bar')
482 #
483 # # make the actual request
484 # .get() (err, res, body) ->
485 # console.log body
486 #
487 # # or, you can POST data
488 # .post(data) (err, res, body) ->
489 # console.log body
490 #
491 # Returns a ScopedClient instance.
492 http: (url) ->
493 @httpClient.create(url)
494
23e72da @tombell Small refactor of twilio adapter
tombell authored
495 HttpClient = require 'scoped-http-client'
496
497 Robot.Response::httpClient = HttpClient
498 Robot::httpClient = HttpClient
b253e94 @atmos fuck a color history
atmos authored
499
500 module.exports = Robot
Something went wrong with that request. Please try again.