-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
50 lines (43 loc) · 1.25 KB
/
index.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
const { createHistoryModule, sipgateIO } = require('sipgateio');
const { HistoryEntryType } = require('sipgateio/dist/history');
const axios = require('axios');
require("dotenv").config();
const slackWebhookUrl = process.env.SLACK_WEBHOOK_URL;
if (!slackWebhookUrl) {
console.error(
'Please provide a Slack webhook URL via the environment variable SLACK_WEBHOOK_URL'
);
return;
}
const sipgateio = sipgateIO({
tokenId: process.env.SIPGATE_TOKEN_ID,
token: process.env.SIPGATE_TOKEN,
});
const historyModule = createHistoryModule(sipgateio);
const handleEntries = (entries) => {
entries.forEach((entry) => {
if (entry.type === HistoryEntryType.SMS) {
axios.post(slackWebhookUrl, {
text: `SMS received from ${entry.source}:\n${entry.smsContent}`,
});
}
if (entry.type === HistoryEntryType.VOICEMAIL) {
axios.post(slackWebhookUrl, {
text: `New voicemail recording from ${entry.source}:\n${entry.recordingUrl}`,
});
}
});
historyModule.batchUpdateEvents(entries, () => {
return { archived: true };
});
};
setInterval(async () => {
historyModule
.fetchAll({
types: [HistoryEntryType.SMS, HistoryEntryType.VOICEMAIL],
archived: false,
directions: ['INCOMING'],
})
.then(handleEntries)
.catch(console.error);
}, 5000);