简体   繁体   English

如何处理从外部应用程序传入 Alexa 技能的消息?

[英]How to handle incoming message to Alexa skill from external app?

I have an Alexa skill and a winform on a windows 10 device.我在 windows 10 设备上有一个 Alexa 技能和一个 winform。 I'm sending a message from the winform to Alexa using the Skill Messaging API .我正在使用技能消息 API从 winform 向 Alexa 发送消息。 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.我得到了访问令牌,发送了消息并收到了 202 状态码,表示“消息已成功接受,并将发送到技能”,所以我相信 winform 端一切正常。

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?查看我需要处理 Messaging.MessageReceived 类型的传入请求的文档 Is that correct?那是对的吗?

I tried something like that in the skills FunctionHandler but didn't have any luck.我在技能 FunctionHandler 中尝试了类似的方法,但没有任何运气。

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?传入消息是否不会像 echo 设备那样触发函数处理程序?

Thanks.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM