Build a Chatbot with Dialogflow and React Native

Published on Mar 26, 2019

10 min read



Chatbots are a powerful way to provide conversational experiences for any software product. Each conversational experience depends on the implementation of the chatbot to either be a good or poor experience for the end user. The modern day world is living in the technology wave of Artificial Intelligence and bots are a huge part of it.

In this tutorial, we are going to build a chatbot application from scratch using Dialogflow and React Native. The main reason to use Google's Dialogflow for this tutorial is that you do not have to go through a hefty signup process by providing your card details, unlike other bot frameworks or similar service providers.

What are we going to build? Let us build a chatbot that returns the current date when asked in different ways.

The complete code for this tutorial can be found inside this Github repository.



In order to follow this tutorial, you will need:

  • react-native-cli version 2.0.1 or above available via npm
  • Knowledge of React, React Native and JavaScript
  • A Google account
  • react-native-gifted-chat, which provides a customizable and complete chat UI interface
  • react-native-dialogflow, which will help us bridge our app with Google Dialogflow’s SDK

Getting Started


In order to get started, the first requirement to use react-native-cli and create a new project directory. Run the following command from your terminal.

react-native init RNDiagflowChatbot
# traverse inside the directory
cd RNDiagflowChatbot

Also, make sure that you are now inside the project directory in your terminal window. This step is required since we are going to add two npm packages that are going to help us build our Chatbot app.

npm install --save react-native-gifted-chat react-native-dialogflow react-native-voice

Note that we are not going to use react-native-voice directly but you are required to install and link to the React Native project. The reason is react-native-dialogflow requires react-native-voice as a peer dependency. The next step is to link the Dialogflow SDK library to the React Native project.

react-native link react-native-dialogflow
react-native link react-native voice

You will get a success message when the linking process is complete. Also, to prevent the application from crashing later when we are running it, you have to add some permissions for iOS inside the file iOS/RNDiagflowChatbot/Info.plist, inside the root <dict> tag.

1// Info.plist
3 <key>NSSpeechRecognitionUsageDescription</key>
4 <string>Your usage description here</string>
5 <key>NSMicrophoneUsageDescription</key>
6 <string>Your usage description here</string>
7 <key>UIRequiredDeviceCapabilities</key>

Now, let us move on to create the first chat component. We will be using the App component inside the App.js file but you are most welcome to refactor your code later.

1// App.js
2import React, { Component } from 'react';
3import { StyleSheet, Text, View, Image } from 'react-native';
4import { GiftedChat } from 'react-native-gifted-chat';
6class App extends Component {
7 state = {
8 messages: [
9 {
10 _id: 1,
11 text: `Hi! I am the FAQ bot 🤖 from Jscrambler.\n\nHow may I help you with today?`,
12 createdAt: new Date(),
13 user: {
14 _id: 2,
15 name: 'FAQ Bot',
16 avatar: ''
17 }
18 }
19 ]
20 };
22 onSend(messages = []) {
23 this.setState(previousState => ({
24 messages: GiftedChat.append(previousState.messages, messages)
25 }));
26 }
28 render() {
29 return (
30 <View style={{ flex: 1, backgroundColor: '#fff' }}>
31 <GiftedChat
32 messages={this.state.messages}
33 onSend={messages => this.onSend(messages)}
34 user={{
35 _id: 1
36 }}
37 />
38 </View>
39 );
40 }
43export default App;

We start by requiring necessary components, including GiftedChat from the react-native-gifted-chat package. In the component's state, you will find one static or welcome message whenever the component gets rendered initially.

The createdAt time will display the current time and date in the chat UI. The user object is the user sending messages — in our case, the bot. It is defined with properties like username, its unique ID, and an avatar. The react-native-gifted-chat automatically adds a circle avatar in the UI.

The line <View style={{ flex: 1, backgroundColor: '#fff' }}> in the render function shows that you can add your own custom styling along using Gifted Chat's components. The GiftedChat component can take props like messages from our component's initial state, an onSend prop that is a callback function used when sending the message, and the user ID of the message.

This is how easy to implement a chat interface in a React Native app. To run your app in an iOS simulator, run the command react-native run-ios. For Android users, run the command react-native run-android and do make sure you have the Android emulator (or commonly known as Android Virtual Device) running the background.

See the current state of our application in action below.

Google's Dialogflow Setup


Dialogflow is an NLP service from Google. It has many integrations, SDKs for many languages and prebuilt agents. It works very straightforward with Google Assistant. Visit the Dialogflow website and create a new account or log-in with your existing Google ID. Once you are logged in, you will be welcomed by a screen that consists of different Agents.

Click on the Create Agent button to make one. We are going to name our agent: faq-bot. Fill in the details like below.

After filling out the details, click on the button Create.

Generally, for small applications you will have one agent. In Dialogflow, the basic flow of conversation involves these steps:

  • The user giving input
  • Your Dialogflow agent parsing that input
  • Your agent returning a response to the user

These agents can understand the vast and varied nuances of human language and translate that to standard and structured meaning that your apps and services can understand. Each agent contains different intents.

An intent is the action or the response sent back to the user in the chatbot application. An intent can contain different types of responses or actions. The next step in the process is to create your first intent.

An intent can be a simple text response that is displayed back to the user or trained phrases to match a specific intent. There are also actions and parameters that extract parameters or information from user queries. Examples of this kind of information include dates, times, names, places, and more.

Right now, we do not have any intent created of our own. Let us create one in the next section.

First Dialogflow Intent


Let us add our first intent which we are going to call date.current. The purpose of this intent is to return the current date back to the user when asked. We can add pre-defined common training phrases to identify what the user is asking to our bot. Take a look below at the following training phrases.

Since “date” as a keyword is available in Dialogflow's API, it is automatically considered to be a parameter to take action on.

The Last step in this process is to add a text response for the user to receive.

Do not forget to click the Save button at the top.

Connecting Dialogflow with React Native


We need a few keys to use Dialogflow's npm package with our app. Right now, from the console window, go to Settings (it is an icon next to the agent's name).

Then click on the value next to Service Account.

Once in the service account, find the account that named Dialogflow Integrations, and scroll to the right until you see the three dots. Click on this menu, and click Create Key.


Download it as JSON file, and open that file in an editor. You will find the following content in this file.

2 "type": "service_account",
3 "project_id": "faq-bot-XXXX",
4 "private_key_id": "XXXX",
5 "private_key": "-----BEGIN PRIVATE KEY-----XXXX\n-----END PRIVATE KEY-----\n",
6 "client_email": "XXXX",
7 "client_id": "XXXX",
8 "auth_uri": "XXXX",
9 "token_uri": "XXXX",
10 "auth_provider_x509_cert_url": "XXXX",
11 "client_x509_cert_url": "XXXX"

Every key in the JSON file will have its value (in place of XXXX) some of them are sensitive.

Building the Chatbot


Now, create a new file called env.js and inside place the same values as above.

1// env.js
3export const dialogflowConfig = {
4 type: 'service_account',
5 project_id: 'faq-bot-XXXX',
6 private_key_id: 'XXXX',
7 private_key: '-----BEGIN PRIVATE KEY-----XXXX\n-----END PRIVATE KEY-----\n',
8 client_email: 'XXXX',
9 client_id: 'XXXX',
10 auth_uri: 'XXXX',
11 token_uri: 'XXXX',
12 auth_provider_x509_cert_url: 'XXXX',
13 client_x509_cert_url: 'XXXX'

Next, export the configuration object. You will be requiring it among other things in the App.js file.

1import { Dialogflow_V2 } from 'react-native-dialogflow';
3import { dialogflowConfig } from './env';

Also, we are refactoring the user object by separating it from the state like below.

1const BOT_USER = {
2 _id: 2,
3 name: 'FAQ Bot',
4 avatar: ''

The state now looks like below.

1state = {
2 messages: [
3 {
4 _id: 1,
5 text: `Hi! I am the FAQ bot 🤖 from Jscrambler.\n\nHow may I help you with today?`,
6 createdAt: new Date(),
7 user: BOT_USER // <= note this
8 }
9 ]

Let us now use a lifecycle method componendDidMount to apply Dialogflow's configuration.

1componentDidMount() {
2 Dialogflow_V2.setConfiguration(
3 dialogflowConfig.client_email,
4 dialogflowConfig.private_key,
5 Dialogflow_V2.LANG_ENGLISH_US,
6 dialogflowConfig.project_id
7 );
8 }

All the values inside Dialogflow_V2.setConfiguration() are coming from the env.js file. In the onSend function, you have to make sure that it sends the text of the message to the Dialogflow agent. Refactor it like below.

1onSend(messages = []) {
2 this.setState(previousState => ({
3 messages: GiftedChat.append(previousState.messages, messages)
4 }));
6 let message = messages[0].text;
7 Dialogflow_V2.requestQuery(
8 message,
9 result => this.handleGoogleResponse(result),
10 error => console.log(error)
11 );
12 }

The method Dialogflow_V2.requestQuery is used to send a text request to the agent. It contains three parameters:

  • the text itself as the first parameter; in our case message
  • the result and error callback functions

The function handleGoogleResponse(result) handles the response coming back and further calls the sendBotResponse() function.

1handleGoogleResponse(result) {
2 let text = result.queryResult.fulfillmentMessages[0].text.text[0];
3 this.sendBotResponse(text);
6sendBotResponse(text) {
7 let msg = {
8 _id: this.state.messages.length + 1,
9 text,
10 createdAt: new Date(),
11 user: BOT_USER
12 };
14 this.setState(previousState => ({
15 messages: GiftedChat.append(previousState.messages, [msg])
16 }));
17 }

The sendBotResponse function then updates the state of the App component and display whatever response back to the user in the chat interface. See the application in action below.


You can find the complete code for App.js below.

1// App.js
3import React, { Component } from 'react';
4import { StyleSheet, Text, View } from 'react-native';
5import { GiftedChat } from 'react-native-gifted-chat';
6import { Dialogflow_V2 } from 'react-native-dialogflow';
8import { dialogflowConfig } from './env';
10const BOT_USER = {
11 _id: 2,
12 name: 'FAQ Bot',
13 avatar: ''
16class App extends Component {
17 state = {
18 messages: [
19 {
20 _id: 1,
21 text: `Hi! I am the FAQ bot 🤖 from Jscrambler.\n\nHow may I help you with today?`,
22 createdAt: new Date(),
23 user: BOT_USER
24 }
25 ]
26 };
28 componentDidMount() {
29 Dialogflow_V2.setConfiguration(
30 dialogflowConfig.client_email,
31 dialogflowConfig.private_key,
32 Dialogflow_V2.LANG_ENGLISH_US,
33 dialogflowConfig.project_id
34 );
35 }
37 handleGoogleResponse(result) {
38 let text = result.queryResult.fulfillmentMessages[0].text.text[0];
39 this.sendBotResponse(text);
40 }
42 onSend(messages = []) {
43 this.setState(previousState => ({
44 messages: GiftedChat.append(previousState.messages, messages)
45 }));
47 let message = messages[0].text;
48 Dialogflow_V2.requestQuery(
49 message,
50 result => this.handleGoogleResponse(result),
51 error => console.log(error)
52 );
53 }
55 sendBotResponse(text) {
56 let msg = {
57 _id: this.state.messages.length + 1,
58 text,
59 createdAt: new Date(),
60 user: BOT_USER
61 };
63 this.setState(previousState => ({
64 messages: GiftedChat.append(previousState.messages, [msg])
65 }));
66 }
68 render() {
69 return (
70 <View style={{ flex: 1, backgroundColor: '#fff' }}>
71 <GiftedChat
72 messages={this.state.messages}
73 onSend={messages => this.onSend(messages)}
74 user={{
75 _id: 1
76 }}
77 />
78 </View>
79 );
80 }
83export default App;



The possibilities of using a powerful API such as Dialogflow are endless. In no time, you can build up your own chatbot interface inside a React Native application as a valuable support or marketing tool.

Originally published at Jscrambler

More Posts

Browse all posts

Aman Mittal author

I'm a software developer and a technical writer. On this blog, I write about my learnings in software development and technical writing.

Currently, working maintaining docs at 𝝠 Expo. Read more about me on the About page.

Copyright ©  2019-2024 Aman Mittal · All Rights Reserved.