-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
109 lines (95 loc) · 2.6 KB
/
index.js
File metadata and controls
109 lines (95 loc) · 2.6 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
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
const ovh = require("ovh");
const polka = require("polka");
const bodyParser = require("body-parser");
require("dotenv").config();
const billingAccount = process.env.BILLING_ACCOUNT;
const serviceName = process.env.SERVICE_NAME;
const client = ovh({
appKey: process.env.APP_KEY,
appSecret: process.env.APP_SECRET,
consumerKey: process.env.CONSUMER_KEY
});
const app = polka();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.post("/", (req, res) => {
console.log("Listing", req.params, req.body);
client
.requestPromised(
"GET",
`/telephony/${billingAccount}/screen/${serviceName}/screenLists`
)
.then(v =>
Promise.all(
v.map(id =>
client.requestPromised(
"GET",
`/telephony/${billingAccount}/screen/${serviceName}/screenLists/${id}`
)
)
)
)
.then(result => {
const message =
"Les numéros actuellement bloqués sont :\n\n" +
result
.map(
number =>
`* ${
number.callNumber
} *${number.status.toUpperCase()}* (id : \`${number.id}\`)`
)
.join("\n");
res.end(message);
})
.catch(e => console.error(e));
});
app.post("/block", (req, res) => {
const message = req.body.text || "";
console.log("Blocking", req.params, req.body, message);
if (!message.startsWith("+")) {
res.end(
`NOPE! Le numéro "${message}" n'est pas au format international (ex : +33102030405).`
);
return;
}
const payload = {
nature: "international",
type: "incomingBlackList",
callNumber: message
};
client
.requestPromised(
"POST",
`/telephony/${billingAccount}/screen/${serviceName}/screenLists`,
payload
)
.then(() => res.end(`YO! ${message} bloqué.`))
.catch(e => {
console.log("ERREUR", e);
res.end(`OOPS! Une erreur est survenue.`);
});
});
app.post("/unblock", (req, res) => {
const id = req.body.text || "";
console.log("Unblocking", req.params, req.body, id);
client
.requestPromised(
"DELETE",
`/telephony/${billingAccount}/screen/${serviceName}/screenLists/${id}`
)
.then(() => res.end(`YO! ${id} débloqué.`))
.catch(e => {
console.log("ERREUR", e);
res.end(`OOPS! Une erreur est survenue.`);
});
});
app.get("/", (_, res) => {
res.end("Hello world!");
});
const host = process.env.HOST || "0.0.0.0";
const port = process.env.PORT || 3000;
app.listen(port, host, err => {
if (err) throw err;
console.log(`> Running on ${host}:${port}`);
});