测试 alexa 技能 returns "Error converting the Lambda event JSON payload to a string"

Testing alexa skill returns "Error converting the Lambda event JSON payload to a string"

我正在尝试让 Alexa 设备读出我 return 来自用 C# 编写的 Lambda 函数的文本字符串。

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

[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 toolkil,如果我使用示例 Alexa 调用测试函数,文本字符串显然是 returned。

然而,一旦我将函数发布到 AWS Lambda,我收到以下错误提示 Error converting the Lambda event JSON payload to a string

{
  "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)"
    ]
  }
}

这令人困惑,因为我没有尝试 return JSON 有效载荷。

经过进一步调查,我发现 Alexa 不会简单地说出 returned 字符串,你必须构建一个响应对象,然后 return 那。

使用 Alexa.NET 我写了下面的 class 这允许我指示 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;
    }
}