question

christopher john lee avatar image
christopher john lee asked

How to handle incoming message to Alexa skill from external app?

0

I have an Alexa skill and a winform on a windows 10 device. I'm sending a message from the winform to Alexa using the Skill Messaging API. I've got the access token, sent the message and received a 202 status code to say the 'message has been successfully accepted, and will be sent to the skill' so I believe everything on the winform side is okay.

The code for it;

var handler = new HttpClientHandler();
handler.ServerCertificateCustomValidationCallback = (requestMessage, certificate, chain, policyErrors) => true;

using (var httpClient = new HttpClient(handler))
{
    // Obtain skill messaging token
    using (var requestToken = new HttpRequestMessage(new HttpMethod("POST"), "https://api.amazon.com/auth/O2/token"))
    {
        requestToken.Content = new StringContent("grant_type=client_credentials&scope=alexa:skill_messaging&client_id=amzn1.application-oa2-client.************&client_secret=************");
        requestToken.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");

        var responseToken = httpClient.SendAsync(requestToken);

        Response r = JsonConvert.DeserializeObject<Response>(responseToken.Result.Content.ReadAsStringAsync().Result);

        // Send message
        using (var requestMessage = new HttpRequestMessage(new HttpMethod("POST"), "https://api.eu.amazonalexa.com/v1/skillmessages/users/" + strUserId))
        {
            requestMessage.Headers.TryAddWithoutValidation("Authorization", "Bearer " + r.access_token);

            requestMessage.Content = new StringContent("{ \"data\" : { \"message\" : \"Hi pickle\" }}");
            requestMessage.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");

            var responseMessage = httpClient.SendAsync(requestMessage);

            MessageBox.Show(responseMessage.Result.ToString());
        }
    }
}

How do I capture the incoming message event on the skill though? Looking at the docs I need to handle an incoming request of type Messaging.MessageReceived? Is that correct?

I tried something like that in the skills FunctionHandler but didn't have any luck.

public SkillResponse FunctionHandler(SkillRequest input, ILambdaContext context)
{
    // Initialise response
    skillResponse = new SkillResponse
    {
        Version = "1.0",
        Response = new ResponseBody()
    };

    ssmlResponse = new SsmlOutputSpeech();

        if (input.GetRequestType() == typeof(LaunchRequest))
        {
            LaunchRequestHandler(input, context);
        }
        else if (input.GetRequestType() == typeof(IntentRequest))
        {
            IntentRequestHandler(input, context);
        }
        else if (input.GetRequestType() == typeof(SessionEndedRequest))
        {
            SessionEndedRequestHandler(input, context);
        }
        else if(input.GetRequestType().Equals("Messaging.MessageReceived"))
        {
            ssmlResponse.Ssml = "<speak>" + input.Request.Type + "</speak>";
        }


    skillResponse.Response.OutputSpeech = ssmlResponse;
    return skillResponse;
}

How do I react to the message? Is it permissions I need to set up? Does the incoming message not trigger the functionhandler the same way the echo device does?

Thanks
alexa skills kitalexa skillsc#
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Andy Whitworth avatar image
Andy Whitworth answered

Yes, the Skill Messaging API works as you describe and if everything is working correctly then you should receive a Messaging.MessageReceived event in your skill code. I would add some debug to log every request received in your skill code and check that you're not receiving the event.

If you're not receiving the event then double check the client id/secret and userId you're using. Also check that the amazon endpoint you're using corresponds to the region where the user's account is registered. There are different endpoints you need to call depending on the user's geography https://developer.amazon.com/en-US/docs/alexa/smapi/skill-messaging-api-reference.html#north-american-na-and-european-eu-post-commands


10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

christopher john lee avatar image
christopher john lee answered

Hello Andy, thanks for the reply. Client id, secret, user id and endpoint are all good. Event is being received but not recognised.

"Error converting the Lambda event JSON payload to type Alexa.NET.Request.SkillRequest: Unknown request type: Messaging.MessageReceived. (Parameter 'Type'): JsonSerializerException "

3 comments
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Andy Whitworth avatar image Andy Whitworth commented ·
OK, that's good that you're receiving the request. I guess it's an issue with your C# SDK not handling the MessageReceived events. I know the C# SDK isn't an Amazon provided thing so perhaps get in touch with the author.
0 Likes 0 ·
Andy Whitworth avatar image Andy Whitworth Kyron Longwood commented ·

Have you read this -> https://www.nuget.org/packages/Alexa.NET.SkillMessaging

Specifically the "Add support for Skill Messaging requests" part ?

0 Likes 0 ·