c# - 测试 Alexa 技能返回 "Error converting the Lambda event JSON payload to a string"

标签 c# .net-core aws-lambda alexa-skills-kit

我正在尝试让 Alexa 设备说出我从用 C# 编写的 Lambda 函数返回的一串文本。

现在我已经编写了一个返回字符串的基本方法。

[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]

namespace AlexaTeachMeNewWord
{
    public class Function
    {
        public string FunctionHandler(object input, ILambdaContext context)
        {
            return "Hello this is a test";
        }
    }
}

使用适用于 Visual Studio 2019 的 AWS 工具,如果我使用示例 Alexa 调用测试该函数,则会清楚地返回文本字符串。

Alexa skill test

但是,当我将函数发布到 AWS Lambda 后,我收到以下错误消息,告诉我将 Lambda 事件 JSON 负载转换为字符串时出错

{
  "errorType": "JsonSerializerException",
  "errorMessage": "Error converting the Lambda event JSON payload to a string. JSON strings must be quoted, for example \"Hello World\" in order to be converted to a string: Unexpected character encountered while parsing value: {. Path '', line 1, position 1.",
  "stackTrace": [
    "at Amazon.Lambda.Serialization.Json.JsonSerializer.Deserialize[T](Stream requestStream)",
    "at lambda_method(Closure , Stream , Stream , LambdaContextInternal )"
  ],
  "cause": {
    "errorType": "JsonReaderException",
    "errorMessage": "Unexpected character encountered while parsing value: {. Path '', line 1, position 1.",
    "stackTrace": [
      "at Newtonsoft.Json.JsonTextReader.ReadStringValue(ReadType readType)",
      "at Newtonsoft.Json.JsonTextReader.ReadAsString()",
      "at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, Boolean hasConverter)",
      "at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)",
      "at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)",
      "at Newtonsoft.Json.JsonSerializer.Deserialize[T](JsonReader reader)",
      "at Amazon.Lambda.Serialization.Json.JsonSerializer.Deserialize[T](Stream requestStream)"
    ]
  }
}

这很令人困惑,因为我没有尝试返回 JSON 有效负载。

最佳答案

经过进一步调查,我发现 Alexa 不会简单地说出返回的字符串,您必须构建一个响应对象并将其返回。

使用 Alexa.NET,我编写了以下类,它允许我指示 Alexa 设备说出我的文本字符串。

希望这对某人有帮助。

public class Function
{
    public SkillResponse FunctionHandler(SkillRequest req, ILambdaContext context)
    {
        // create the speech response
        var speech = new SsmlOutputSpeech();
        speech.Ssml = "<speak>This is an test.</speak>";

        // create the response
        var responseBody = new ResponseBody();
        responseBody.OutputSpeech = speech;
        responseBody.ShouldEndSession = true; // this triggers the reprompt
        responseBody.Card = new SimpleCard { Title = "Test", Content = "Testing Alexa" };

        var skillResponse = new SkillResponse();
        skillResponse.Response = responseBody;
        skillResponse.Version = "1.0";

        return skillResponse;
    }
}

关于c# - 测试 Alexa 技能返回 "Error converting the Lambda event JSON payload to a string",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58067337/

相关文章:

c# - 我应该将私有(private)方法公开以供测试吗?

c# - Azure Function - 此平台不支持 System.Data.SqlClient

amazon-web-services - 使用无服务器框架请求验证

java - AWS lambda + spring boot = 不连接组件

node.js - 使用nodejs lambda的S3文件上传问题

c# - 如何使用Reflection调用 "private async Task"类型的方法

c# - mongodb 中的身份验证

c# - 如何在 MVC 中为国家和州/省创建选择列表

C# WPF Xaml : Globally set all text in a view to one color, 和所有背景到另一个

c# - 如何在 gRPC 服务器上关闭 HTTPS