我正在尝试让 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 调用测试该函数,则会清楚地返回文本字符串。
但是,当我将函数发布到 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/