-
Notifications
You must be signed in to change notification settings - Fork 0
/
set-app-webhook.js
82 lines (78 loc) · 2.42 KB
/
set-app-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
require('dotenv').config();
const async = require('async');
const request = require('request');
const Nexmo = require('nexmo');
const nexmo = new Nexmo({
apiKey: process.env.API_KEY,
apiSecret: process.env.API_SECRET,
applicationId: process.env.APPLICATION_ID,
privateKey: process.env.PRIVATE_KEY
})
const argv = process.argv;
if (argv.length > 3) {
console.error("Invalid number of arguments.");
console.log("Usage:\n\t%s %s [<webhook-root-url>]", argv[0], argv[1])
process.exit(1);
}
const rootUrlArg = argv[2];
const appId = process.env.APPLICATION_ID;
async.waterfall([
(callback) => {
if (rootUrlArg) {
callback(null, null, rootUrlArg);
return;
}
request("http://localhost:4040/api/tunnels", callback);
},
(arg1, arg2, callback) => {
if (arg1 === null) {
const rootUrl = arg2;
callback(null, rootUrl);
return;
}
const resp = arg1;
if (resp.statusCode !== 200) {
callback("error response from ngrok: " + resp.statusCode);
return;
}
const body = JSON.parse(arg2);
var ngrokUrl;
body.tunnels.forEach(tunnel => {
// pick the https tunnel
if (tunnel.proto === "https") {
ngrokUrl = tunnel.public_url;
}
});
if (!ngrokUrl) {
callback("failed to obtain ngrok url");
return;
}
callback(null, ngrokUrl);
},
(rootUrl, callback) => {
nexmo.applications.get(appId, (err, app) => { callback(err, rootUrl, app) }, true);
},
(rootUrl, app, callback) => {
if (!app || !app.capabilities) {
callback("Unexpected get app response: " + JSON.stringify(app));
return;
}
if (app.capabilities.messages) {
const webhooks = app.capabilities.messages.webhooks;
webhooks.inbound_url.address = rootUrl + "/webhooks/messages/inbound";
webhooks.status_url.address = rootUrl + "/webhooks/messages/status";
} else {
callback("No supported API enabled");
return;
}
nexmo.applications.update(appId, app, callback);
}
], (err, app) => {
if (err) {
console.error(err);
} else {
for (const key in app.capabilities) {
console.log(key, app.capabilities[key].webhooks);
}
}
});