-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack_publisher.js
122 lines (106 loc) · 3.64 KB
/
slack_publisher.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
'use strict';
const config_file_path = 'config.json';
const Promise = require('bluebird');
const rp = require('request-promise');
const debug = require('debug')('slackPublisher');
const debugApp = require('debug')('app');
const configReader = require('./config_reader.js').create(config_file_path);
const slackPublisher = function slackPublisher() {
const slack_hooks_endpoint = 'https://hooks.slack.com/services';
const slackTokenUri = function slackTokenUri() {
const team_token = configReader.get('publisher:slack:team_token');
const service_token = configReader.get('publisher:slack:service_token');
const auth_token = configReader.get('publisher:slack:auth_token');
const token_uri = `/${team_token}/${service_token}/${auth_token}`;
return token_uri;
};
const buildText = function buildText(ad) {
const aired_year = `in ${ad.ad_year}, `;
const aired_country = `in ${ad.ad_country}. `;
const directed_by = `Directed by ${ad.ad_director}. `;
const created_by = `Created by ${ad.ad_agency}.`;
let attachment_text = '';
if (ad.ad_year || ad.ad_country) {
attachment_text += `Originally aired `;
}
if (ad.ad_year) {
attachment_text += `${aired_year}`;
}
if (ad.ad_country) {
attachment_text += `${aired_country}`;
}
if (ad.ad_director) {
attachment_text += `${directed_by}`;
}
if (ad.ad_agency) {
attachment_text += `${created_by}`;
}
return attachment_text;
};
const buildPayload = function buildPayload(ad) {
const disclaimer = "_Note: while this ad has been shown on " +
"mainstream media, it was randomly chosen and wasn't reviewed; " +
"viewer discretion is advised._ " +
"_This isn't an endorsement nor a condonation of the products, brands, " +
"messages, ideas, groups, or actions implicitly or explicitly depicted " +
"within the ad._\n";
const contribute_message = "_<https://github.com/coaxial/adaday|Source " +
"code available>, please consider contributing._\n";
const message_text = "Today's random ad is here!\n" +
contribute_message;
const icon_emoji = ':tv:';
const attachment_title = `${ad.advertiser_name}: ${ad.ad_title}`;
const attachment_text = `${buildText(ad)}\n${disclaimer}`;
const attachment_link = ad.video_url;
const image_url = ad.image_url;
const channel = configReader.get('publisher:slack:channel');
const username = 'AdADay';
const payload = {
channel: channel,
username: username,
text: message_text,
icon_emoji: icon_emoji,
attachments: [
{
title: attachment_title,
title_link: attachment_link,
text: attachment_text,
image_url: image_url,
mrkdwn_in: ['text']
}
]
}
debug(`payload being sent to Slack: ${JSON.stringify(payload, null, 4)}`);
return payload;
};
const postHook = Promise.method(function postHook(ad) {
const slack_token_uri = slackTokenUri();
const hooks_url = slack_hooks_endpoint + slack_token_uri;
const payload = buildPayload(ad);
const options = {
method: 'POST',
uri: hooks_url,
json: payload
};
return rp(options)
.then((response) => {
debug(`Slack API responded with '${response}'`);
debugApp('Ad successfully posted to Slack');
return payload;
})
.catch((err) => {
throw new Error(`'${slack_hooks_endpoint}' responded with '${err}'`);
});
});
//
// Public methods
//
return {
publishAd: Promise.method(function publishAd(ad) {
return postHook(ad);
})
};
};
module.exports = {
create: slackPublisher
};