forked from github/hubot-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
advice.coffee
47 lines (40 loc) · 1.21 KB
/
advice.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
# Description:
# Get free advice from http://adviceslip.com/
#
# Dependencies:
# None
#
# Configuration:
# None
#
# Commands:
# hubot what should I do about (.*)
# hubot what do you think about (.*)
# hubot how do you handle (.*)
# hubot I need some advice
#
# Author:
# pengwynn
#
getAdvice = (msg, query) ->
msg.http("http://api.adviceslip.com/advice/search/#{query}")
.get() (err, res, body) ->
results = JSON.parse body
if results.message? then randomAdvice(msg) else msg.send(msg.random(results.slips).advice)
randomAdvice = (msg) ->
msg.http("http://api.adviceslip.com/advice")
.get() (err, res, body) ->
results = JSON.parse body
advice = if err then "You're on your own, bud" else results.slip.advice
msg.send advice
module.exports = (robot) ->
robot.respond /what (do you|should I) do (when|about) (.*)/i, (msg) ->
getAdvice msg, msg.match[3]
robot.respond /how do you handle (.*)/i, (msg) ->
getAdvice msg, msg.match[1]
robot.respond /(.*) some advice about (.*)/i, (msg) ->
getAdvice msg, msg.match[2]
robot.respond /(.*) think about (.*)/i, (msg) ->
getAdvice msg, msg.match[2]
robot.respond /(.*) advice$/i, (msg) ->
randomAdvice(msg)