Digging a little bit - Webhooks

So far, your chatbot is a sort of a huge database filled with hard coded questions-answers fields.

Now, our goal will be to understand how we can make it more dynamic (this sentence makes me immediately think of yet another old tutorial on transitioning from HTML/CSS to PHP for dynamic webpages).

Apart from having a chatbot with smart talks powered by your server, webhooks are overall powerful because it allows one to integrate absolutely anything with a chat interface.

That’s why leveraging on DialogFlow or any other big chatbot platform is very handy as it handles all the NLP part, integration with apps like Messenger, Whatsapp, etc and other big concerns you and I are undoubtedly not aware of.

Let me explain. If you want to build an e-commerce upon a chatbot for example, you can.

Scenario :

“Hi, I’m looking for the latest Nike Air Max.”

(Performing a database request via a webhook) -> Showing the latest Air Max model. “Would you like to buy it ?”

“Of course!”

“Type your credit card information bla bla bla”

Done.

This example is just here to demonstrate my point. Such an application would raise big security concerns, huge UX issues, and it would kill jobs as there wouldn’t be no more ads.

Setup

First, navigate to “Fullfillment” from the left side menu.

HTTPS IS NOT AN OPTION

DialogFlow won’t work if your SSL certificate is not perfectly configured on your server, believe me. (I lost days on it, see later)

Remember it

You can configure basic auth and headers in requests sent, but it’s not mandatory.

Second, define an intent (a scenario).

Click on the button “Create intent”

Next, you will be redirected to this configuration screen.

Fill what you want and click on the button Save

A brief description for each part :

Contexts : It allows you to define variables for each user with a lifespan. Documentation

It allows you to define variables for each user with a lifespan. Events : Those are events provided by Google. For example, when the user starts a conversation for the first time with the “Start” button. Documentation

Those are events provided by Google. For example, when the user starts a conversation for the first time with the “Start” button. Training phrases : Quoting the tooltip : “Phrases you can expect from users, that will trigger the intent”

Quoting the tooltip : “Phrases you can expect from users, that will trigger the intent” Actions and parameters : Documentation

Responses : Just like small talks, it allows you to configure default responses to use when the intent is triggered.

Just like small talks, it allows you to configure default responses to use when the intent is triggered. Fulfillment : The part you need to enable if you want your webhook to be called. Documentation. Note that if your webhook is broken for some reasons, the default responses will be used, how smart did you expect DialogFlow to be ? 😃

DialogFlow is very well documented, really. The documentation is interactive, there are built-in chats in-between code samples to make you understand things. Spend your time on it, it’s worth.

Here’s my configuration

Amazing, now save yours

Testing it just to be sure.

Yay !!!

Coding part - Extending your chatbot with super powers

This is what you came for

I won’t go neither about setting up SSL certificate nor setting up an express app in NodeJs. (The whole next article will be exactly on it, that’s why)

Let’s assume your server is running, https works perfect, and your webhook route is : /webhook .

DialogFlow performs POST requests with JSON data when triggering a webhook.

This is how you handle it.

It’s that easy

Note that you can also use the official NodeJS package released by Google : https://github.com/dialogflow/dialogflow-nodejs-client-v2

And if you test it, once again.

If the response doesn’t come from the webhook request, look at this documentation and click on Show JSON :