Created
November 18, 2020 05:48
-
-
Save mlaccetti/416246d4653ae972f87d2cacaa2c18d0 to your computer and use it in GitHub Desktop.
TwiML Express App
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
{ | |
"name": "twiml-openai", | |
"version": "0.1.0", | |
"description": "TwiML OpenAI Integration", | |
"main": "server.js", | |
"scripts": { | |
"server": "node server.js" | |
}, | |
"author": "Michael Laccetti <michael@laccetti.com>", | |
"license": "GPL-2.0-only", | |
"dependencies": { | |
"express": "4.17.1", | |
"twilio": "3.51.0" | |
} | |
} |
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 VoiceResponse = require('twilio').twiml.VoiceResponse; | |
const app = express(); | |
app.use(express.json()); | |
app.use(express.urlencoded({ extended: true })); | |
const port = 13337; | |
app.post('/', (req, res) => { | |
// Create TwiML response | |
const response = new VoiceResponse(); | |
const gather = response.gather({ | |
language: 'en-CA', | |
voice: 'woman', | |
input: 'speech', | |
action: 'https://twiml-openai.loca.lt/completed' | |
}); | |
gather.say('Welcome to our demo, ask us a question.'); | |
response.redirect({ | |
method: 'POST' | |
}, '/timeout'); | |
const responseText = response.toString(); | |
console.log(responseText); | |
res.type('application/xml'); | |
res.send(responseText); | |
}); | |
app.post('/timeout', (req, res) => { | |
console.log(req.body); | |
const response = new VoiceResponse(); | |
response.say({ | |
voice: 'woman', | |
language: 'en-CA' | |
}, 'Sorry, we didn\'t get that. Please call back and try again.'); | |
response.hangup(); | |
const responseText = response.toString(); | |
console.log(responseText); | |
res.type('application/xml'); | |
res.send(responseText); | |
}); | |
app.post('/completed', (req, res) => { | |
console.log(req.body.SpeechResult); | |
// this is where we would hook into OpenAI to feed gather the response | |
const response = new VoiceResponse(); | |
response.say({ | |
voice: 'woman', | |
language: 'en-CA' | |
}, 'Thanks for sharing your question with us.'); | |
response.hangup(); | |
const responseText = response.toString(); | |
console.log(responseText); | |
res.type('application/xml'); | |
res.send(responseText); | |
}); | |
app.listen(port, () => { | |
console.log(`TwiML OpenAI app listening at http://localhost:${port}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment