Guest User

Untitled

a guest
Nov 2nd, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. const express = require('express');
  2. const bodyParser = require('body-parser')
  3. const axios = require('axios')
  4. const VoiceResponse = require('twilio').twiml.VoiceResponse;
  5.  
  6. const app = express();
  7. app.use(bodyParser.urlencoded({extended: false}))
  8.  
  9. app.post('/voice', (req, res) => {
  10. const twiml = new VoiceResponse()
  11.  
  12. twiml.say('Hi! I want to know what do you think about coding. One')
  13. //twiml.record({maxLength: '10', action: '/callback'})
  14. twiml.record({maxLength: '10', action: '/recording',playBeep:false})
  15. twiml.hangup()
  16.  
  17. res.send(twiml.toString())
  18. });
  19.  
  20. app.post('/recording', (req, res) => {
  21. const twiml = new VoiceResponse()
  22. const recordingUrl = req.body.RecordingUrl
  23.  
  24. twiml.say('Thanks for howling... take a listen to what you howled.')
  25. twiml.play(recordingUrl)
  26. twiml.say('Goodbye.')
  27.  
  28. res.send(twiml.toString())
  29. })
  30.  
  31. app.post('/callback', (req, res) => {
  32. const addOns = JSON.parse(req.body.AddOns)
  33.  
  34. if (!('ibm_watson_speechtotext' in addOns.results)) {
  35. return 'Add Watson Speech to Text add-on in your Twilio console'
  36. }
  37.  
  38. const payloadUrl = addOns.results.ibm_watson_speechtotext.payload[0].url
  39. const accountSID = process.env.TWILIO_ACCOUNT_SID
  40. const authToken = process.env.TWILIO_AUTH_TOKEN
  41.  
  42. axios.get(payloadUrl, {auth: {username: accountSID, password: authToken}})
  43. .then(response => {
  44. const results = response.data.results[0].results
  45. const transcripts = results.map(item => item.alternatives[0].transcript)
  46. return transcripts
  47. })
  48. .then(transcripts => res.send(transcripts.join(' ')))
  49. })
  50.  
  51. app.listen(1553);
Add Comment
Please, Sign In to add comment