0

I am developing a Google Actions project for Google Home using api.ai.

Is it possible to pass a state parameter as part of the response to api.ai and have it included in the next request?

The Amazon Alexa API handles such a flow and it came in handy.

Thanks

EDIT:

I did not mention this before: I have api.ai sending requests to a webhook after google assistant triggers my intents. The response api.ai expects is defined here. I've tried including extra fields in the response but these are not included in any future intent requests from api.ai. I've also tried adding fields and values to the google specific portion of the response (defined here) to no avail.

As an example of what I am interested in, when responding to Alexa requests we can include a json field "sessionAttributes" which are then passed by Amazon in any future requests that are part of that interaction.

In this instance I query a database key on the first intent (which is subsequently used to pull a record) and pass that key in sessionAttributes to avoid performing that lookup for every intent request I receive.

Kanembel
  • 434
  • 4
  • 11
  • You have this ability in Contexts. Have you saw this: https://docs.api.ai/docs/concept-actions#section-extracting-values-from-contexts ? – Ido Green May 02 '17 at 23:23
  • Thats not quite what I need. I have data on the server that I am trying to cache for the duration of the interaction. I am curious to know if there is a way to do so in the request/response body. – Kanembel May 02 '17 at 23:53
  • @NickDario did answer below help? Please accept if so. – Dana Jun 18 '17 at 20:26
  • I haven't had a chance to test it, I ended up using a redis session to store the info locally. I'm not using node but I did try to add the value to the context in the json response to little effect. I'll take another look though. – Kanembel Jun 20 '17 at 00:05

2 Answers2

2

The equivalent that you are looking for to sessionAttributes in Alexa dev depends on whether you are using an API.ai webhook or a conversation webhook. Reference this doc for more info on the differences.

Since you are using API.ai, assuming you are using the Node.js client library, use the snippet below. 'Contexts' can store data, not just serve as a flag of sorts to establish where you are in a conversation. Call setContext just before you call 'ask' and close out your webhook fulfillment.

app.setContext('<context name>', <lifespan of context>, <JSON to store>)

Then for the next round of fulfillment, retrieve the JSON from the parameters object within the Context. Get it with:

var myContext = app.getContext('<context name>')

Reference the examples in these docs for more info.

Dana
  • 683
  • 1
  • 6
  • 21
0

You could create an optional parameter in API.ai to catch/store this value and append any message being sent to API.ai with a marker and then the database value you want to cache, so API.ai recognizes from the marker the value to be cached as the parameter, and this would be passed back out of API.ai as well, so if you needed to chain/loop this, on your side you again just check if it has the special 'append' parameter value to append that to the next user message.

That being said, contexts can likely achieve the same end-goal you are trying to achieve in a much more straightforward fashion

AiDev
  • 1,214
  • 8
  • 11
  • Is this still valid with regard to my edit? The response api.ai expects does not include any marker that I can pass back. – Kanembel May 03 '17 at 23:26