Last active
August 29, 2015 14:23
-
-
Save ClaudeSutterlin/2f1c2d8e47af58692775 to your computer and use it in GitHub Desktop.
Text->Watson->Twilio->Phone Call
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
var express = require('express'); | |
var watson = require('watson-developer-cloud'); | |
var fs = require('fs'); | |
var router = express.Router(); | |
var twilio = require('twilio'); | |
var cfenv = require('cfenv'); | |
var appEnv = cfenv.getAppEnv(); | |
var AWS = require('aws-sdk'); | |
AWS.config.update({accessKeyId: 'XXXXXX', secretAccessKey: 'XXXXXX'}); | |
// This endpoint is called by Twilio to provide the call data | |
router.post('/:id/callaction', function(req, res, next) { | |
var message = req.body.message; | |
var file = req.param('id'); | |
res.contentType('application/xml'); | |
res.send('<?xml version="1.0" encoding="UTF-8"?><Response><Play>https://s3.amazonaws.com/CoffeeBeanBucket/'+file+'.wav</Play></Response>'); | |
}); | |
// This endpoint is used to perform the operation, JSON data is posted like so: {"message":"This is a test message","phonenumber":"5555555555"} | |
router.post('/send', function(req, res, next) { | |
var message = req.body.message; | |
var phonenumber = req.body.phonenumber; | |
console.log("Sending message:" + message); | |
var text_to_speech = watson.text_to_speech({ | |
username: 'XXXXXXX', | |
password: 'XXXXXXX', | |
version: 'v1' | |
}); | |
var params = { | |
text: message, | |
voice: 'VoiceEnUsMichael', // Optional voice | |
accept: 'audio/wav' | |
}; | |
// convert the text to speech and save it on the disk | |
var transcript = text_to_speech.synthesize(params); | |
transcript.pipe(fs.createWriteStream('output.wav')); | |
transcript.on('end', function () { | |
var s3 = new AWS.S3(); | |
// read the file off disk | |
fs.readFile('output.wav', function (err, data) { | |
var filename = Date.now();// + ".wav"; | |
var params = {Bucket: 'CoffeeBeanBucket', Key: filename+'.wav', Body: data, ACL:'public-read', ContentType:'audio/x-wav'}; | |
// upload the file to Amazon S3 | |
s3.putObject(params, function(err, data) { | |
if (err) | |
console.log(err) | |
else{ | |
// when the file is uploaded, place the call | |
var client = new twilio.RestClient('XXXXXX', 'XXXXXX'); | |
client.makeCall({ | |
to: phonenumber, | |
from:'2162424007', | |
url: appEnv.url+'/voice/'+filename+'/callaction' | |
}, function(err, call) { | |
if (err){ | |
console.log("Twilio Error:" + err); | |
} | |
console.log('This call\'s unique ID is: ' + call.sid); | |
console.log('This call was created at: ' + call.dateCreated); | |
}); | |
} | |
}); | |
}); | |
}); | |
res.json({"result":"okay"}); | |
}); | |
module.exports = router; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment