Creating an Ionic AI Chatbot with Dialogflow (or API.AI)

Artificial Intelligence (AI) is everywhere. I can’t spend one day without hearing about one new AI feature, they are even featured in my video games!

No wonder Elon Musk is accelerating his Mars colonization project.

ionic artificial intelligence Dialogflow

Artificial Intelligence can be quite complicated, but new platforms are created to simplify it.
The Dialogflow Company created a simple conversational platform. By using this type of tools many tasks can be automated and we can reduce the employees charge (even eliminate it in the best case).

In this tutorial, we are going to use this AI platform to create an Ionic application that can (almost) replace an entire call center.

Note before starting, Dialogflow is the new name for API.AI so you might see some screenshots referencing the old name.

A simple Dialogflow example

It all starts at the Dialogflow website where an account can be created. As you can see, the interface is quite familiar and that’s because Google bought it. As usual when something is  great, Google buys it.

Once the account is created, we can go to the console.

Before diving in this service, let’s have a look at how Artificial Intelligence works.

One good example is comparing it to humans.

Can you multiple 9 by 9?

You can either go from 9 * 10 = 90 then subtract 9 to get 81, or use the famous trick:

ionic artificial intelligence ai human exmaple

We, human beings, have a tendency to look at how things work and generally extrapolate an algorithm to alleviate the charge on our memory.

AIs are quite similar. They also have algorithms, Dialogflow uses Machine Learning (ML) algorithms to understand what a user is trying to say (Intent). Those algorithms are evolving very fast. AIs can be compared to dogs and we are the dog trainers telling them to sit 200 times a day then giving them rewards when they do it successfully. However, those training sessions are extremely fast. It can take a week to tame and teach tricks to a dog, for an AI this can be done in hours.

All of that depends on how much training data we have (this can be unlimited if we have a company like Facebook or Google) and the processing power (that can easily be acquired thanks to Cloud Computing).

As you can see, it’s the perfect time to invest in this technology.

Let’s go back to the Dialogflow service.

The first thing we see is the Hotel Booking Agent.
An agent will respond to our demands. This one was built to help us understand how to use the Dialogflow service.

We are going to create our own agents:

ionic artificial intelligence Dialogflow create agent

The first one will be very simple. Our aim is to create an AI agent that can say “Hello” and “Goodbye” then use it in the Ionic application.

The creation is very basic:

ionic artificial intelligence API.AI Dialogflow describe agent

Our AI agent will need some intents that will help us understand what the user wants to say and how it should answer back.

We create the “Welcome” intent first:

ionic artificial intelligence API.AI Dialogflow create intent

And populate it:

ionic artificial intelligence API.AI Dialogflow fill intent

When the user will say one of the following sentences:

  • Good Morning
  • Hello
  • Hi

The AI agent will answer with one of the following sentences:

  • Hello
  • Hi

Intents must be separated, we can’t create one big intent and put everything inside.

We do the same for the “Goodbye” intent:

ionic artificial intelligence API.AI Dialogflow create 2nd intent ionic artificial intelligence API.AI Dialogflow fill 2nd intent

We can now directly test how the AI agent answers to the user by using the conversation area on the right:

ionic artificial intelligence API.AI Dialogflow agent test 1

The agent is responding accordingly.

We can even push it further:

ionic artificial intelligence API.AI Dialogflow agent test 2

Even if the words don’t completely match what we entered before, the Machine Learning (ML) algorithm understands what the user is trying to say.

We can now connect Dialogflow to Ionic.

The Ionic Application

Let’s start a new Ionic project:

And install the Dialogflow Cordova plugin:

Note that since we are using the Cordova plugin, this is only working on a simulator or a real device.
The home.html is quite simple:

We have:

  1. An input to type a question
  2. A binding showing the answer property
  3. A button that will trigger the ask method by passing the question value

The home.ts file is a bit more challenging.

Let’s start with the imports and injections:

The ApiAIPromises is available thanks to the Dialogflow Cordova plugin. However, we need to declare it so TypeScript give us some leeway later on.
APIAIPlugin is also available if you prefer to work with callbacks.

The first important import is NgZone, Angular doesn’t care about what is happening with Cordova. We will use NgZone to notify Angular that a property has changed and the template needs an update.

The whole code will run once the device is ready thanks to the Platform Service.

The ApiAIPromises init method will connect our Ionic application to the Dialogflow project, it only needs a client access token which is available there:

ionic artificial intelligence API.AI Dialogflow client access token

The final step, the ask method:

The ApiAIPromises requestText method will pass our query to the Dialogflow server.
The answer property will finally be updated by using the content of the speech field and ngZone will tell Angular to update the template.

We can now talk to our Dialogflow bot in our Ionic application!

ionic artificial intelligence API.AI Dialogflow hello result

As I stated before, AI are dogs that need training.

There is a training section in Dialogflow that allows us to do it manually:
ionic artificial intelligence API.AI Dialogflow trainingWe can attach the user’s questions to other intents in order to tell our dog AI that something should be different.
In AI programming training is done by telling the AI over X iterations which answer should be matched to which data. I don’t have access to Dialogflow’s algorithm, it’s highly likely that by clicking this button, the agent is told 10000 times that “Hello” should be matched with the “Welcome” intent (we will dive deeper into this in another tutorial).

Spicing things up

Having an AI that can only say “Hello” and “Goodbye” is quite boring.

As a digital nomad, I have spent many hours/days in offices, grabbing tickets at counters waiting for my turn to get some information on visas and immigration related subjects.
All of those offices have 10-20 employees answering the same questions days after days.

Those processes can be improved by implementing AIs and that’s our new goal.

Let’s create a new Visa-Agent and a new Visa.check intent:
ionic artificial intelligence API.AI Dialogflow new Visa agent
Here are the basic user inputs:

ionic artificial intelligence API.AI Dialogflow basic inputVisa processes can be quite complex, for now we will only care about:

  1. The country
  2. The city where the user will enter
  3. The city where the user will leave
  4. The user’s name
  5. The user’s surname

Those five requirements have something in common, they all have parameters that can be used by the AI.

Just like programming, we can see those sentences as:

  1. I need a visa for $country
  2. I will enter from $enteringCity
  3. I will leave from $leavingCity
  4. My name is $name
  5. My surname is $surname

Just like TypeScript, those parameters have types or if we follow Dialogflow naming convention: Entities.

Here are more complex user inputs that use those parameters:

ionic artificial intelligence API.AI Dialogflow complex input with entities

We need to add them so our agent can detect and use them:

ionic artificial intelligence API.AI Dialogflow parameters

Those information are crucial to our visa process so we end up with mandatory parameters.

Those Entities are basic ones populated by Dialogflow. If a word (like France) belongs to an Entity, it will be recognized.
However, if that Entity is missing some information, the words won’t be recognized.
A very simple example here is the given-name Entity, if the user’s name isn’t in the database, it won’t be recognized and a new Entity will be required to match specific names like 和子 or Анастасия.

Those parameters will be asked with a priority order (top to bottom).

By clicking there:
ionic artificial intelligence API.AI Dialogflow last name parameterThe AI can use the following sentences to ask for the missing parameter:

ionic artificial intelligence API.AI Dialogflow parameter sentenceSince the givenName parameter has a higher priority, this information is already there for us to use in our question.

The Visa-Agent Artificial Intelligence can now ask and memorize in this order:

  1. The country
  2. The entering city
  3. The leaving city
  4. The user’s name
  5. The user’s surname

We can now connect it to the Ionic application.

Ionic Application Update

Our AI is smarter now and we can have a “real” conversation by updating the Ionic application.

Starting with the client access token:

We will display the answers by using an ngFor in the home.html file:

Adding the answers property to the home.ts file:

And pushing the answers there when Dialogflow’s server responds:

We finally have our Visa-Chatbot working with Ionic!

ionic artificial intelligence API.AI Dialogflow final result

Enter your email address in the box below to get access to this tutorial (and others) source code.


An AI is only limited by the data that we provided and its algorithms. The AI usefulness will highly depend on the designed use cases, if an unexpected case is encountered the user can be redirected to a real employee.

As South Park, Humans and many work of arts are forecasting: more AI = less jobs.

One thing is sure, it’s better been of the side using AIs them than the one getting replaced by them.


  1. Andre Andre
    October 7, 2017    

    Hi my friend, i have this error when i trying to implements your tutorial :
    Uncaught (in promise): ReferenceError: ApiAIPromises is not defined ReferenceError: ApiAIPromises is not defined at http://localhost:8101/build/main.js:60:25 at t.invoke

    • Matthieu Drula Matthieu Drula
      October 7, 2017    

      Did you add:

      declare var ApiAIPromises: any;

      It’s also only working on a real device or a simulator (I’ll add this in the tutorial).

      • Garth Garth
        October 18, 2017    

        Hi, thanks for the tutorial. I have the same problem as Andre above, and yes I do have the declare line above the HomePage constructor.
        Context: Windows 10 64-bit.

        • Matthieu Drula Matthieu Drula
          October 18, 2017    

          Maybe you are not using a device or not waiting for the platform ready event:

          platform.ready().then(() => {

          If you push on github I could have a look.

    • Jack Jack
      January 5, 2018    

      Have you solved this error??
      If yes then give me solution. I am also getting same kinda error.

      Or else give me link to your git link this project.

      Thanks in advance for helping me!!!

  2. Vipul Singh Vipul Singh
    October 30, 2017    

    how to use voice recognition instant of typing please share…!!!!!

  3. franco franco
    November 7, 2017    

    hello is possibile have total code : home.html, home.ts etc… for only import and compile.

  4. Raja Raja
    November 18, 2017    

    I am also getting same error. I tried to deploy it in ionic lab , no use. Can you share full code or give the solution for this.

  5. guest guest
    December 7, 2017    

    What are the reasons for having to install the” Dialogflow Cordova plugin”?
    Why is the plugin necessary?
    Or is it helpful, but not necessary?

  6. Jack Jack
    January 4, 2018    

    Error Here
    “ApiAIPromises is not defined”;

    how to solve. and yes i had declared that in home.ts and also written all code which you had written above.

    Please solve my error.Thanks in advance.

  7. January 27, 2018    

    I got this error

    > cordova run android –device
    cp: copyFileSync: could not write to dest file (code=ENOENT):C:\Users\rmace\git\ionic-apiai\platforms\android\res\xml\config.xml

  8. Ghada Ghada
    February 26, 2018    

    Hey, Thank you for this Tuto,
    Actually, i don’t know how to get an observable whole conversation using ApiAIPromises to get an interface including the history of the convesation.
    Could you please tell me how to do it.

  9. ali ali
    March 5, 2018    

    Code doesnt seem recognize the variable question. Is there a github link to look at the finalzied source code?

    • April 19, 2018    

      i also receive the question variable problem. have you fixed it already?

  10. March 12, 2018    

    After installing the plugin, the build of my app fails. I don’t know what to do. There’s even an issue on Github about this. Do you know what to do?

Leave a Reply

Your email address will not be published. Required fields are marked *