首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >测试alexa skill返回“将Lambda事件JSON有效负载转换为字符串时出错”

测试alexa skill返回“将Lambda事件JSON有效负载转换为字符串时出错”
EN

Stack Overflow用户
提问于 2019-09-24 01:21:52
回答 1查看 1.2K关注 0票数 2

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

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

代码语言:javascript
复制
[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

代码语言:javascript
复制
{
  "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有效负载。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-24 19:12:36

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

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

希望这对某些人有帮助。

代码语言:javascript
复制
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;
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58067337

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档