DEV Community

Cover image for DialogFlow Chatbot with NewBot Framework
Samuel
Samuel

Posted on

DialogFlow Chatbot with NewBot Framework

The goal: to create a chatbot quickly in Javascript with the understanding of the natural language (DialogFlow).

Prerequisites

  • Have a Google Cloud and DialogFlow account
  • Have NodeJS and NPM installed on your machine
  • Knowledge of Javascript

Why NewBot Framework?

  • Universal, a single code to work everywhere
  • Creation of conversational website
  • Modular and structured
  • Using the NLP (Natural Language Processing)
  • Conversational script syntax to write user-chatbot conversations easily and quickly
  • Internationalization
  • Unit tests
  • Emulator with NewBot CLI

Compatibility

All platforms, i.e.

  • Facebook Messenger
  • Google Assistant
  • Amazon Alexa
  • Slack
  • Telegram
  • Viber
  • Skype
  • etc.

Above all

Install NewBot CLI to take advantage of command lines

npm install -g newbot-cli
Enter fullscreen mode Exit fullscreen mode

Create a project

newbot new <your directory name>
Enter fullscreen mode Exit fullscreen mode

Go to the generated folder

You can test the chatbot with the command newbot serve. See below

Retrieve the credentials of DialogFlow

Have a Google Cloud and DialogFlow account

DialogFlow Account

Click on the service in Google Cloud

DialogFlow Account 2

Create new Key

Create Key

Download the private key and add it to the root of the project

Add DialogFlow in our project

Install the newbot-dialogflow module :

npm install newbot-dialogflow
Enter fullscreen mode Exit fullscreen mode

In the main.js file, add the DialogFlow skill :

import dialogflowSkill from 'newbot-dialogflow'
import code from './main.converse'

export default {
    code,
    skills: {
        dialogflow: dialogflowSkill({
            projectId: 'newbot-fttkoh',
            credentials: 'newbot-fttkoh-69d17227a8b7.json'
        })
    }
}
Enter fullscreen mode Exit fullscreen mode
  • projectId : The project ID (in Chatbot Settings)
  • credentials: Path to credentials file

Input unknown

Add this code to the main.converse file :

@Intent('input.unknown')
unknown() {
    > { :intent.response }
}
Enter fullscreen mode Exit fullscreen mode
  • An input.unknow intention is already present in DialogFlow, it is triggered when no other intention is triggered
  • We then display the response received from DialogFlow

:intent.date.value is a magic variable that retrieves the date entered by the user

The language used is specific to NewBot. It is called ConverseScript and allows you to create conversational scripts with a syntax close to Javascript
More : https://newbot.io/docs/syntax/variables.html

Add our own intention: we buy a pizza!

  1. Create a new intent in DialogFlow
  2. Put a name ("Buy Pizza" by example)
  3. Add trainings phrases
  4. Add action name. Here, this name is input.buy
  5. You can add parameters. Here, we recover the date in the sentence
  6. Add response(s)

Create Intent
Add responses

main.converse content contains a new intention!

@Intent('input.buy')
buy() {
    date = :intent.date.value
    > { :intent.response }
    callApi(date)
}


@Intent('input.unknown')
unknown() {
    > { :intent.response }
}

Enter fullscreen mode Exit fullscreen mode

We retrieve the date entity and call a function that can trigger an API

callApi() is a function created by us, present in Javascript :

main.js

import dialogflowSkill from 'newbot-dialogflow'
import code from './main.converse'

export default {
    code,
    skills: {
        dialogflow: dialogflowSkill({
            projectId: 'newbot-fttkoh',
            credentials: 'newbot-fttkoh-69d17227a8b7.json'
        })
    },
    functions: {
        callApi(date) {
            console.log(`Call your own API. date is ${date}`)
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

Test in Emulator

Type the following command at the root of your project

newbot serve
Enter fullscreen mode Exit fullscreen mode

Go to localhost:3000 and test the project in the emulator

Documentation

Here : https://newbot.io/en/docs

Top comments (0)