Adam Daniels

Adam Daniels

1553846895

How to build your first chatbot with Dialogflow and React Native?

#react-native #mobile-apps #javascript

What is GEEK

Buddha Community

Dylan Iqbal

1553847581

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.

Requirements

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 three 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.

// Info.plist

  <key>NSSpeechRecognitionUsageDescription</key>
  <string>Your usage description here</string>
  <key>NSMicrophoneUsageDescription</key>
  <string>Your usage description here</string>
  <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.

// App.js
import React, { Component } from 'react';  
import { StyleSheet, Text, View, Image } from 'react-native';  
import { GiftedChat } from 'react-native-gifted-chat';

class App extends Component {  
  state = {
    messages: [
      {
        _id: 1,
        text: `Hi! I am the FAQ bot 🤖 from Jscrambler.\n\nHow may I help you with today?`,
        createdAt: new Date(),
        user: {
          _id: 2,
          name: 'FAQ Bot',
          avatar: 'https://i.imgur.com/7k12EPD.png'
        }
      }
    ]
  };

  onSend(messages = []) {
    this.setState(previousState => ({
      messages: GiftedChat.append(previousState.messages, messages)
    }));
  }

  render() {
    return (
      <View style={{ flex: 1, backgroundColor: '#fff' }}>
        <GiftedChat
          messages={this.state.messages}
          onSend={messages => this.onSend(messages)}
          user={{
            _id: 1
          }}
        />
      </View>
    );
  }
}

export default App;  


We start by requiring the 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 a Natural Language Processing (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:

  • 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

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. It 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 a set of trained phrases. There are also actions and parameters that extract 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 of the dialog.

Connecting Dialogflow with React Native

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

Then click on the value next to Service Account.

Once in the service account, find the account 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.

{
  "type": "service_account",
  "project_id": "faq-bot-XXXX",
  "private_key_id": "XXXX",
  "private_key": "-----BEGIN PRIVATE KEY-----XXXX\n-----END PRIVATE KEY-----\n",
  "client_email": "XXXX",
  "client_id": "XXXX",
  "auth_uri": "XXXX",
  "token_uri": "XXXX",
  "auth_provider_x509_cert_url": "XXXX",
  "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.

// env.js

export const dialogflowConfig = {  
  type: 'service_account',
  project_id: 'faq-bot-XXXX',
  private_key_id: 'XXXX',
  private_key: '-----BEGIN PRIVATE KEY-----XXXX\n-----END PRIVATE KEY-----\n',
  client_email: 'XXXX',
  client_id: 'XXXX',
  auth_uri: 'XXXX',
  token_uri: 'XXXX',
  auth_provider_x509_cert_url: 'XXXX',
  client_x509_cert_url: 'XXXX'
};


You are now exporting the configuration object which you will be requiring among other things in the App.js file.

import { Dialogflow_V2 } from 'react-native-dialogflow';

import { dialogflowConfig } from './env';  


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

const BOT_USER = {  
  _id: 2,
  name: 'FAQ Bot',
  avatar: 'https://i.imgur.com/7k12EPD.png'
};


The state now looks like below.

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


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

componentDidMount() {  
    Dialogflow_V2.setConfiguration(
      dialogflowConfig.client_email,
      dialogflowConfig.private_key,
      Dialogflow_V2.LANG_ENGLISH_US,
      dialogflowConfig.project_id
    );
  }


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.

onSend(messages = []) {  
    this.setState(previousState => ({
      messages: GiftedChat.append(previousState.messages, messages)
    }));

    let message = messages[0].text;
    Dialogflow_V2.requestQuery(
      message,
      result => this.handleGoogleResponse(result),
      error => console.log(error)
    );
  }


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

  • 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

The function handleGoogleResponse(result) was created to handle the response coming back and then call the sendBotResponse() function.

handleGoogleResponse(result) {  
    let text = result.queryResult.fulfillmentMessages[0].text.text[0];
    this.sendBotResponse(text);
}

sendBotResponse(text) {  
    let msg = {
      _id: this.state.messages.length + 1,
      text,
      createdAt: new Date(),
      user: BOT_USER
    };

    this.setState(previousState => ({
      messages: GiftedChat.append(previousState.messages, [msg])
    }));
  }


The sendBotResponse function then updates the state of the App component and displays 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.

// App.js

import React, { Component } from 'react';  
import { StyleSheet, Text, View } from 'react-native';  
import { GiftedChat } from 'react-native-gifted-chat';  
import { Dialogflow_V2 } from 'react-native-dialogflow';

import { dialogflowConfig } from './env';

const BOT_USER = {  
  _id: 2,
  name: 'FAQ Bot',
  avatar: 'https://i.imgur.com/7k12EPD.png'
};

class App extends Component {  
  state = {
    messages: [
      {
        _id: 1,
        text: `Hi! I am the FAQ bot 🤖 from Jscrambler.\n\nHow may I help you with today?`,
        createdAt: new Date(),
        user: BOT_USER
      }
    ]
  };

  componentDidMount() {
    Dialogflow_V2.setConfiguration(
      dialogflowConfig.client_email,
      dialogflowConfig.private_key,
      Dialogflow_V2.LANG_ENGLISH_US,
      dialogflowConfig.project_id
    );
  }

  handleGoogleResponse(result) {
    let text = result.queryResult.fulfillmentMessages[0].text.text[0];
    this.sendBotResponse(text);
  }

  onSend(messages = []) {
    this.setState(previousState => ({
      messages: GiftedChat.append(previousState.messages, messages)
    }));

    let message = messages[0].text;
    Dialogflow_V2.requestQuery(
      message,
      result => this.handleGoogleResponse(result),
      error => console.log(error)
    );
  }

  sendBotResponse(text) {
    let msg = {
      _id: this.state.messages.length + 1,
      text,
      createdAt: new Date(),
      user: BOT_USER
    };

    this.setState(previousState => ({
      messages: GiftedChat.append(previousState.messages, [msg])
    }));
  }

  render() {
    return (
      <View style={{ flex: 1, backgroundColor: '#fff' }}>
        <GiftedChat
          messages={this.state.messages}
          onSend={messages => this.onSend(messages)}
          user={{
            _id: 1
          }}
        />
      </View>
    );
  }
}

export default App;  


Conclusion

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.

We hope this tutorial provided you an easy walkthrough to grab the concepts and build something of your own.

Learn More

The Complete React Native and Redux Course

React Native - The Practical Guide

The complete React Native course ( 2nd edition )

NativeScript Tutorial for Beginners - Build iOS, Android and Web Apps with NativeScript and Angular

React Native – The Future of Mobile

Build Mobile-Friendly Web Apps with React Native Web

*Originally published by Aman Mittal on *https://blog.jscrambler.com

Autumn  Blick

Autumn Blick

1598839687

How native is React Native? | React Native vs Native App Development

If you are undertaking a mobile app development for your start-up or enterprise, you are likely wondering whether to use React Native. As a popular development framework, React Native helps you to develop near-native mobile apps. However, you are probably also wondering how close you can get to a native app by using React Native. How native is React Native?

In the article, we discuss the similarities between native mobile development and development using React Native. We also touch upon where they differ and how to bridge the gaps. Read on.

A brief introduction to React Native

Let’s briefly set the context first. We will briefly touch upon what React Native is and how it differs from earlier hybrid frameworks.

React Native is a popular JavaScript framework that Facebook has created. You can use this open-source framework to code natively rendering Android and iOS mobile apps. You can use it to develop web apps too.

Facebook has developed React Native based on React, its JavaScript library. The first release of React Native came in March 2015. At the time of writing this article, the latest stable release of React Native is 0.62.0, and it was released in March 2020.

Although relatively new, React Native has acquired a high degree of popularity. The “Stack Overflow Developer Survey 2019” report identifies it as the 8th most loved framework. Facebook, Walmart, and Bloomberg are some of the top companies that use React Native.

The popularity of React Native comes from its advantages. Some of its advantages are as follows:

  • Performance: It delivers optimal performance.
  • Cross-platform development: You can develop both Android and iOS apps with it. The reuse of code expedites development and reduces costs.
  • UI design: React Native enables you to design simple and responsive UI for your mobile app.
  • 3rd party plugins: This framework supports 3rd party plugins.
  • Developer community: A vibrant community of developers support React Native.

Why React Native is fundamentally different from earlier hybrid frameworks

Are you wondering whether React Native is just another of those hybrid frameworks like Ionic or Cordova? It’s not! React Native is fundamentally different from these earlier hybrid frameworks.

React Native is very close to native. Consider the following aspects as described on the React Native website:

  • Access to many native platforms features: The primitives of React Native render to native platform UI. This means that your React Native app will use many native platform APIs as native apps would do.
  • Near-native user experience: React Native provides several native components, and these are platform agnostic.
  • The ease of accessing native APIs: React Native uses a declarative UI paradigm. This enables React Native to interact easily with native platform APIs since React Native wraps existing native code.

Due to these factors, React Native offers many more advantages compared to those earlier hybrid frameworks. We now review them.

#android app #frontend #ios app #mobile app development #benefits of react native #is react native good for mobile app development #native vs #pros and cons of react native #react mobile development #react native development #react native experience #react native framework #react native ios vs android #react native pros and cons #react native vs android #react native vs native #react native vs native performance #react vs native #why react native #why use react native

Hire Dedicated React Native Developer

Have you ever thought of having your own app that runs smoothly over multiple platforms?

React Native is an open-source cross-platform mobile application framework which is a great option to create mobile apps for both Android and iOS. Hire Dedicated React Native Developer from top React Native development company, HourlyDeveloper.io to design a spectacular React Native application for your business.

Consult with experts:- https://bit.ly/2A8L4vz

#hire dedicated react native developer #react native development company #react native development services #react native development #react native developer #react native

Juned Ghanchi

1621573085

React Native App Developers India, React Native App Development Company

Expand your user base by using react-native apps developed by our expert team for various platforms like Android, Android TV, iOS, macOS, tvOS, the Web, Windows, and UWP.

We help businesses to scale up the process and achieve greater performance by providing the best react native app development services. Our skilled and experienced team’s apps have delivered all the expected results for our clients across the world.

To achieve growth for your business, hire react native app developers in India. You can count on us for all the technical services and support.

#react native app development company india #react native app developers india #hire react native developers india #react native app development company #react native app developers #hire react native developers

Hire Dedicated React Native Developers - WebClues Infotech

Being one of the emerging frameworks for app development the need to develop react native apps has increased over the years.

Looking for a react native developer?

Worry not! WebClues infotech offers services to Hire React Native Developers for your app development needs. We at WebClues Infotech offer a wide range of Web & Mobile App Development services based o your business or Startup requirement for Android and iOS apps.

WebClues Infotech also has a flexible method of cost calculation for hiring react native developers such as Hourly, Weekly, or Project Basis.

Want to get your app idea into reality with a react native framework?

Get in touch with us.

Hire React Native Developer Now: https://www.webcluesinfotech.com/hire-react-native-app-developer/

For inquiry: https://www.webcluesinfotech.com/contact-us/

Email: sales@webcluesinfotech.com

#hire react native developers #hire dedicated react native developers #hire react native developer #hiring a react native developer #hire freelance react native developers #hire react native developers in 1 hour

Factors affecting the cost of hiring a React Native developer in USA - TopDevelopers.co

Want to develop app using React Native? Here are the tips that will help to reduce the cost of react native app development for you.
Cost is a major factor in helping entrepreneurs take decisions about investing in developing an app and the decision to hire react native app developers in USA can prove to be fruitful in the long run. Using react native for app development ensures a wide range of benefits to your business. Understanding your business and working on the aspects to strengthen business processes through a cost-efficient mobile app will be the key to success.

#best react native development companies from the us #top react native app development companies in usa #cost of hiring a react native developer in usa #top-notch react native developer in usa #best react native developers usa #react native