Skip to content

Instantly share code, notes, and snippets.

@sergiogama
Last active April 2, 2019 21:22
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 sergiogama/4d710250552f8ca3091356d972f358c5 to your computer and use it in GitHub Desktop.
Save sergiogama/4d710250552f8ca3091356d972f358c5 to your computer and use it in GitHub Desktop.
Using Watson Assistant, 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 Assistant. With Watson Assistant 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. This flow receives name and last name from facebook and send to context inside WA.

Watson assistant asset used as demo: https://ibm.biz/Bd2GBg

  1. Create 3 dervice on IBM Cloud:
    • Watson Assistant
    • Watson Speech to Text
    • Watson Text to Speech
  2. Import JSON to WAS from the following link: https://ibm.biz/Bd2GBg
  3. Import the flow from this page
  4. Get credentials from Watson services created on item 1, and configra the nodes accordingly
  5. Creat a new chatbot on Chatfuel. www.chatfuel.com Refence: https://www.youtube.com/watch?v=Oni7g2uK9PM&t=5s

Good luck!

[{"id":"a557f018.fd4b5","type":"watson-conversation-v1","z":"f84437b3.8311d8","name":"","workspaceid":"45d04fe2-f92f-43f4-a168-0e7116383694","multiuser":true,"context":true,"empty-payload":false,"default-endpoint":true,"service-endpoint":"https://gateway.watsonplatform.net/assistant/api","timeout":"","optout-learning":false,"x":680,"y":160,"wires":[["ee40c7f5.e0c4d8","bde027c5.152118"]]},{"id":"bd4be952.4447b8","type":"watson-speech-to-text","z":"f84437b3.8311d8","name":"","alternatives":1,"speakerlabels":false,"smartformatting":false,"lang":"pt-BR","langhidden":"pt-BR","langcustom":"NoCustomisationSetting","langcustomhidden":"","custom-weight":"0.5","band":"NarrowbandModel","bandhidden":"NarrowbandModel","keywords":"","keywords-threshold":"0.5","word-confidence":false,"password":"","apikey":"abszCIRwsx_k0zrXZTNAjSkLWsCISbRr-QUg0nhRA3-E","payload-response":true,"streaming-mode":false,"streaming-mute":true,"auto-connect":false,"discard-listening":false,"disable-precheck":false,"default-endpoint":true,"service-endpoint":"https://stream.watsonplatform.net/speech-to-text/api","x":368,"y":160,"wires":[["e7c11c04.2dacf"]]},{"id":"f42b53f1.04a11","type":"watson-text-to-speech","z":"f84437b3.8311d8","name":"","lang":"pt-BR","langhidden":"pt-BR","langcustom":"NoCustomisationSetting","langcustomhidden":"","voice":"pt-BR_IsabelaVoice","voicehidden":"","format":"audio/wav","password":"","apikey":"PDtZz0reYmMhkL_9Ko6tqul2auhkiJ55oPal6tNJ58Jm","payload-response":true,"default-endpoint":true,"service-endpoint":"https://stream.watsonplatform.net/text-to-speech/api","x":126,"y":325,"wires":[["b2037020.c1f38"]]},{"id":"6480505c.5d1c9","type":"http in","z":"f84437b3.8311d8","name":"","url":"/chat","method":"get","upload":false,"swaggerDoc":"","x":77.5,"y":98,"wires":[["d88d2219.e302e"]]},{"id":"b0d0d041.46667","type":"http response","z":"f84437b3.8311d8","name":"","statusCode":"","headers":{},"x":771,"y":233,"wires":[]},{"id":"d88d2219.e302e","type":"change","z":"f84437b3.8311d8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"req.query.mensagem","tot":"msg"},{"t":"set","p":"user","pt":"msg","to":"req.query.usuario","tot":"msg"},{"t":"set","p":"res","pt":"flow","to":"res","tot":"msg"},{"t":"set","p":"audio","pt":"flow","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":243,"y":98,"wires":[["63e78b55.07ed04"]]},{"id":"41882423.7c973c","type":"function","z":"f84437b3.8311d8","name":"Response","func":"response = [];\nfor(x=0;x<msg.payload.output.text.length;x++)\n{\n response[x] = {\"text\":msg.payload.output.text[x]};\n}\nmsg.payload =response;\nreturn msg;","outputs":1,"noerr":0,"x":434,"y":233,"wires":[["16ad8a9e.cd9595"]]},{"id":"63e78b55.07ed04","type":"switch","z":"f84437b3.8311d8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"https://cdn.fbsbx.com","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":406,"y":98,"wires":[["61e38676.5c3fe8"],["e7c11c04.2dacf"]]},{"id":"6556b496.655f2c","type":"link out","z":"f84437b3.8311d8","name":"Convert","links":["3e6a0ec9.0e88f2"],"x":680,"y":92,"wires":[]},{"id":"3e6a0ec9.0e88f2","type":"link in","z":"f84437b3.8311d8","name":"Convert-in","links":["6556b496.655f2c"],"x":37,"y":160,"wires":[["b11cda91.a12978"]]},{"id":"61e38676.5c3fe8","type":"function","z":"f84437b3.8311d8","name":"Audio=true","func":"flow.set(\"audio\",true);\nreturn msg;","outputs":1,"noerr":0,"x":569,"y":92,"wires":[["6556b496.655f2c"]]},{"id":"bde027c5.152118","type":"switch","z":"f84437b3.8311d8","name":"","property":"audio","propertyType":"flow","rules":[{"t":"false"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":80,"y":241,"wires":[["41882423.7c973c"],["5403f57.20f160c"]]},{"id":"b2037020.c1f38","type":"function","z":"f84437b3.8311d8","name":"Create Unique ID","func":"flow.set(\"idAudio\",0);\nflow.set(\"idAudio\", Math.floor((Math.random() * 100000000000) + 1));\nreturn msg;","outputs":1,"noerr":0,"x":320,"y":325,"wires":[["1fdafd88.928932"]]},{"id":"5967a5ea.23956c","type":"http in","z":"f84437b3.8311d8","name":"","url":"/facebook/speech","method":"get","upload":false,"swaggerDoc":"","x":129.5,"y":445,"wires":[["8c60b21a.9dd98"]]},{"id":"d8124a67.15c1c8","type":"http response","z":"f84437b3.8311d8","name":"","statusCode":"","headers":{"content-type":"audio/mpeg3"},"x":637.5000038146973,"y":445.42852687835693,"wires":[]},{"id":"8c60b21a.9dd98","type":"change","z":"f84437b3.8311d8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"req.query.audioid","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":340.2142639160156,"y":445.07142543792725,"wires":[["1914aa9b.b3dd45"]]},{"id":"1914aa9b.b3dd45","type":"function","z":"f84437b3.8311d8","name":"Response","func":"for(var k in flow.get(\"speech\")) {\n if (flow.get(\"speech\")[k]._id == msg.payload) \n {\n msg.payload = flow.get(\"speech\")[k].speech;\n// Delete an item\n flow.get(\"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":505.61905670166016,"y":445.4285430908203,"wires":[["d8124a67.15c1c8"]]},{"id":"414b2da9.3db3f4","type":"function","z":"f84437b3.8311d8","name":"Save in session","func":"var url = \"https://\" + msg.req.headers.host;\n\nif(!flow.get(\"speech\"))\n{\n flow.set(\"speech\",[]);\n flow.set(\"speech[0]\",{_id:flow.get(\"idAudio\").toString(),speech:msg.payload});\n}\nelse\n{\n var pos;\n pos = flow.get(\"speech\").length;\n flow.set(\"speech[\" + pos + \"]\",{_id:flow.get(\"idAudio\").toString(),speech:msg.payload});\n}\n\nmsg.payload = [{\"attachment\":{\n \"type\":\"audio\",\n \"payload\":{\"url\":url+ \"/facebook/speech?audioid=\"+flow.get(\"idAudio\").toString()}\n }\n}];\nreturn msg;","outputs":1,"noerr":0,"x":681,"y":325,"wires":[["16ad8a9e.cd9595"]]},{"id":"16ad8a9e.cd9595","type":"function","z":"f84437b3.8311d8","name":"Restore state","func":"msg.res = flow.get(\"res\");\nflow.set(\"audio\",false);\nreturn msg;","outputs":1,"noerr":0,"x":608,"y":233,"wires":[["b0d0d041.46667"]]},{"id":"f3665349.0e58c","type":"comment","z":"f84437b3.8311d8","name":"Callback for facebook","info":"","x":131,"y":408,"wires":[]},{"id":"e7c11c04.2dacf","type":"function","z":"f84437b3.8311d8","name":"","func":"msg.additional_context = \n{\"nome\":msg.req.query.nome,\n \"sobrenome\":msg.req.query.sobrenome};\nreturn msg;","outputs":1,"noerr":0,"x":540,"y":140,"wires":[["a557f018.fd4b5"]]},{"id":"ee40c7f5.e0c4d8","type":"debug","z":"f84437b3.8311d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":768,"y":38,"wires":[]},{"id":"b11cda91.a12978","type":"ffmpeg-conversion","z":"f84437b3.8311d8","name":"MP3 -> WAV","format":"wav","audiochannels":"mono","x":150,"y":160,"wires":[["bd4be952.4447b8"]]},{"id":"1fdafd88.928932","type":"ffmpeg-conversion","z":"f84437b3.8311d8","name":"WAV -> MP3","format":"mp3","audiochannels":"mono","x":504,"y":325,"wires":[["414b2da9.3db3f4"]]},{"id":"5403f57.20f160c","type":"function","z":"f84437b3.8311d8","name":"Response","func":"response = \"\";\nfor(x=0;x<msg.payload.output.text.length;x++)\n{\n response += '<p><s>' + msg.payload.output.text[x] + '</s></p><break time=\"1000ms\"/>';\n}\nmsg.payload =response;\nreturn msg;","outputs":1,"noerr":0,"x":216,"y":274,"wires":[["f42b53f1.04a11"]]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment