Using Watson STT and TTS on Facebook Messenger ChatBot thru ChatFuel

This flow can be used as orchestrator of ChatBot on Facebook Messenger thru ChatFuel. It receive a link from messenger, convert to WAV, send to STT for transcription and send the transcription to Watson Conversation. With Watson Conversation response, use TTS to convert it to audio, convert to MP3 (Format used by Facebook Messenger), store in a global variable session, and send back a link to Facebook Messenger. This Flow uses ffmpeg node to convert audio.

[{"id":"9a17d538.618498","type":"tab","label":"Bot with STT and TTS ","disabled":false,"info":""},{"id":"d3a3af44.a2f9f","type":"http in","z":"9a17d538.618498","name":"","url":"/facebook/message","method":"get","upload":true,"swaggerDoc":"","x":137.42857360839844,"y":48.571428298950195,"wires":[["a1f1fd7.91848"]]},{"id":"a1f1fd7.91848","type":"change","z":"9a17d538.618498","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"req.query.message","tot":"msg"},{"t":"set","p":"resAux","pt":"flow","to":"res","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":132.58728790283203,"y":118.03175354003906,"wires":[["433b69d7.aec6f8"]]},{"id":"50ee45b7.2cd9ec","type":"watson-conversation-v1","z":"9a17d538.618498","name":"","workspaceid":"151e17a1-809e-4cf8-bfc0-77be6e73597b","multiuser":false,"context":true,"default-endpoint":true,"service-endpoint":"https://gateway.watsonplatform.net/conversation/api","x":570.8729858398438,"y":313.46032524108887,"wires":[["f6282e5c.90788"]]},{"id":"41f77873.d8da48","type":"change","z":"9a17d538.618498","name":"","rules":[{"t":"set","p":"res","pt":"msg","to":"resAux","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":1185.2659301757812,"y":356.3532934188843,"wires":[["8491ce54.fd854"]]},{"id":"8491ce54.fd854","type":"http response","z":"9a17d538.618498","name":"","statusCode":"","headers":{},"x":1362.7976837158203,"y":450.8175048828125,"wires":[]},{"id":"eebbe85d.c9f748","type":"watson-speech-to-text","z":"9a17d538.618498","name":"","continuous":true,"speakerlabels":false,"lang":"pt-BR","langhidden":"","langcustom":"NoCustomisationSetting","langcustomhidden":"","band":"BroadbandModel","bandhidden":"BroadbandModel","password":"vCxHYSSHfS4Z","payload-response":true,"default-endpoint":true,"service-endpoint":"https://stream.watsonplatform.net/speech-to-text/api","x":606.5872955322266,"y":228.03175830841064,"wires":[["50ee45b7.2cd9ec"]]},{"id":"433b69d7.aec6f8","type":"switch","z":"9a17d538.618498","name":"","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"https://cdn.fbsbx.com","vt":"str"},{"t":"else"}],"checkall":"true","outputs":2,"x":125.15869903564453,"y":192.0317578315735,"wires":[["86394318.04266"],["3d86423d.cad27e"]]},{"id":"3d86423d.cad27e","type":"change","z":"9a17d538.618498","name":"","rules":[{"t":"set","p":"audio","pt":"flow","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":357.4444274902344,"y":236.8888931274414,"wires":[["50ee45b7.2cd9ec"]]},{"id":"8e114ae5.0337e8","type":"watson-text-to-speech","z":"9a17d538.618498","name":"","lang":"pt-BR","langhidden":"pt-BR","langcustom":"NoCustomisationSetting","langcustomhidden":"NoCustomisationSetting","voice":"pt-BR_IsabelaVoice","voicehidden":"","format":"audio/wav","password":"jlU4eeSZ1nRG","payload-response":true,"default-endpoint":true,"service-endpoint":"https://stream.watsonplatform.net/text-to-speech/api","x":997.2660675048828,"y":196.1031837463379,"wires":[["f57fddce.70a8d"]]},{"id":"ef508d3d.85bdd","type":"ffmpeg-conversion","z":"9a17d538.618498","name":"","format":"wav","audiochannels":"mono","x":572.015869140625,"y":152.88887691497803,"wires":[["eebbe85d.c9f748"]]},{"id":"3b03563.0bef1aa","type":"function","z":"9a17d538.618498","name":"","func":"msg.payload =msg.payload.output.text[0];\nreturn msg;","outputs":1,"noerr":0,"x":837.1944580078125,"y":195.4245901107788,"wires":[["8e114ae5.0337e8"]]},{"id":"97e60357.775c6","type":"http in","z":"9a17d538.618498","name":"","url":"/facebook/response","method":"get","upload":false,"swaggerDoc":"","x":141.4444351196289,"y":406.74617099761963,"wires":[["9132c60b.8f7ae8"]]},{"id":"32728616.3418fa","type":"http response","z":"9a17d538.618498","name":"","statusCode":"","headers":{"content-type":"audio/mpeg3"},"x":662.4444389343262,"y":407.17469787597656,"wires":[]},{"id":"9132c60b.8f7ae8","type":"change","z":"9a17d538.618498","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"req.query.audioid","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":363.15869903564453,"y":406.8175964355469,"wires":[["4ce29d97.486dc4"]]},{"id":"1da2c99e.bdb486","type":"function","z":"9a17d538.618498","name":"","func":"var url = \"https://\" + msg.req.headers.host;\n\nif(!context.global.speech)\n{\n    context.global.speech = [];\n    context.global.speech[0] =\n    {_id:context.global.id.toString(),speech:msg.payload};\n}\nelse\n{\n    var pos;\n    pos = context.global.speech.length;\n    context.global.speech[pos] =\n    {_id:context.global.id.toString(),speech:msg.payload};\n}\n\nmsg.payload = [{\"attachment\":{\n    \"type\":\"audio\",\n    \"payload\":{\"url\":url+ \"/facebook/response?audioid=\"+context.global.id.toString()}\n    }\n}];\nreturn msg;","outputs":1,"noerr":0,"x":1031.444450378418,"y":356.31745529174805,"wires":[["41f77873.d8da48"]]},{"id":"4ce29d97.486dc4","type":"function","z":"9a17d538.618498","name":"","func":"for(var k in context.global.speech) {\n  if (context.global.speech[k]._id == msg.payload) \n    {\n      msg.payload = context.global.speech[k].speech;\n// Delete an item\n      context.global.speech.splice(k,1);\n      break;\n    } \n  }\nreturn msg;\n\n//msg.payload = Buffer.from(msg.payload.audio, 'utf8');\n//return msg;","outputs":1,"noerr":0,"x":526.5634918212891,"y":407.17471408843994,"wires":[["32728616.3418fa"]]},{"id":"86394318.04266","type":"change","z":"9a17d538.618498","name":"","rules":[{"t":"set","p":"audio","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":366.15869903564453,"y":150.3174705505371,"wires":[["ef508d3d.85bdd"]]},{"id":"f6282e5c.90788","type":"switch","z":"9a17d538.618498","name":"","property":"audio","propertyType":"flow","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","outputs":2,"x":749.4444313049316,"y":314.8888807296753,"wires":[["3b03563.0bef1aa"],["34d15423.51bdac"]]},{"id":"34d15423.51bdac","type":"function","z":"9a17d538.618498","name":"","func":"msg.payload = \n[{\"text\":msg.payload.output.text[0]}];\nreturn msg;","outputs":1,"noerr":0,"x":1036.2001113891602,"y":451.4285612106323,"wires":[["8491ce54.fd854"]]},{"id":"fb5504a9.7c1d78","type":"ffmpeg-conversion","z":"9a17d538.618498","name":"","format":"mp3","audiochannels":"mono","x":1318.0000801086426,"y":195.4285888671875,"wires":[["1da2c99e.bdb486"]]},{"id":"f57fddce.70a8d","type":"function","z":"9a17d538.618498","name":"","func":"context.global.id = context.global.id || 0;\ncontext.global.id = Math.floor((Math.random() * 100000000000) + 1);\nreturn msg;","outputs":1,"noerr":0,"x":1156.1072387695312,"y":195.92857789993286,"wires":[["fb5504a9.7c1d78"]]}]
sergiogama

Flow Info

created 2 weeks, 6 days ago

Node Types

Core
  • change (x5)
  • function (x5)
  • http in (x2)
  • http response (x2)
  • switch (x2)
Other

Tags

  • Watson
  • Conversation
  • STT
  • TTS
  • ffmpeg
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option