├── .gitignore ├── README.md ├── app.json ├── index.js ├── package-lock.json └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Webhook-and-API.AI-Sample - Echo your text 2 | 3 | A very simple step by step demo (see video at the end of read me) of implementing an echo service using webhook. Whatever user asks Google Assistant will be sent to your API and then same text will be sent back as speech text. 4 | 5 | This simple app will help you understand 6 | - How to write an API in node 7 | - How to integrate your node JS app with DialogFlow (API.AI) as webhook 8 | 9 | # Deploy to: 10 | [![Deploy to Heroku](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy) 11 | 12 | # How it works? 13 | - Push this app to Heroku 14 | - Test the API https://webhook-echo-sample.herokuapp.com/echo - download postman from https://www.getpostman.com/ to test it 15 | 16 | Open DialogFlow portal - https://dialogflow.com/ 17 | - Create an account and login 18 | 19 | # Video 20 | Watch this video to learn the steps to create sample application. 21 | 22 | [![IMAGE ALT TEXT HERE](https://img.youtube.com/vi/KcEo0eKDLpI/0.jpg)](https://www.youtube.com/watch?v=KcEo0eKDLpI&t=8s) 23 | -------------------------------------------------------------------------------- /app.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Webhook-echo-sample", 3 | "description": "Webhook echo sample", 4 | "repository": "https://github.com/SunilSyal/Webhook-and-API.AI-Samples.git", 5 | "keywords": ["api.ai", "dialogflow", "natural language"] 6 | } 7 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const express = require("express"); 4 | const bodyParser = require("body-parser"); 5 | 6 | const restService = express(); 7 | //const cors = require('cors'); 8 | 9 | // const corsOptions = { 10 | // origin: ['https://mathemafia.com', 'http://yourdomain2.com'], 11 | // }; 12 | 13 | // restService.use(cors(corsOptions)); 14 | 15 | restService.use( 16 | bodyParser.urlencoded({ 17 | extended: true, 18 | }) 19 | ); 20 | 21 | restService.use(bodyParser.json()); 22 | 23 | restService.use(function (req, res, next) { 24 | // Website you wish to allow to connect 25 | res.setHeader( 26 | "Access-Control-Allow-Origin", 27 | "https://mathemafia.com", 28 | "http://mathemafia.com" 29 | ); 30 | 31 | // Request methods you wish to allow 32 | res.setHeader("Access-Control-Allow-Methods", "GET, POST"); 33 | 34 | // Pass to next layer of middleware 35 | next(); 36 | }); 37 | 38 | restService.get("/test1", function (req, res) { 39 | var resObj = { 40 | org: req.headers.origin, 41 | host: req.headers.host, 42 | }; 43 | 44 | res.end(JSON.stringify(req.headers)); 45 | }); 46 | 47 | restService.post("/echo", function (req, res) { 48 | var speech = 49 | req.body.queryResult && 50 | req.body.queryResult.parameters && 51 | req.body.queryResult.parameters.echoText 52 | ? req.body.queryResult.parameters.echoText 53 | : "Seems like some problem. Speak again."; 54 | 55 | var speechResponse = { 56 | google: { 57 | expectUserResponse: true, 58 | richResponse: { 59 | items: [ 60 | { 61 | simpleResponse: { 62 | textToSpeech: speech, 63 | }, 64 | }, 65 | ], 66 | }, 67 | }, 68 | }; 69 | 70 | return res.json({ 71 | payload: speechResponse, 72 | //data: speechResponse, 73 | fulfillmentText: speech, 74 | speech: speech, 75 | displayText: speech, 76 | source: "webhook-echo-sample", 77 | }); 78 | }); 79 | 80 | restService.post("/audio", function (req, res) { 81 | var speech = ""; 82 | switch (req.body.result.parameters.AudioSample.toLowerCase()) { 83 | //Speech Synthesis Markup Language 84 | case "music one": 85 | speech = 86 | ''; 87 | break; 88 | case "music two": 89 | speech = 90 | ''; 91 | break; 92 | case "music three": 93 | speech = 94 | ''; 95 | break; 96 | case "music four": 97 | speech = 98 | ''; 99 | break; 100 | case "music five": 101 | speech = 102 | ''; 103 | break; 104 | case "delay": 105 | speech = 106 | 'Let me take a break for 3 seconds. I am back again.'; 107 | break; 108 | //https://www.w3.org/TR/speech-synthesis/#S3.2.3 109 | case "cardinal": 110 | speech = '12345'; 111 | break; 112 | case "ordinal": 113 | speech = 114 | 'I stood 10 in the class exams.'; 115 | break; 116 | case "characters": 117 | speech = 118 | 'Hello is spelled as Hello'; 119 | break; 120 | case "fraction": 121 | speech = 122 | 'Rather than saying 24+3/4, I should say 24+3/4'; 123 | break; 124 | case "bleep": 125 | speech = 126 | 'I do not want to say F&%$# word'; 127 | break; 128 | case "unit": 129 | speech = 130 | 'This road is 50 foot wide'; 131 | break; 132 | case "verbatim": 133 | speech = 134 | 'You spell HELLO as hello'; 135 | break; 136 | case "date one": 137 | speech = 138 | 'Today is 2017-12-16'; 139 | break; 140 | case "date two": 141 | speech = 142 | 'Today is 16-12'; 143 | break; 144 | case "date three": 145 | speech = 146 | 'Today is 16-12-2017'; 147 | break; 148 | case "time": 149 | speech = 150 | 'It is 2:30pm now'; 151 | break; 152 | case "telephone one": 153 | speech = 154 | '09012345678 '; 155 | break; 156 | case "telephone two": 157 | speech = 158 | '(781) 771-7777 '; 159 | break; 160 | // https://www.w3.org/TR/2005/NOTE-ssml-sayas-20050526/#S3.3 161 | case "alternate": 162 | speech = 163 | 'IPL stands for IPL'; 164 | break; 165 | case "radio": 166 | speech = 167 | '