Skip to content

Instantly share code, notes, and snippets.

@mlaccetti
Created November 18, 2020 05:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlaccetti/416246d4653ae972f87d2cacaa2c18d0 to your computer and use it in GitHub Desktop.
Save mlaccetti/416246d4653ae972f87d2cacaa2c18d0 to your computer and use it in GitHub Desktop.
TwiML Express App
{
"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"
}
}
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