DEV Community

Anand Mukundan
Anand Mukundan

Posted on • Originally published at dotnetindia.com on

Building for Alexa - 4 - Progressive Response

This post looks at a specific functionality that you can add to your skills called Progressive Response.

These allow you to show the user that the skill is working on something that may take some time. For example, if you are looking up an order delivery status, but you need to check with multiple logistic providers to get the final status. This whole operation may take maybe 30 seconds for you and then you need to package this information into a useful voice response. So it may make sense when you are half way thru your third-party checks to let the user that you are working thru the request and it may take you some more time to get the information.

See the source image

Think of this are the voice equivalent of the hourglass in a windows application or the animated circle you find in web applications (Like the ones you see here).

The way you send a progressive dialog is to call the API endpoint /directives with a VoicePlayer.Speak command.

Currently, you do not have an out of the box way to do this via the AlexaSkillKit.NET library, but as this is a normal REST endpoint, you can directly call it like any other REST service.

Below is a function I wrote that you can use to send progressive dialogs as part of your Alexa Skill (This uses the RestSharp Library for the API calling). The apiToken,requestId and apiURL are part of the request you get from Amazon. You need to use the token you receive or the REST call will fail.

 internal static void SendProgressiveResponse(string apiToken, string requestId, 
string apiBaseUrl, string speechText)
        {
            if (speechText.Length > 600)
            {
                throw new ArgumentException("Progressive Text cannot be more than 600 characters", "speechText");
            }
            var restClient = new RestClient(apiBaseUrl);
            RestRequest restRequest = new RestRequest();
            restRequest.Resource = "v1/directives";
            restRequest.Method = RestSharp.Method.POST;
            restRequest.AddHeader("Authorization", "Bearer " + apiToken);
            restRequest.AddHeader("Content-Type", "application/json");
            var body = new
            {
                header = new
                {
                    requestId = requestId
                },
                directive = new
                {
                    type = "VoicePlayer.Speak",
                    speech = speechText
                }
            };
            restRequest.AddJsonBody(body);
            var response = restClient.Execute(restRequest);
            if (response.StatusCode == System.Net.HttpStatusCode.NoContent)
            {
                Log.Debug("Progressive was successful");
            }
            else
            {
                Log.Debug($"Progressive failed withe code - {response.StatusCode}, {response.StatusDescription}.");
            }
        }

You can get more information on Progressive Responses here..

Top comments (0)