-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
47 lines (44 loc) · 1.1 KB
/
utils.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
var requestp = require('request-promise');
function getNgrokUrl() {
return requestp({
url: 'http://localhost:4040/api/tunnels',
json: true
})
.then(function(body) {
var url = body.tunnels[0].public_url;
return url;
})
.catch(function(err) {
console.log("ERROR: Failed to fetch ngrok url\n" + err)
})
}
async function createWebhook(webex) {
var ngrokUrl = await getNgrokUrl();
var webhookName = 'bot-webhook'
webex.webhooks.list()
.then(webhooks => {
var webhooksToDelete = []
webhooks.items.forEach(webhook => {
if (webhook.name === webhookName) {
webhooksToDelete.push(webex.webhooks.remove(webhook))
}
})
return Promise.all(webhooksToDelete)
})
.then(() => {
return webex.webhooks.create({
resource: 'messages',
event: 'created',
targetUrl: ngrokUrl,
name: webhookName
})
})
.catch(function(err) {
console.log("ERROR: Failed to create webhook\n" + err)
})
}
module.exports = {
setUpWebhook: function(webex) {
createWebhook(webex);
}
};