Skip to content

Instantly share code, notes, and snippets.

@victorpaulo
Last active August 26, 2019 18:07
Show Gist options
  • Save victorpaulo/384a83ef693cb47913495e35e38f7778 to your computer and use it in GitHub Desktop.
Save victorpaulo/384a83ef693cb47913495e35e38f7778 to your computer and use it in GitHub Desktop.
Text-to-Speech WhatsApp Twilio and AWS
require('dotenv').config();
var AssistantV1 = require('ibm-watson/assistant/v1');
var TextToSpeechV1 = require('ibm-watson/text-to-speech/v1');
const clientTwilio = require('twilio')(process.env.accountSid, process.env.authToken);
const express = require('express');
const bodyParser = require('body-parser');
var awsS3 = require('./aws_s3')
// Constants
const port = process.env.PORT || 8080;
// App
const app = express();
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
// define the about route
// define the about route
app.post('/', function (req, res) {
var text = req.body.Body
var isVoice = false
if (text.indexOf("@") == 0) {
isVoice = true
text = text.substring(1);
}
var assistant = new AssistantV1({
iam_apikey: process.env.WATSON_API,
url: 'https://gateway.watsonplatform.net/assistant/api/',
version: '2018-02-16'
});
var inputMsg = { text: req.body.Body }
var msg = {
input: inputMsg,
workspace_id: process.env.WORKSPACE_ID
}
//watson assistant call
assistant.message(msg)
// service.message(obj)
.then(result => {
console.log("watson result ", JSON.stringify(result));
if (isVoice) {
//text to speech call
var params = {
text: result.output.text[0],
voice: 'en-US_AllisonVoice', // Optional voice
accept: 'audio/mpeg'
};
var textToSpeech = new TextToSpeechV1({
iam_apikey: process.env.API_KEY,
url: 'https://stream.watsonplatform.net/text-to-speech/api/'
});
textToSpeech.synthesize(params).then(audio => {
var file = { name: 'audio_' +req.body.MessageSid+ '.mpeg', data: audio, mimetype: 'audio/mpeg' };
awsS3.uploadToS3(file).then(awsResult => {
//audio = textToSpeech.repairWavHeader(result2);
console.log("awsResult = " + JSON.stringify(awsResult));
//twilio call
clientTwilio.messages.create({
body: result.output.text[0], //JSON.stringify(result.output.text[0], null, 2),
from: 'whatsapp:+14155238886',
to: req.body.From,
mediaUrl: awsResult.Location
})
.then(message => console.log(message.sid))
.done();
//catch S3
}).catch(err => {
console.log(err);
});
//catch textToSpeech
}).catch(err => {
console.log(err);
});
} else {
clientTwilio.messages
.create({
body: result.output.text[0],
from: 'whatsapp:+14........6',
to: req.body.From,
})
.then(message => console.log(message.sid))
.done();
}
//catch Watson assistant
}).catch(err => {
console.log(err);
});
});
var server = app.listen(port, function () {
console.log('Server running at http://127.0.0.1:' + port + '/');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment