Skip to content

Instantly share code, notes, and snippets.

@ahmetkucukoglu
Last active November 13, 2019 20:17
Show Gist options
  • Save ahmetkucukoglu/865896c3f22a948e67e93a655645d0cc to your computer and use it in GitHub Desktop.
Save ahmetkucukoglu/865896c3f22a948e67e93a655645d0cc to your computer and use it in GitHub Desktop.
AWS Serverless Kuyruklama Sistemi Geliştirme - ConsumerHandler.cs
[assembly: Amazon.Lambda.Core.LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
namespace AwsDotnetCsharp
{
using Amazon.Lambda.Core;
using Amazon.Lambda.SQSEvents;
using System;
using Amazon.Comprehend;
using Amazon.Comprehend.Model;
using System.Net;
using System.Linq;
using Newtonsoft.Json;
using System.Threading.Tasks;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.DocumentModel;
using AwsDotnetCsharp.Models;
public class ConsumerHandler
{
public async Task ReceiveQueue(SQSEvent input, ILambdaContext context)
{
var messageBody = JsonConvert.DeserializeObject<CommentsQueueRequest>(input.Records[0].Body);
//DETECT LANGUAGE
var comprehendClient = new AmazonComprehendClient(Amazon.RegionEndpoint.EUCentral1);
var detectDominantLanguageRequest = new DetectDominantLanguageRequest
{
Text = messageBody.Comment
};
var detectDominantLanguageResponse = await comprehendClient.DetectDominantLanguageAsync(detectDominantLanguageRequest);
if (detectDominantLanguageResponse.HttpStatusCode != HttpStatusCode.OK)
throw new Exception("NOT FOUND");
var languageResponse = detectDominantLanguageResponse.Languages.OrderByDescending((x) => x.Score).FirstOrDefault();
//UPDATE DOCUMENT
var dynamoDBClient = new AmazonDynamoDBClient();
var commentsCatalog = Table.LoadTable(dynamoDBClient, Environment.GetEnvironmentVariable("DBTableName"));
var commentDocument = new Document();
commentDocument["id"] = messageBody.Id;
commentDocument["language"] = languageResponse.LanguageCode;
var updatedComment = await commentsCatalog.UpdateItemAsync(commentDocument);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment