/// <summary>
        /// Unmarshaller the response from the service to the response class.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context)
        {
            BatchDetectSentimentResponse response = new BatchDetectSentimentResponse();

            context.Read();
            int targetDepth = context.CurrentDepth;

            while (context.ReadAtDepth(targetDepth))
            {
                if (context.TestExpression("ErrorList", targetDepth))
                {
                    var unmarshaller = new ListUnmarshaller <BatchItemError, BatchItemErrorUnmarshaller>(BatchItemErrorUnmarshaller.Instance);
                    response.ErrorList = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("ResultList", targetDepth))
                {
                    var unmarshaller = new ListUnmarshaller <BatchDetectSentimentItemResult, BatchDetectSentimentItemResultUnmarshaller>(BatchDetectSentimentItemResultUnmarshaller.Instance);
                    response.ResultList = unmarshaller.Unmarshall(context);
                    continue;
                }
            }

            return(response);
        }
Пример #2
0
        public async Task FunctionHandler(DynamoDBEvent dynamoEvent, ILambdaContext context)
        {
            context.Logger.LogLine($"Beginning to process {dynamoEvent.Records.Count} records...");

            List <SentimentAnalysis> analysis = dynamoEvent.Records
                                                .Select(record =>
            {
                context.Logger.LogLine($"Event ID: {record.EventID}");
                context.Logger.LogLine($"Event Name: {record.EventName}");

                return(new SentimentAnalysis(record.Dynamodb));
            })
                                                .ToList();

            BatchDetectSentimentResponse response = await _comprehendClient.BatchDetectSentimentAsync(new BatchDetectSentimentRequest
            {
                TextList     = analysis.Select(s => s.Data).ToList(),
                LanguageCode = "en"
            });



            analysis = Enumerable.Zip(analysis, response.ResultList, (a, b) =>
            {
                a.Sentiment = b.Sentiment;
                return(a);
            })
                       .ToList();

            foreach (var a in analysis)
            {
                ManualOverride(a);
            }

            var badDays = analysis.Where(x => x.Sentiment == SentimentType.NEGATIVE);

            badDays.ToList();

            context.Logger.LogLine(String.Join(' ', analysis.Select(a => a.ToString())));

            var publishTasks = badDays.Select(a =>
                                              _snsClient.PublishAsync(new PublishRequest("arn:aws:sns:eu-west-1:875034483274:intervention-required", a.Data)
            {
                MessageAttributes = new Dictionary <string, MessageAttributeValue> {
                    { "UserId", new MessageAttributeValue {
                          DataType = "String", StringValue = a.UserId
                      } },
                    { "Type", new MessageAttributeValue {
                          DataType = "String", StringValue = a.Type
                      } },
                    { "Level", new MessageAttributeValue {
                          DataType = "String", StringValue = a.SentimentLevel
                      } }
                }
            }));

            var responses = await Task.WhenAll(publishTasks);

            context.Logger.Log(String.Join(' ', responses.Select(r => JsonConvert.SerializeObject(r))));


            context.Logger.LogLine("Stream processing complete.");
        }