user3423762
user3423762

Reputation: 81

How do I get conversationId from ApiAiApp or ActionsSdkApp?

I try to find in document of ApiAiApp and ActionsSdkApp, they do not have property or function to get conversationId. I can see this from the JSON request. Do I have to parse the request myself to get the conversationId? conversationId should be commonly used.

Upvotes: 2

Views: 312

Answers (1)

Prisoner
Prisoner

Reputation: 50701

Although it isn't documented, there is a getConversationId() that is part of the ActionsSdkApp object, but not part of the ApiAiApp object. If you're using Node.js (which you would be if you're using the library), it should be easy to handle the JSON object as well, however. For API.AI you should find it in originalRequest.data.conversation.conversationId.

Keep in mind, however, that there is no "end" event for the conversation. So while you will know when there is a new conversation, and when it is updated, you won't know when the user stops that particular conversation.

Depending on your needs, you may want to use the userid instead of the conversationid. It doesn't provide information about when the conversation ends, but it can track the same user between different sessions and devices.

For most users (tho probably not in your case), the conversationid probably isn't as used as you might think. While it might make sense to use this as a key to save state in a database or something along those lines, it is easier to let Google save the state itself.

You can save the state in a Context in API.AI using the app.setContext( name, lifespan, parametersObject ) method. On the next time through, you can get the data from the app.getContext() method.

In the ActionsSdkApp object, you can do something equivalent using a dialogState object that is the last parameter for most of the ask-like methods (such as the app.ask() method). This will then be available on your next call through the app.getDialogState() method. (This page also talks about an app.data field that can be used for the same thing, but I can't find any more documentation about it.)

Upvotes: 3

Related Questions