const config = require('./config');
const plivo = require('plivo');
const express = require('express');
const app = express();
app.set('port', (process.env.PORT || 5000));
// Handle incoming calls to a Plivo number, connect agent with customer and vice versa without revealing their actual phone numbers.
app.all('/handleincoming/', function(req, res) {
const fromNumber = (req.query.From);
const toNumber = (req.query.To);
const response = plivo.Response();
const customerPhoneMapping = config.customerAgentMap;
const agentCustomerMapping = Object.fromEntries(Object.entries(customerPhoneMapping).map(v => v.reverse()));
if(fromNumber in customerPhoneMapping){ // Check whether the customer's number is in the customer-agent mapping
const number = customerPhoneMapping[fromNumber]; // Assign the value from the customer-agent array to number variable
const params = {
'callerId': toNumber, // Plivo number is used as the caller ID for the call toward the agent
};
const dial = response.addDial(params);
const destNumber = number;
dial.addNumber(destNumber);
res.send(response.toXML());
}
else if(fromNumber in agentCustomerMapping){ // Check whether the agent's number is in the customer-agent mapping
const number = agentCustomerMapping[fromNumber]; // Assign the key from the customer-agent array to number variable
const params = {
'callerId': toNumber, // Plivo number is used as the caller ID for the call toward the customer
};
const dial = response.addDial(params);
const destNumber = number;
dial.addNumber(destNumber);
res.send(response.toXML());
}
});
app.listen(app.get('port'), function () {
console.log('Node app is running on port', app.get('port'));
});