r/GoogleAssistantDev • u/markmazap • Sep 09 '19
Playing audio whilst making API calls
I know you can play back audio clips using SSML markup within a response, but is there any way to play a sound whilst a user is waiting for an API call to complete? I am looking to convey to the user an impression of the app 'working' whilst they wait for an API response.
many thanks,
mark
3
Upvotes
2
u/afirstenberg GDE Sep 09 '19
Yes... with caveats.
The Assistant works on a request/response model - every request must generate a response within the timeout period (about 5 seconds). If your API call will take longer than 5 seconds, or if you don't event want to wait that long, you'll need to do something about this.
One solution is to do something like this:
Start the API processing.
When you get a result back, store this result in a database of some sort against the session ID or a user ID you've assigned to the user. You cannot send this directly back to the user or store anything in user storage at this point.
Immediately send back a Media Response to the user that plays the audio.
When the audio finishes, it will send an event back to your webhook as a request.
At this point, you can check to see if you have a result from your API.
If you do, reply with the result.
If you're still waiting for the API to reply, play the hold music again and repeat.