Home / API.AI / Get your DialogFlow (API.AI) agent to initiate the conversation before user types a message
API.AI

Get your DialogFlow (API.AI) agent to initiate the conversation before user types a message

There have been quite a few changes – in terms of Dialogflow features, in terms of its user interface, as well as in terms of my understanding of the subject. You might find the updated version of this article more current and useful.

Before reading the rest of the article please note that Dialogflow has released a new integration called “Dialogflow Messenger” which allows you to integrate your Dialogflow bot into your website very easily. I suggest going through this article first.

If you would like to continue reading the current article, click on the plus sign below.

OK, understood. I want to see the full article

I thought that I had already answered this question on my website, but on checking again, I see that I haven’t.

So let us understand the question:

When you work with API.AI you notice that it follows the “user types something -> agent replies with an answer” sequence. Suppose you wish to get the agent to say something before the user types anything, how do you do it?

Example

In my CourseBot, I initiate the conversation by using the same ideas described on this page – except that I use Dialogflow API v2 while this article talks about v1.

What is an Event in API.AI?

In API.AI, the Event has a very specific meaning: you can “call” an event to trigger a specific, corresponding intent.

From the API.AI docs:

Events is a feature that allows you to invoke intents by an event name instead of a user query.

First, you define event names in intents. Then, you can trigger these intents by sending a /query request containing an "event" parameter.

For example, in all the agents you create, you will see the DEFAULT Welcome Intent.

On clicking into the intent, you see the following:

The word WELCOME, under the Event section, can be used as a “trigger” to get the Default Welcome Intent to fire without the user typing any message. You can change the word to any other string you like, but you must call the same string to invoke the event later.

REST API

So you might be thinking: that’s great, but if the user doesn’t type anything, then how can we call the event?

This is where you need to use the REST API interface exposed by API.AI.

The Client Access Token

For each agent you create, under the Settings, you will see something called the Client Access Token. Copy it into a file/clipboard for later access.

Send a HTTP Request to call the Event

Now you are ready to issue a command to your API.AI agent to invoke the default welcome intent.

You need to send a request to API.AI. If you were to use cURL (a command line utility for sending HTTP requests) it will look like this (note: it is a single line. I added line breaks for clarity)

curl -H "Authorization: Bearer YOUR_CLIENT_ACCESS_TOKEN" 
"https://api.api.ai/v1/query?v=20150910&e=event_name&timezone=Europe/Paris
&lang=en&sessionId=1234567890"

CLIENT_ACCESS_TOKEN

This is the value of the client access token you copied

e=event_name

Use WELCOME as the event_name

sessionId

Use an appropriate identifier as a session ID. The session here means a single conversation with a user and is used to manage the state of the conversation (don’t worry about it if you don’t understand this at this point)

v=20150910

The version ‘v’ is the date of the API version you wish to use. Say there are three versions of the API released on the following three dates:

1 Jan 2017

25 Feb 2017

15 Mar 2017

v = 20170101 will correspond to the API.AI version which was released on 1 Jan 2017

v = 20170225 will correspond to the API.AI version which was released on 25 Feb 2017

v = 20170318 will correspond to the API.AI version which was released on 15 Mar 2017

and so on. Notice that it doesn’t have to match the exact date. It will just use the version which was most recent when compared to the date you send through.

How to send the request from code

You will incorporate something similar to the request you saw above into your code. I will write more about this in a future article, but all programming languages will usually have helper libraries which will help you construct these HTTP requests.

Update 29 Aug 2017: Check out the PHP code sample here.

Related Posts

  • Hi,
    I have a question pls, How can I send a value from another app to my dialogflow agent. it’s urgent, Please.

    • Dialogflow does not support out of band push notifications like what you are trying to do. The agent has to initiate the communication if it wants to receive a response.

  • Hey,
    I have integerated dailogflow with servicenow. and I have made a voice assistant android app which is connected to dailogflow. Can you tell me how can i do user authentication. (I don’t want to do it with gmail. I want to do it with servicenow username & pass). Thanks in Advance

  • Hi Arvind,
    How can i send value in fulfilment and use globally for all function in fulfilment .is there any way to send value in fulfilment when chatbot button is click.Please suggest if any node js example exist please let me know.

    • That would be an entire course. If this is very urgent, you can get in touch via my contact form for paid 1-on-1 Skype consultation.

  • Hi Aravind,

    Hope you are doing good.
    I’m working on creating a chatbot using Dialogflow and FB messenger integration, wherein I have a requirement to get a picture as response from user and I need to save this picture.
    Can you suggest how this can be achieved?

  • Hi Arvind,
    I want to use integrated web demo iframe. Is there any way, in which i can pass the user name and password silently and authenticate it with the help of webhook and then fetch the profile and fetch the name and it should appeare when looged-in user open the chatpot – “Hi Ankur”

  • Hi, Aravind! How are you doing?
    I did the steps you taught on here, but I’d like to know how the people can chat with it. Thank you!

  • Aravind, this works perfectly for me, but I don’t understand how to use the API response.
    Would the response text be displayed somewhere automatically ? How and where are we supposed to print the text in our application (say, Web demo of DialogFlow)

    • You need to build out your own chat UI for a website chatbot. Dialogflow’s built in web demo integration doesn’t support this. As for other channels like FB and Slack, you will need to do build out a custom integration and also figure out how to use their APIs to push messages into the chat window. You can expect it to be quite a bit of work.

  • Hi, i try this in postman and i get the json response. But is there a way to see how i get the message from the bot in real time once i call this intent through the event?

    • It is quite reasonable to assume the response via Postman will be exactly the same that comes back if you were to use it inside your actual app. I would recommend building out the API call in the programming language of your choice, and look at the response object coming back. It will have the intent response (and all the associated info) and you can proceed to your next step.

  • Hi, Aravind! If I got it right, we have no options how to write to a FB or Telegram user without user’s request. However, it depends on using one-click integration. If I use custom integration with Telegram, FB Messenger – I can use events(as push notifications) only by:
    1. Generating and using SessionId.
    2. Having SenderId.
    How do you think – is it enough?

    • In theory, that is correct. I know for sure that it works for website chatbots, where I have direct experience. I am not sure if there are many people who have actually accomplished push notifications from Dialogflow to Telegram, FB Messenger etc. Be sure to read this article. Also ask on StackOverflow to verify if your approach will work (although I think you are on the right track). Finally, if you do get it working, please leave a comment below. I think many people might come to you for help at that point. 🙂

    • Hi Kirill…I am a student in Spain trying to develop a bot using Dialogflow, Firebase and Telegram. I have been trying for a while to get my Telegram bot to initiate a conversation periodically if the user doesn’t do so for a few days, do you know if this is possible? if so, could you please help me? I am desperate

    • You need to generate your own sessionID, and then (depending on your goal) make sure you use the same sessionID for a given session when interacting with the REST API.

  • Hello, I saw in your previous answer to Jon you highlighted there is no Push Notification concept in Dialogflow. In this case, can you kindly suggest me how I can exploit this feature with a Google Home device?
    Thanks in advance for your help.
    Raul

      • Hello Aravind, thanks a lot for your prompt response.
        I think the new “notifications” feature won’t be usefull for my specifc purpose.

        Anyhow my previous question was intended about: how I can exploit the feature related to run an intent by invoking relevant “event” through call REST api (as per your example here above … “https://api.api.ai/v1/query?v=20150910&e=event_name&timezone=Europe/Paris
        &lang=en&sessionId=1234567890”) in order to experience the final effect on a “screen-less” device as Google Home speaker ?
        Finally,since my agent has been migrated to V2 API, I kindly request you how to submit the equivalent HTTP Rest invokation above with API V2 version (or if it is really needed…)

        Thanks again for your patience and I’m sorry for my bad english and for the previous misunderstanding.
        Raul

  • Can I use this method with an already existing chat? Do I need to get the sessionId from that chat? If yes where do I found it?

  • Hi, I want to notify user that ‘user typing…’ before response displays in Dialogflow chatbot. Is it possible to implement using dialogflow event? Or any other way we can do it?

  • Great article. I’ve been looking for this capability for years! Is there a way you could use IFTTT to trigger the Event? For example if I unlocked my door using a smart lock that could send an IFTTT message could this trigger the event?

    • One important thing to remember when you do this is that the “response” from Dialogflow (that is, whatever is in the Response field inside the specific intent) will automatically be sent back to the requesting app. That is, there isn’t any concept of “push notifications” in Dialogflow. If you aren’t thinking of push notifications, then it will work OK.

  • how did you change the UI of the dialog flow chat bot
    when am doing it,its showing powered by Dialogflow and am unable to change the UI of the frame

    • In theory, you should be able to set up a scheduled task which runs a script at a particular time. The task will call Dialogflow’s REST API and invoke the corresponding Event using the /query endpoint. Would that work for your use case?