-
Notifications
You must be signed in to change notification settings - Fork 0
/
joke-delay-punchline.js
76 lines (67 loc) · 1.97 KB
/
joke-delay-punchline.js
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
70
71
72
73
74
75
76
/*
Description:
Replies to a request for a joke and has a delay for the punchline
Commands:
hubot Tell me a joke
hubot know any good jokes?
Author:
theycallmesef
Environment Variables:
HUBOT_JOKES_URL - (Optional) Url to json file with jokes
*/
var jokejson = require('../lib/jokes.json');
var jokesdataurl = process.env.HUBOT_JOKES_URL;
module.exports = (robot) => {
// ---- Return json from web request ----
// "url" is passed in, "callback" is the return
function GetUrlData(url, callback) {
let jdata
robot.logger.info("joke-delay-punchline: Gathering json data from form api");
// Get json of form submissions
robot.http(url)
.header('Accept', 'application/json')
.get()((err, res, body) => {
if (err) {
// send error message to room
robot.logger.error(`joke-delay-punchline: Error connecting: ${res}`);
return;
} else {
try {
jdata = JSON.parse(body);
} catch(e) {
robot.logger.error(`joke-delay-punchline: Error retreving data: ${e}`);
return;
};
// send results to return function
robot.logger.info(`joke-delay-punchline: Data retrived from API`);
callback(jdata);
};
});
};
function TellJoke(msg, jokedata) {
let rjoke, message
if (jokedata.acknowledge) {
msg.send(msg.random(jokedata.acknowledge));
};
rjoke = jokedata.jokes[Math.floor(Math.random() * jokedata.jokes.length)];
msg.send(rjoke.joke + "...");
message = "..." + rjoke.punchline
if (jokedata.laugh){
message += " " + msg.random(jokedata.laugh)
};
setTimeout(() => {
msg.send(message);
}, 6000);
};
robot.respond(/\b((tell .*a )|\b((do you )?know (a|any)( \w+)? ))jokes?\b/i, msg => {
msg.finish();
let rjoke, message
if (jokesdataurl) {
GetUrlData(jokesdataurl, (jokedurlata) => {
TellJoke(msg, jokedurlata);
});
} else {
TellJoke(msg, jokejson);
};
});
};