Created
February 22, 2017 18:57
-
-
Save mooyoul/cece6df613f9f024d8b8a39cf1b8d96d to your computer and use it in GitHub Desktop.
nexmo-ivr-demo.js
This file contains hidden or 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
'use strict'; | |
const path = require('path'); | |
const express = require('express'); | |
const bodyParser = require('body-parser'); | |
const app = express(); | |
const NCCO_INITIAL_PROMPT = [{ | |
"action": "stream", | |
"streamUrl": [`${process.env.BASE_URL}/prompt.mp3`], | |
"bargeIn": true | |
}, { | |
"action": "input", | |
"timeout": 10, | |
"maxDigits": 1, | |
"eventUrl": [`${process.env.BASE_URL}/ivr`] | |
}]; | |
const NCCO_BYE = [{ | |
"action": "stream", | |
"streamUrl": [`${process.env.BASE_URL}/bye.mp3`] | |
}]; | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({extended: false})); | |
app.use(express.static(path.join(__dirname, 'public'))); | |
app.use((req, res, next) => { | |
console.log('Got request, %s %s', req.method, req.url, req.ip, req.headers); | |
next(); | |
}); | |
app.route('/init') | |
.get((req, res) => { | |
res.send(NCCO_INITIAL_PROMPT); | |
}); | |
app.route('/ivr') | |
.post((req, res) => { | |
console.log('got ivr payload: %j', req.body); | |
const dtmf = req.body.dtmf; | |
if (dtmf !== '3') { | |
return res.send(NCCO_INITIAL_PROMPT); | |
} | |
res.send(NCCO_BYE); | |
}); | |
app.route('/events') | |
.get((req, res) => { | |
console.log('got event get request'); | |
console.log(req.url, req.query); | |
res.sendStatus(204); | |
}) | |
.post((req, res) => { | |
console.log('got event post request'); | |
console.log(req.url, req.query, req.body); | |
res.sendStatus(204); | |
}); | |
app.listen(9000, () => { | |
console.log('Server started at port 9000...'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment