Created
February 27, 2023 05:03
-
-
Save rktalusani/50b1f35bd7be54f76b199e7b423bd3a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express') | |
var bodyParser = require('body-parser') | |
const uuid = require("uuid") | |
const jwt = require("jsonwebtoken") | |
const request = require('request'); | |
const fs = require('fs') | |
const app = express() | |
app.use(bodyParser.json()) | |
app.use(bodyParser.urlencoded({ extended: false })) | |
const app_id="39dc9665-f65e-4764-92d3-715aa98b3f55"; | |
const private_key="39dc9665-f65e-4764-92d3-715aa98b3f55.key"; | |
var callid="" | |
const port = 3000 | |
app.get('/answer', (req, res) => { | |
callid = req.query.uuid; | |
console.log(callid); | |
const ncco = [ | |
{ | |
action: 'talk', | |
text: 'Welcome. Please say Sales or Support' | |
}, | |
{ | |
action: "input", | |
eventUrl: ["https://rwait.loca.lt/ivr"], | |
type: [ "speech" ], | |
speech: { | |
context: [ "sales", "support" ] | |
} | |
} | |
] | |
console.log(ncco); | |
res.json(ncco); | |
}) | |
app.post('/ivr', (req, res) => { | |
var text = req.body.speech.results[0].text; | |
const ncco = [ | |
{ | |
action: "stream", | |
streamUrl: ["https://vids.vonage.com/unifiedAgent/hold.mp3"], | |
loop: 0 | |
} | |
] | |
console.log(ncco); | |
setTimeout(function(){transferNCCO(text)},20000); | |
res.json(ncco); | |
}) | |
app.post('/event', (req, res) => { | |
console.log(JSON.stringify(req.body)); | |
res.sendStatus(200); | |
}) | |
app.post('/rtcevent', (req, res) => { | |
if(req.body.type=="audio:dtmf") | |
console.log(JSON.stringify(req.body)); | |
res.sendStatus(200); | |
}) | |
app.listen(3000, () => { | |
console.log(`Example app listening on port ${port}`) | |
}) | |
const localtunnel = require('localtunnel'); | |
(async () => { | |
const tunnel = await localtunnel({ | |
subdomain: "rwait", | |
port: 3000 | |
}); | |
console.log(`App available at: ${tunnel.url}`); | |
})(); | |
function transferNCCO(text){ | |
var jwt = generateJwt(app_id,private_key); | |
console.log(jwt); | |
var body = { | |
"action": "transfer", | |
"destination": { | |
"type": "ncco", | |
"ncco":[ | |
{ | |
"action":"talk", | |
"text":"you said" + text | |
} | |
] | |
} | |
}; | |
console.log(callid); | |
var options = { | |
uri: 'https://api.nexmo.com/v1/calls/'+callid, | |
method: 'PUT', | |
json: body, | |
headers:{ | |
'Authorization': 'Bearer '+jwt | |
} | |
}; | |
console.log(JSON.stringify(options)); | |
request(options, function(error, res, body){ | |
if (error) { | |
console.log('Error :', error); | |
return; | |
} | |
console.log(' Body :', body) | |
}); | |
} | |
function generateJwt(applicationId, privateKey, userJwt=false, user='') { | |
var claims = { | |
application_id: applicationId | |
}; | |
if(userJwt == true){ | |
var claims = { | |
application_id: applicationId, | |
sub: user, | |
acl: { | |
"paths": { | |
"/*/users/**": {}, | |
"/*/conversations/**": {}, | |
"/*/sessions/**": {}, | |
"/*/devices/**": {}, | |
"/*/image/**": {}, | |
"/*/media/**": {}, | |
"/*/applications/**": {}, | |
"/*/push/**": {}, | |
"/*/knocking/**": {} | |
} | |
} | |
}; | |
} | |
var cert =null; | |
if (privateKey instanceof Buffer) { | |
cert = privateKey; | |
} else if ( | |
typeof privateKey === "string" && | |
privateKey.startsWith("-----BEGIN PRIVATE KEY-----") | |
) { | |
cert = new Buffer(privateKey); | |
} else if (privateKey !== undefined) { | |
if (!fs.existsSync(privateKey)) { | |
throw new Error(`File "${privateKey}" not found.`); | |
} | |
cert = fs.readFileSync(privateKey); | |
} | |
var toSign = { | |
iat: claims.issuedAt || parseInt(Date.now() / 1000, 10), | |
jti: claims.jti || uuid.v1(), | |
exp: parseInt(Date.now() / 1000, 10) + 7200 | |
}; | |
Object.keys(claims).forEach(key => { | |
toSign[key] = claims[key]; | |
}); | |
var token = jwt.sign(toSign, cert, { algorithm: "RS256" }); | |
return token; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment