TidyDev
TidyDev

Reputation: 3668

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

I'm attempting to make an Alexa device speak a string of text that I return from my Lambda function written in C#.

Right now I've written a basic method which returns a string.

[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";
        }
    }
}

Using the AWS toolkil for Visual Studio 2019, if I test the function with the example Alexa invocation, the string of text is clearly returned.

Alexa skill test

However once I've published the function to AWS Lambda, I get the below error telling me 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)"
    ]
  }
}

This is confusing since i'm not trying to return a JSON payload.

Upvotes: 2

Views: 1449

Answers (1)

TidyDev
TidyDev

Reputation: 3668

After further investigation, I found that Alexa won't simply speak the returned string, you have to build a response object and return that.

Using Alexa.NET I write the below class which allows me to instruct the Alexa device to speak my text string.

Hope this helps somebody.

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;
    }
}

Upvotes: 3

Related Questions