This repository has been archived by the owner on Mar 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
/
webhook.js
234 lines (194 loc) · 5.03 KB
/
webhook.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
const request = require('request-promise')
const auth = require('../helpers/auth.js')
var webhook = {}
/**
* Retrieves existing webhook config and renders
*/
webhook.get_config = function (req, resp) {
// construct request to retrieve webhook config
var request_options = {
url: 'https://api.twitter.com/1.1/account_activity/all/' + auth.twitter_webhook_environment + '/webhooks.json',
oauth: auth.twitter_oauth
}
request.get(request_options)
// success
.then(function (body) {
var json_response = {
configs: JSON.parse(body),
csrf_token: req.csrfToken(),
update_webhook_url: 'https://' + req.headers.host + '/webhook/twitter'
}
if (json_response.configs.length) {
json_response.update_webhook_url = json_response.configs[0].url
}
console.log(json_response)
resp.render('webhook', json_response)
})
// failure
.catch(function (body) {
if (body) {
console.log(body)
}
var json_response = {
title: 'Error',
message: 'Webhook config unable to be retrieved',
button: {
title: 'Ok',
url: '/webhook'
}
}
resp.status(500);
resp.render('status', json_response)
})
}
/**
* Triggers challenge response check
*/
webhook.validate_config = function (req, resp) {
// get bearer token
auth.get_twitter_bearer_token()
// validate webhook config
.then(function (bearer_token) {
// request options
var request_options = {
url: 'https://api.twitter.com/1.1/account_activity/all/' + auth.twitter_webhook_environment + '/webhooks/' + req.body.webhook_id + '.json',
resolveWithFullResponse: true,
auth: {
'bearer': bearer_token
}
}
// PUT request to retreive webhook config
request.put(request_options)
// success
.then(function (response) {
var json_response = {
title: 'Success',
message: 'Challenge request successful and webhook status set to valid.',
button: {
title: 'Ok',
url: '/webhook'
}
}
resp.render('status', json_response)
})
// failure
.catch(function (response) {
var json_response = {
title: 'Error',
message: response.error,
button: {
title: 'Ok',
url: '/webhook'
}
}
resp.render('status', json_response)
})
})
}
/**
* Deletes exiting webhook config
* then creates new webhook config
*/
webhook.update_config = function (req, resp) {
// delete webhook config
delete_webhook(req.body.webhook_id)
// create new webhook config
.then(function () {
var request_options = {
url: 'https://api.twitter.com/1.1/account_activity/all/' + auth.twitter_webhook_environment + '/webhooks.json',
oauth: auth.twitter_oauth,
headers: {
'Content-type': 'application/x-www-form-urlencoded'
},
form: {
url: req.body.url
}
}
return request.post(request_options)
})
// render success response
.then(function (body) {
var json_response = {
title: 'Success',
message: 'Webhook successfully updated.',
button: {
title: 'Ok',
url: '/webhook'
}
}
resp.render('status', json_response)
})
// render error response
.catch(function (body) {
var json_response = {
title: 'Error',
message: 'Webhook not updated.',
button: {
title: 'Ok',
url: '/webhook'
}
}
console.log(body)
// Look for detailed error
if (body.error) {
json_response.message = JSON.parse(body.error).errors[0].message
}
resp.render('status', json_response)
})
}
/**
* Deletes existing webhook config
*/
webhook.delete_config = function (req, resp) {
// delete webhook config
delete_webhook(req.body.webhook_id)
// render success response
.then(function (body) {
var json_response = {
title: 'Success',
message: 'Webhook successfully deleted.',
button: {
title: 'Ok',
url: '/webhook'
}
}
resp.render('status', json_response)
})
// render error response
.catch(function () {
var json_response = {
title: 'Error',
message: 'Webhook was not deleted.',
button: {
title: 'Ok',
url: '/webhook'
}
}
resp.render('status', json_response)
})
}
/**
* Helper function that deletes the webhook config.
* Returns a promise.
*/
function delete_webhook (webhook_id) {
return new Promise (function (resolve, reject) {
// if no webhook id provided, assume there is none to delete
if (!webhook_id) {
resolve()
return;
}
// construct request to delete webhook config
var request_options = {
url: 'https://api.twitter.com/1.1/account_activity/all/' + auth.twitter_webhook_environment + '/webhooks/' + webhook_id + '.json',
oauth: auth.twitter_oauth,
resolveWithFullResponse: true
}
request.delete(request_options).then(function () {
resolve()
}).catch(function () {
reject()
})
})
}
module.exports = webhook