This repository has been archived by the owner on Dec 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hubot-say.js
57 lines (48 loc) · 1.56 KB
/
hubot-say.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
"use strict";
var assert = require("assert-plus");
var URL = require("url");
var restify = require("restify");
var bunyan = require("bunyan");
var log = bunyan.createLogger({
name: "hubot-say",
level: "debug"
});
function validateConfig(config) {
assert.object(config, "config");
assert.string(config.hubot_url, "config.hubot_url");
assert.optionalString(config.room, "config.room");
assert.optionalString(config.type, "config.type");
}
/**
* Handle a publish event.
*
* @param subscription the subscription object doing the handling?
* @param triggering_tags the tags that triggered this publish event
* @param message the published message
*/
function handlePublish(sub_id, subscriber_tags, publisher_tags, config, data) {
assert.string(data.message, "data.message");
assert.optionalString(data.room, "data.room");
assert.optionalString(data.type, "data.type");
if ((! data.room) && config.room) {
data.room = config.room;
}
if ((! data.type) && config.type) {
data.type = config.type;
}
var parsedUrl = URL.parse(config.hubot_url);
restify.createJsonClient({
url: parsedUrl.protocol + "//" + parsedUrl.host
})
.post(parsedUrl.path, data, function(err) {
if (err) {
log.error(err, "unable to invoke callback %s for subscription %s", config.hubot_url, sub_id);
} else {
log.debug("posted to %s for %s", config.hubot_url, sub_id);
}
});
}
module.exports = {
handle: handlePublish,
validate: validateConfig
};