|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Dialogflow With ##Platform_Name## Chat UI control | Syncfusion |
| 4 | +description: Checkout and learn about Integration of Google Dialogflow With ##Platform_Name## Chat UI control of Syncfusion Essential JS 2 and more. |
| 5 | +platform: ej2-javascript |
| 6 | +control: Chat UI |
| 7 | +publishingplatform: ##Platform_Name## |
| 8 | +documentation: ug |
| 9 | +domainurl: ##DomainURL## |
| 10 | +--- |
| 11 | + |
| 12 | +# Integration of Google Dialogflow With ##Platform_Name## Chat UI control |
| 13 | + |
| 14 | +The Syncfusion Chat UI supports integration with [Google Dialogflow](https://cloud.google.com/dialogflow/docs), enabling advanced conversational AI features in your applications. |
| 15 | + |
| 16 | +## Getting Started With the ChatUI control |
| 17 | + |
| 18 | +Before integrating Dialogflow, ensure that the Syncfusion Chat UI control is correctly rendered in your application: |
| 19 | +[ ##Platform_Name## Getting Started Guide](../getting-started) |
| 20 | + |
| 21 | +## Prerequisites |
| 22 | + |
| 23 | +* Google account to access `Dialogflow` and `Google Cloud Console`. |
| 24 | +* Backend requires `Node.js` (v16 or higher) and `npm`. |
| 25 | +* Syncfusion Chat UI for ##Platform_Name## `@syncfusion/ej2-interactive-chat` installed in your project. |
| 26 | +* Dialogflow Service Account with the `Dialogflow API Client` role and its JSON key file. |
| 27 | + |
| 28 | +## Install Dependencies |
| 29 | + |
| 30 | +* Install `backend` dependencies for Dialogflow and server setup: |
| 31 | + |
| 32 | +```bash |
| 33 | + |
| 34 | +npm install express body-parser dialogflow cors |
| 35 | + |
| 36 | +``` |
| 37 | +* Install the Syncfusion Chat UI in your project: |
| 38 | + |
| 39 | +```bash |
| 40 | + |
| 41 | +npm install @syncfusion/ej2-interactive-chat --save |
| 42 | + |
| 43 | +``` |
| 44 | + |
| 45 | +## Set Up the Dialogflow Agent |
| 46 | + |
| 47 | +1. In the Dialogflow console, create an [agent](https://cloud.google.com/agent-assist/docs), set a name (e.g., `MyChatBot`), and configure the default language (e.g., English - `en`). |
| 48 | + |
| 49 | +2. Add intents with training phrases and responses (e.g., greetings, FAQs). Test using the Dialogflow simulator. |
| 50 | + |
| 51 | +3. In the Google Cloud Console, go to `APIs & Services` > `Credentials`, create a Service Account with the Dialogflow API Client role, and download the JSON key file. |
| 52 | + |
| 53 | +> `Security Note`: Never commit the JSON key file to version control. Use environment variables or a secret manager (e.g., Google Cloud Secret Manager) for production. |
| 54 | +
|
| 55 | +## Configure Node.js Backend |
| 56 | + |
| 57 | +Create `backend/service-acct.json` with your Dialogflow service account credentials: |
| 58 | + |
| 59 | +{% tabs %} |
| 60 | +{% highlight js tabtitle="service-acct.json" %} |
| 61 | +{ |
| 62 | + "type": "service_account", |
| 63 | + "project_id": "your-dialogflow-project-id", |
| 64 | + "private_key_id": "abc123xyz...", |
| 65 | + "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEv...", |
| 66 | + "client_email": "dialogflow-agent@your-dialogflow-project-id.iam.gserviceaccount.com", |
| 67 | + ... |
| 68 | +} |
| 69 | +{% endhighlight %} |
| 70 | +{% endtabs %} |
| 71 | + |
| 72 | +Set up an Express server in `backend/index.js` to handle Dialogflow requests: |
| 73 | + |
| 74 | +{% tabs %} |
| 75 | +{% highlight js tabtitle="index.js" %} |
| 76 | + |
| 77 | +const express = require('express'); |
| 78 | +const { SessionsClient } = require('dialogflow'); |
| 79 | +const bodyParser = require('body-parser'); |
| 80 | +const cors = require('cors'); |
| 81 | +const serviceAccount = require('./service-acct.json'); |
| 82 | + |
| 83 | +const app = express(); |
| 84 | +app.use(cors()); |
| 85 | +app.use(bodyParser.json()); |
| 86 | + |
| 87 | +const projectId = serviceAccount.project_id; |
| 88 | +const sessionClient = new SessionsClient({ credentials: serviceAccount }); |
| 89 | + |
| 90 | +app.post('/api/message', async (req, res) => { |
| 91 | + const message = req.body.text; |
| 92 | + const sessionId = req.body.sessionId || 'default-session'; |
| 93 | + |
| 94 | + const sessionPath = `projects/${projectId}/agent/sessions/${sessionId}`; |
| 95 | + |
| 96 | + const request = { |
| 97 | + session: sessionPath, |
| 98 | + queryInput: { |
| 99 | + text: { |
| 100 | + text: message, |
| 101 | + languageCode: 'en-US', |
| 102 | + }, |
| 103 | + }, |
| 104 | + }; |
| 105 | + |
| 106 | + try { |
| 107 | + const responses = await sessionClient.detectIntent(request); |
| 108 | + const result = responses[0].queryResult; |
| 109 | + res.json({ reply: result.fulfillmentText }); |
| 110 | + } catch (err) { |
| 111 | + console.error('Dialogflow error:', err); |
| 112 | + res.status(500).json({ reply: "Error connecting to Dialogflow." }); |
| 113 | + } |
| 114 | +}); |
| 115 | + |
| 116 | +app.listen(5000, () => console.log('Backend running on http://localhost:5000')); |
| 117 | + |
| 118 | +{% endhighlight %} |
| 119 | +{% endtabs %} |
| 120 | + |
| 121 | +> Use a unique `sessionId` (e.g., UUID) for each user to maintain conversation context. |
| 122 | +
|
| 123 | +## Integrate ChatUI in ##Platform_Name## |
| 124 | + |
| 125 | +Use the Chat UI `messageSend` event to exchanges message. Each time a user sends a message, this event will be invoked with details of the sent message. |
| 126 | + |
| 127 | +### Forward Message to backend: |
| 128 | + |
| 129 | +Upon message submission, a POST request is sent to your backend API endpoint (`/api/message`). This backend service forwards the user’s message to Dialogflow and waits for a response. |
| 130 | + |
| 131 | +### Displaying Bot response: |
| 132 | + |
| 133 | +You can add the below respective files in your application: |
| 134 | + |
| 135 | +{% if page.publishingplatform == "typescript" %} |
| 136 | + |
| 137 | +{% tabs %} |
| 138 | +{% highlight ts tabtitle="index.ts" %} |
| 139 | +{% include code-snippet/chat-ui/dialogflow/index.ts %} |
| 140 | +{% endhighlight %} |
| 141 | +{% highlight html tabtitle="index.html" %} |
| 142 | +{% include code-snippet/chat-ui/dialogflow/index.html %} |
| 143 | +{% endhighlight %} |
| 144 | +{% endtabs %} |
| 145 | + |
| 146 | +{% elsif page.publishingplatform == "javascript" %} |
| 147 | + |
| 148 | +{% tabs %} |
| 149 | +{% highlight js tabtitle="index.js" %} |
| 150 | +{% include code-snippet/chat-ui/dialogflow/index.js %} |
| 151 | +{% endhighlight %} |
| 152 | +{% highlight html tabtitle="index.html" %} |
| 153 | +{% include code-snippet/chat-ui/dialogflow/index.html %} |
| 154 | +{% endhighlight %} |
| 155 | +{% endtabs %} |
| 156 | + |
| 157 | +{% endif %} |
| 158 | + |
| 159 | +## Run and Test |
| 160 | + |
| 161 | +### Start the Backend server: |
| 162 | + |
| 163 | +Navigate to your backend project folder and run the following command to start the Node.js server: |
| 164 | + |
| 165 | +```bash |
| 166 | + |
| 167 | +node index.js |
| 168 | + |
| 169 | +``` |
| 170 | + |
| 171 | +### Start the ##Platform_Name## frontend: |
| 172 | + |
| 173 | +In a separate terminal window, navigate to your project folder and start the development server: |
| 174 | + |
| 175 | +```bash |
| 176 | + |
| 177 | +npm start |
| 178 | + |
| 179 | +``` |
| 180 | +Open your app and chat with your Dialogflow-powered bot. |
| 181 | + |
| 182 | + |
| 183 | + |
| 184 | +## Troubleshooting: |
| 185 | + |
| 186 | +* `Permission Denied`: Ensure the service account has the `Dialogflow API Client` role in the Google Cloud Console. |
| 187 | +* `CORS Error`: Verify that the CORS origin in backend/index.js matches your frontend URL (e.g., http://localhost:3000). |
| 188 | +* `No Response`: Test intents in the Dialogflow Console simulator to ensure they are configured correctly. |
| 189 | +* `Quota Exceeded`: Check Dialogflow API quotas in the Google Cloud Console. |
| 190 | +* `Network Issues`: Confirm the backend server is running and the frontend is pointing to the correct URL (e.g., http://localhost:5000). |
| 191 | +* `Invalid Credentials`: Verify the service account JSON or environment variables are correctly configured. |
0 commit comments