private async Task <String> FormatEmailAsync(CaseDetails caseDetails) { String emailBody = ""; IAmazonS3 client = new AmazonS3Client(bucketRegion); try { GetObjectRequest objectRequest = new GetObjectRequest { BucketName = "norbert.templates", Key = "email-staff-response.txt" }; using (GetObjectResponse objectResponse = await client.GetObjectAsync(objectRequest)) using (Stream responseStream = objectResponse.ResponseStream) using (StreamReader reader = new StreamReader(responseStream)) { emailBody = reader.ReadToEnd(); } emailBody = emailBody.Replace("AAA", caseReference); emailBody = emailBody.Replace("CCC", HttpUtility.HtmlEncode(caseDetails.contactResponse)); emailBody = emailBody.Replace("DDD", HttpUtility.HtmlEncode(caseDetails.customerName)); emailBody = emailBody.Replace("FFF", HttpUtility.HtmlEncode(await GetContactFromDynamoAsync(caseReference))); emailBody = emailBody.Replace("NNN", HttpUtility.HtmlEncode(caseDetails.staffName)); } catch (Exception error) { await SendFailureAsync(" Reading Response Template", error.Message); Console.WriteLine("ERROR : FormatEmailAsync : Reading Response Template : " + error.Message); Console.WriteLine("ERROR : FormatEmailAsync : " + error.StackTrace); } return(emailBody); }
public async Task FunctionHandler(object input, ILambdaContext context) { if (await GetSecrets()) { Boolean suppressResponse = false; JObject o = JObject.Parse(input.ToString()); caseReference = (string)o.SelectToken("CaseReference"); taskToken = (string)o.SelectToken("TaskToken"); String fromStatus = (String)o.SelectToken("FromStatus"); String transition = (String)o.SelectToken("Transition"); if (fromStatus.Equals(transition)) { suppressResponse = true; Console.WriteLine("supressing response : " + caseReference); } Console.WriteLine("caseReference : " + caseReference); try { if (context.InvokedFunctionArn.ToLower().Contains("prod")) { dynamoTable = "MailBotCasesLive"; liveInstance = true; } } catch (Exception) { } if (liveInstance) { cxmEndPoint = secrets.cxmEndPointLive; cxmAPIKey = secrets.cxmAPIKeyLive; CaseDetails caseDetailsLive = await GetCaseDetailsAsync(); await ProcessCaseAsync(caseDetailsLive, suppressResponse); await SendSuccessAsync(); } else { cxmEndPoint = secrets.cxmEndPointTest; cxmAPIKey = secrets.cxmAPIKeyTest; CaseDetails caseDetailsTest = await GetCaseDetailsAsync(); await ProcessCaseAsync(caseDetailsTest, suppressResponse); await SendSuccessAsync(); } } Console.WriteLine("Completed"); }
private async Task <Boolean> SendMessageAsync(String emailBody, CaseDetails caseDetails, String emailFrom, Boolean suppressResponse) { if (!suppressResponse) { try { AmazonSQSClient amazonSQSClient = new AmazonSQSClient(sqsRegion); try { SendMessageRequest sendMessageRequest = new SendMessageRequest(); sendMessageRequest.QueueUrl = secrets.sqsEmailURL; sendMessageRequest.MessageBody = emailBody; Dictionary <string, MessageAttributeValue> MessageAttributes = new Dictionary <string, MessageAttributeValue>(); MessageAttributeValue messageTypeAttribute1 = new MessageAttributeValue(); messageTypeAttribute1.DataType = "String"; messageTypeAttribute1.StringValue = caseDetails.customerName; MessageAttributes.Add("Name", messageTypeAttribute1); MessageAttributeValue messageTypeAttribute2 = new MessageAttributeValue(); messageTypeAttribute2.DataType = "String"; messageTypeAttribute2.StringValue = caseDetails.customerEmail; MessageAttributes.Add("To", messageTypeAttribute2); MessageAttributeValue messageTypeAttribute3 = new MessageAttributeValue(); messageTypeAttribute3.DataType = "String"; messageTypeAttribute3.StringValue = "Northampton Borough Council: Your Call Number is " + caseReference;; MessageAttributes.Add("Subject", messageTypeAttribute3); MessageAttributeValue messageTypeAttribute4 = new MessageAttributeValue(); messageTypeAttribute4.DataType = "String"; messageTypeAttribute4.StringValue = emailFrom; MessageAttributes.Add("From", messageTypeAttribute4); sendMessageRequest.MessageAttributes = MessageAttributes; SendMessageResponse sendMessageResponse = await amazonSQSClient.SendMessageAsync(sendMessageRequest); } catch (Exception error) { await SendFailureAsync("Error sending SQS message", error.Message); Console.WriteLine("ERROR : SendMessageAsync : Error sending SQS message : '{0}'", error.Message); Console.WriteLine("ERROR : SendMessageAsync : " + error.StackTrace); return(false); } } catch (Exception error) { await SendFailureAsync("Error starting AmazonSQSClient", error.Message); Console.WriteLine("ERROR : SendMessageAsync : Error starting AmazonSQSClient : '{0}'", error.Message); Console.WriteLine("ERROR : SendMessageAsync : " + error.StackTrace); return(false); } } return(true); }
private async Task <CaseDetails> GetCaseDetailsAsync() { CaseDetails caseDetails = new CaseDetails(); HttpClient cxmClient = new HttpClient(); cxmClient.BaseAddress = new Uri(cxmEndPoint); string requestParameters = "key=" + cxmAPIKey; HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "/api/service-api/norbert/case/" + caseReference + "?" + requestParameters); try { HttpResponseMessage response = cxmClient.SendAsync(request).Result; if (response.IsSuccessStatusCode) { HttpContent responseContent = response.Content; String responseString = responseContent.ReadAsStringAsync().Result; JObject caseSearch = JObject.Parse(responseString); caseDetails.customerName = (String)caseSearch.SelectToken("values.first-name") + " " + (String)caseSearch.SelectToken("values.surname"); caseDetails.contactResponse = (String)caseSearch.SelectToken("values.contact_response"); caseDetails.customerEmail = (String)caseSearch.SelectToken("values.email"); caseDetails.staffName = (String)caseSearch.SelectToken("values.agents_name"); caseDetails.transitionTo = (String)caseSearch.SelectToken("values.new_case_status"); caseDetails.serviceArea = (String)caseSearch.SelectToken("values.service_area_4"); caseDetails.sentiment = (String)caseSearch.SelectToken("values.sentiment"); caseDetails.recommendationAccuracy = (String)caseSearch.SelectToken("values.recommendation_accuracy"); } else { await SendFailureAsync("Getting case details for " + caseReference, response.StatusCode.ToString()); Console.WriteLine("ERROR : GetStaffResponseAsync : " + request.ToString()); Console.WriteLine("ERROR : GetStaffResponseAsync : " + response.StatusCode.ToString()); } } catch (Exception error) { await SendFailureAsync("Getting case details for " + caseReference, error.Message); Console.WriteLine("ERROR : GetStaffResponseAsync : " + error.StackTrace); } return(caseDetails); }
private async Task <Boolean> ProcessCaseAsync(CaseDetails caseDetails, Boolean supporessResponse) { Boolean success = true; try { if (!String.IsNullOrEmpty(caseDetails.contactResponse)) { if (await StoreToDynamoAsync(caseReference, "ActualService", caseDetails.serviceArea) && await StoreToDynamoAsync(caseReference, "ActualSentiment", caseDetails.sentiment) && await StoreToDynamoAsync(caseReference, "ActualResponse", caseDetails.contactResponse) && await StoreToDynamoAsync(caseReference, "RecommendationAccuracy", caseDetails.recommendationAccuracy)) { String emailBody = await FormatEmailAsync(caseDetails); if (!String.IsNullOrEmpty(emailBody)) { //TODO remove norbert hardcoding if (await SendMessageAsync(emailBody, caseDetails, "*****@*****.**", supporessResponse)) //if (await SendMessageAsync(emailBody, caseDetails, "*****@*****.**")) { switch (caseDetails.transitionTo.ToLower()) { case "close": await TransitionCaseAsync("close-case"); break; case "awaiting_customer_response": await TransitionCaseAsync("awaiting-customer"); break; default: await SendFailureAsync("Enexpected New Case Status for " + caseReference, caseDetails.transitionTo.ToLower()); Console.WriteLine("Enexpected New Case Status for " + caseReference + " : " + caseDetails.transitionTo.ToLower()); success = false; break; } } else { success = false; } } else { await SendFailureAsync("Empty Message Body : " + caseReference, "ProcessCaseAsync"); Console.WriteLine("ERROR : ProcessCaseAsyn : Empty Message Body : " + caseReference); success = false; } } else { success = false; } } else { await SendFailureAsync("Empty Response : " + caseReference, "ProcessCaseAsync"); Console.WriteLine("ERROR : ProcessCaseAsyn : Empty Response : " + caseReference); success = false; } } catch (Exception) { return(false); } return(success); }