-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook.js
More file actions
55 lines (47 loc) · 1.39 KB
/
Copy pathwebhook.js
File metadata and controls
55 lines (47 loc) · 1.39 KB
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
import ngrok from '@ngrok/ngrok';
import { Bkper } from 'bkper-js';
import { getOAuthToken } from 'bkper'
import { App } from 'bkper-js';
import dotenv from 'dotenv';
import { fileURLToPath } from 'url';
import path from 'path';
const __filename = fileURLToPath(import.meta.url); // get the resolved path to the file
const __dirname = path.dirname(__filename); // get the name of the directory
dotenv.config({ path: `${__dirname}/.env` });
process.env.NODE_ENV = 'development';
const apiKey = process.env.BKPER_API_KEY;
Bkper.setConfig({
oauthTokenProvider: () => getOAuthToken()
})
const app = new App();
let webhookListener;
(async function () {
try {
console.log("Starting ngrok...");
webhookListener = await ngrok.forward({ port: 3004, authtoken_from_env: true });
const webhookUrl = webhookListener.url();
console.log(`Started webhook at ${webhookUrl}`);
await app.setWebhookUrlDev(webhookUrl).patch();
} catch (err) {
console.log(err);
throw err;
}
})();
async function exit() {
try {
await app.setWebhookUrlDev(null).patch();
console.log(' \nRemoved webhook.');
if (webhookListener) {
webhookListener.close();
}
} catch (err) {
console.log(err);
}
process.exit();
}
process.on('exit', exit);
process.on('SIGINT', exit);
process.on('SIGUSR1', exit);
process.on('SIGUSR2', exit);
process.on('uncaughtException', exit);
process.stdin.resume();