forked from scottsweb/notifyslack
-
Notifications
You must be signed in to change notification settings - Fork 1
/
notifyslack.js
195 lines (137 loc) · 4.18 KB
/
notifyslack.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// *********************** //
// NotifySlack v0.1 //
// *********************** //
// to-do:
// basic front end template for getting setup
// implement a storage mechanism/make it into an app? requires authentication etc
// app settings
var settings = {
'wp_client_id': '',
'wp_client_secret': '',
'slack_hook': '',
'slack_domain': '',
'slack_channel': '#channel',
'oauth_token': ''
};
// express
var express = require('express'),
app = express();
// slack
var Slack = require('node-slack');
var slack = new Slack(settings.slack_hook);
// cron
var CronJob = require('cron').CronJob;
// oauth settings
var oauth2 = require('simple-oauth2')({
clientID: settings.wp_client_id,
clientSecret: settings.wp_client_secret,
grant_type: 'authorization_code',
site: 'https://public-api.wordpress.com',
authorizationPath: '/oauth2/authorize',
tokenPath: '/oauth2/token'
});
// auth uri definition
var authorization_uri = oauth2.authCode.authorizeURL({
redirect_uri: 'http://localhost:3000/callback'
});
// boot the app
var server = app.listen(3000, function () {
var host = server.address().address
var port = server.address().port
console.log('NotifySlack (http://%s:%s)', host, port)
});
// default view
app.get('/', function (req, res) {
console.log('/');
if (settings.oauth_token == '') {
res.send('NotifySlack App - <a href="/auth">Grab your oAuth token</a>.');
} else {
res.send('NotifySlack App');
}
});
// redirect to /auth with WordPress.com to get token
app.get('/auth', function (req, res) {
console.log(authorization_uri);
res.redirect(authorization_uri);
});
// callback service parsing the authorisation token and asking for the access token
app.get('/callback', function (req, res) {
var code = req.query.code;
console.log('/callback');
oauth2.authCode.getToken({
code: code,
redirect_uri: 'http://localhost:3000/callback'
}, saveToken);
function saveToken(error, result) {
if (error) { console.log('Access Token Error', error.message); }
token = oauth2.accessToken.create(result);
console.log(token);
res.redirect('/')
}
});
// run the sweep every minute when a token is set
new CronJob('0 * * * * *', function() {
if (settings.oauth_token == '' )
return;
console.log('Running NotifySlack...');
var rest = require('restler');
var options = {
accessToken: settings.oauth_token
};
rest.get('https://public-api.wordpress.com/rest/v1/notifications/?unread=true', options).on('complete', function(result, response) {
if (result instanceof Error) {
console.log('Error:', result.message);
} else {
var counts = {};
var ent = require('ent');
result.notes.forEach(function(note) {
console.log('Sending ' + note.id + ' to Slack.');
switch(note.type) {
case "achieve_daily_streak":
var text = ent.decode(note.subject.text);
var title = 'WordPress.com: ' + ent.decode(note.subject.text);
break;
case "post_milestone_achievement":
case "like_milestone_achievement":
var text = ent.decode(note.subject.text);
var title = 'WordPress.com: ' + ent.decode(note.body.header_text);
break;
default:
var text = '<'+ note.body.header_link +'|'+ ent.decode(note.subject.text) +' »>';
var title = 'WordPress.com: ' + ent.decode(note.body.header_text);
}
// send to slack
slack.send({
text: text,
channel: settings.slack_channel,
username: title,
icon_url: note.subject.icon,
unfurl_links: true,
link_names: 1
});
// add to array mark as read
counts[note.id] = 1;
});
// mark these as read
// console.log(counts);
if (Object.keys(counts).length) {
var postoptions = {
data: {
counts: counts
},
accessToken: settings.oauth_token
};
rest.post('https://public-api.wordpress.com/rest/v1/notifications/read', postoptions).on('complete', function(postresult, postresponse) {
if (postresult instanceof Error) {
console.log('Error:', postresult.message);
} else {
console.log(Object.keys(counts).length + ' notifications marked as read.');
console.log(postresult);
}
});
} else {
console.log('No new notifications.')
}
}
});
}, null, true);