Created
April 29, 2021 12:25
-
-
Save vipulwairagade/ce941597cea50d2e17dc46984eb99635 to your computer and use it in GitHub Desktop.
Make an automated outbound IVR call using Twilio in Node.js
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') | |
const twilio = require('twilio') | |
const VoiceResponse = require('twilio').twiml.VoiceResponse; | |
const accountSid = 'AC3306*************************2f89'; | |
const authToken = '3a8a*********************b0697'; | |
const client = require('twilio')(accountSid, authToken); | |
const app = express() | |
const port = 1337 | |
app.use(express.json()) | |
app.use(express.urlencoded({ extended: false })); | |
app.get('/', (req, res) => { | |
res.send('Hello World!') | |
}) | |
app.post('/makeCall', async (req, res) => { | |
let body = req.body | |
console.log("🚀 ~ app.post ~ body", body) | |
let call = await client.calls | |
.create({ | |
url: '<your-http-base-url-here>/callAnswered', | |
to: '+9190*****246', | |
from: '+125*****667' | |
}) | |
console.log(call.sid) | |
console.log(call.responseText) | |
res.status(200).send(call) | |
}) | |
app.post('/callAnswered', (req, res) => { | |
console.log("🚀 ~ app.post ~ req", req.body) | |
// Use the Twilio Node.js SDK to build an XML response | |
const twiml = new VoiceResponse(); | |
const gather = twiml.gather({ | |
action: '/gatherUserInput', | |
method: 'POST', | |
input: 'dtmf', | |
numDigits: 1 | |
}); | |
gather.say('If you are satisfied with our service, press 1. Otherwise, press 2.'); | |
// If the user doesn't enter input, loop | |
twiml.say('You did not enter any input. Please try again.') | |
twiml.redirect('/callAnswered'); | |
// Render the response as XML in reply to the webhook request | |
res.header("Content-Type", "text/xml") | |
res.status(200).send(twiml.toString()) | |
}); | |
// Create a route that will handle <Gather> input | |
app.post('/gatherUserInput', (req, res) => { | |
// Use the Twilio Node.js SDK to build an XML response | |
const twiml = new VoiceResponse(); | |
// If the user entered digits, process their request | |
if (req.body.Digits) { | |
switch (req.body.Digits) { | |
case '1': | |
twiml.say('Thank You for your feedback.'); | |
break; | |
case '2': | |
twiml.say('Thank You for your feedback. We will look forward to improve our services.'); | |
break; | |
default: | |
twiml.say("Sorry, I don't understand that choice."); | |
twiml.pause('2') | |
twiml.redirect('/callAnswered'); | |
break; | |
} | |
} else { | |
// If no input was sent, redirect to the /callAnswered route | |
twiml.redirect('/callAnswered'); | |
} | |
// Render the response as XML in reply to the webhook request | |
res.header("Content-Type", "text/xml") | |
res.status(200).send(twiml.toString()) | |
}); | |
app.get('*', function (req, res) { | |
res.status(404).send(JSON.stringify({ error: "Route not found" })); | |
}); | |
app.listen(port, () => { | |
console.log(`Example app listening at http://localhost:${port}`) | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment