尝试与Lex互动时出现424错误

时间:2019-01-22 19:36:22

标签: c# amazon-web-services aws-lambda

我在似乎没有那么多文档的AWS问题上遇到了一些困难。

我在这里有我的lambda函数:

public async Task<string> FunctionHandler(ConnectRequest request, ILambdaContext context)
        {
            AmazonLexClient lexClient = new AmazonLexClient();

            var response = new PostTextResponse();

            PostTextRequest postRequest = new PostTextRequest();

            postRequest.BotName = "X";
            postRequest.BotAlias = "X";
            postRequest.UserId = Guid.NewGuid().ToString();
            postRequest.InputText = "What Time Is My Appointment?";



            try
            {
                response = await lexClient.PostTextAsync(postRequest);
                context.Logger.Log(response.IntentName);
                context.Logger.Log(response.DialogState);
            }
            catch (Exception ex)
            {

                context.Logger.Log($"EXCEPTION CAUGHT: {Environment.NewLine} {ex.ToJson()} {Environment.NewLine} {response.Message} {response.IntentName} {response.SlotToElicit}");
                return "Error";
            }

            context.Logger.Log($"Success from lambda {Environment.NewLine}  Message: {response.Message} {Environment.NewLine} " +
                               $"Dialog State:{response.DialogState}");


            return "Success";
        }

我正在从连接流中调用它,如下所示: enter image description here

而我得到的回报是:

"ErrorType": 2,
    "ErrorCode": "DependencyFailedException",
    "RequestId": "",
    "StatusCode": 424,
    "Message": "Invalid Lambda Response: Received error response from Lambda: Unhandled",
    "Data": {},
    "InnerException": {
        "Response": {
            "StatusCode": 424,
            "IsSuccessStatusCode": false,
            "ContentType": "application/json",
            "ContentLength": 85,
            "ResponseBody": {}
        },
        "Message": "Exception of type 'Amazon.Runtime.Internal.HttpErrorResponseException' was thrown."

我在Amazon PostText Documentation中读过的内容可能有两件事:

  • 如果Amazon Lex没有足够的权限来调用Lambda
    功能。

  • 如果Lambda函数执行时间超过30秒。

  • 如果实现Lambda函数返回了Delegate对话框动作
    而不删除任何广告位值。

我已确认我的Lambda确实有权使用PostText和访问Lex。我试图将我的函数的返回类型更改为PostTextReponse,但是没有运气,所以我不确定从这里去哪里,关于这种事情的文档并不多。

感谢您的帮助,谢谢!

1 个答案:

答案 0 :(得分:1)

对于任何对此感到好奇的人,我都找到了答案:

首先,使用这样的Lambda函数时,最好返回想要的对象。我最终要做的是。您还需要将SessionAttributes设置为 JSON 格式。

我的代码正在运行,现在如下:

public async Task<LambdaResponseItem> FunctionHandler(ConnectRequest request, ILambdaContext context)
        {
            var client = new AmazonLexClient();
            var response = new PostContentResponse();
            var lambdaInfo = new Dictionary<string, string>();

            var contentRequest = new PostContentRequest();
            var postContentStream = new MemoryStream();
            var postContentWriter = new StreamWriter(postContentStream);

            try
            {
                var userInput = request.Details?.Parameters?.GetValueOrDefault("UserInput");

                postContentWriter.Write(userInput); // Grab user input (utterance) value from AWS Connect.
                postContentWriter.Flush();
                postContentStream.Position = 0;

                contentRequest.Accept = "text/plain; charset=utf-8";
                contentRequest.BotName = "IntroGreeting";
                contentRequest.BotAlias = EnvironmentVariables.IsProduction ? "Production" : "Development"; 
                contentRequest.ContentType = "text/plain; charset=utf-8";
                contentRequest.UserId = request.Details?.ContactData?.ContactId;
                contentRequest.InputStream = postContentStream;
                contentRequest.SessionAttributes = request.Details?.Parameters?.ToJson(); // * Must be in Json format or request will return error *



                // POST to Lex
                response = await client.PostContentAsync(contentRequest);
                return new LambdaResponseItem(){
                    Content = ""
                     }

            }
            catch (Exception ex)
            {
                context.Logger.Log($"POST Request to Amazon Lex Failed {ex.ToJson()}");
                }