-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
37 lines (27 loc) · 1.05 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
const express = require('express');
require("dotenv").config();
const WEBHOOK_PORT = process.env.WEBHOOK_PORT;
const WEBHOOK_URL = process.env.WEBHOOK_URL;
function handleNewCall(request, response) {
const { from: caller, to: calleeNumber } = request.body;
console.log(`New call from ${caller} to ${calleeNumber} is ringing...`);
response.set('Content-Type', 'application/xml');
response.send(`<Response onAnswer="${WEBHOOK_URL}/on-answer" onHangup="${WEBHOOK_URL}/on-hangup" />`);
}
function handleOnAnswer(request, response) {
const { from: caller, to: callee } = request.body;
console.log(`${callee} answered call from ${caller}`);
response.end();
}
function handleOnHangup(request, response) {
console.log(`The call has been hung up`);
response.end();
}
const app = express();
app.use(express.urlencoded({ extended: true }));
app.post('/new-call', handleNewCall);
app.post('/on-answer', handleOnAnswer);
app.post('/on-hangup', handleOnHangup);
app.listen(WEBHOOK_PORT, () => {
console.log(`Server listening on: http://localhost:${WEBHOOK_PORT}`);
});