我正在尝试让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";
}
}
}使用AWS toolkil for Visual Studio 2019,如果我使用示例Alexa调用测试函数,则会清楚地返回文本字符串。

但是,一旦我将函数发布到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)"
]
}
}这很令人困惑,因为我并没有试图返回JSON有效负载。
发布于 2019-09-24 19:12:36
经过进一步的研究,我发现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;
}
}https://stackoverflow.com/questions/58067337
复制相似问题