/// <summary> /// Unmarshaller the response from the service to the response class. /// </summary> /// <param name="context"></param> /// <returns></returns> public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context) { GetActivityTaskResponse response = new GetActivityTaskResponse(); context.Read(); int targetDepth = context.CurrentDepth; while (context.ReadAtDepth(targetDepth)) { if (context.TestExpression("input", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.Input = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("taskToken", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.TaskToken = unmarshaller.Unmarshall(context); continue; } } return(response); }
//const aws = require('aws-sdk'); /// <summary> /// A simple function that takes a string and does a ToUpper /// </summary> /// <param name="input"></param> /// <param name="context"></param> /// <returns></returns> public string FunctionHandler(ILambdaContext context) { //try //{ // var bucketName = System.Environment.GetEnvironmentVariable("mysqlconnection"); // var quotenumber = GetQuote(58); // string input = quotenumber.QuoteNumber; // return input?.ToUpper(); //} //catch (Exception e) //{ } LambdaLogger.Log("1. Lambda start"); //Starts workflow instance with parameter var stste = StartWorkflowInstanceAsync(); LambdaLogger.Log("2. Workflow started." + stste ?? "".ToString()); string emailstatus = "Problem occured in email approval."; string token = string.Empty; try { //AmazonStepFunctionsClient stepfunctions = new AmazonStepFunctionsClient(Amazon.RegionEndpoint.USEast1); AmazonSimpleEmailServiceClient ses = new AmazonSimpleEmailServiceClient(Amazon.RegionEndpoint.USEast1); Task <GetActivityTaskResponse> responsetask = null; GetActivityTaskResponse response = null; //return "Success"; LambdaLogger.Log("Task ARN"); var aws_access_key_id = Environment.GetEnvironmentVariable("aws_access_key_id"); var aws_secret_access_key = Environment.GetEnvironmentVariable("aws_secret_access_key"); using (AmazonStepFunctionsClient stepfunctions = new AmazonStepFunctionsClient(aws_access_key_id, aws_secret_access_key, Amazon.RegionEndpoint.USEast1)) { GetActivityTaskRequest request = new GetActivityTaskRequest { ActivityArn = "arn:aws:states:us-east-1:494875521123:activity:PMApprovalStep", WorkerName = "PMApprovingTaskActivity" }; LambdaLogger.Log("Response Output req sent "); responsetask = stepfunctions.GetActivityTaskAsync(request); responsetask.GetAwaiter(); response = responsetask.Result; //if(responsetask.) if (response != null) { LambdaLogger.Log("Response Output = " + response.Input); } else { LambdaLogger.Log("Response Output = null"); } token = response.TaskToken; LambdaLogger.Log("Token = " + token); } if (!string.IsNullOrEmpty(response.Input)) { List <string> tolst = GetUsersAsPerRole("PM"); var input = JObject.Parse(response.Input); //var email = "*****@*****.**"; //get to mail from salesOrg as per approval level var quoteid = (int)input.SelectToken("quoteid"); //var tolst = new List<string>(); //tolst.Add(email); Body body = new Body { Html = new Content { Charset = "UTF-8", Data = "<div>Hi!<br /><br /> " + "Promotion '" + (string)input.SelectToken("promodesc") + "' needs your approval!<br />" + "Can you please approve:<br /> <h3><a href='https://wyunsq1ccf.execute-api.us-east-1.amazonaws.com/respond/approve?taskToken=" + Uri.EscapeDataString(token) + ""eid=" + quoteid + "'>Click To Approve</a></h3><br />" + //"https://wyunsq1ccf.execute-api.us-east-1.amazonaws.com/respond/approve?taskToken=" + Uri.EscapeDataString(token) + ""eid=" + quoteid + "<br />" + "Or reject:<br />" + "<h3><a href='https://wyunsq1ccf.execute-api.us-east-1.amazonaws.com/respond/reject?taskToken=" + Uri.EscapeDataString(token) + ""eid=" + quoteid + "'>Click To Reject</a></h3></div><br /><br />Thank You,<br />Application Team" //"https://wyunsq1ccf.execute-api.us-east-1.amazonaws.com/respond/reject?taskToken=" + Uri.EscapeDataString(token) + ""eid=" + quoteid + "</div>" } }; Content subjectContent = new Content { Charset = "UTF-8", Data = "Your Approval Needed as Pricing Manager for Promotion Id " + quoteid }; LambdaLogger.Log("Starting Mail sending.. "); Message msg = new Message { Body = body, Subject = subjectContent }; SendEmailRequest s = new SendEmailRequest { Source = "*****@*****.**", Destination = new Destination { ToAddresses = tolst }, Message = msg, ConfigurationSetName = "sesconfigset", SourceArn = "arn:aws:ses:us-east-1:494875521123:identity/[email protected]" }; var status = ses.SendEmailAsync(s); status.GetAwaiter(); emailstatus = status.Result.HttpStatusCode.ToString(); LambdaLogger.Log("Going to update Db"); if (!string.IsNullOrEmpty(token)) { UpdateApprovalStatusWithToken(quoteid, Uri.EscapeDataString(token)); //Moving quote status to 3(PM) and updating token } //string input, //var bucketName = System.Environment.GetEnvironmentVariable("mysqlconnection"); //var quotenumber = GetQuote(58); //input = quotenumber.QuoteNumber; //return input?.ToUpper(); } } catch (Exception e) { LambdaLogger.Log("Response Output = " + e); } return(emailstatus); }
private async Task <string> InsufficientCreditHandler(string insufficientCreditActivityARN, ILambdaContext context) { string result = ""; GetActivityTaskResponse response = await stepFunctionsClient.GetActivityTaskAsync( new GetActivityTaskRequest() { ActivityArn = insufficientCreditActivityARN }); if (HttpStatusCode.OK.Equals(response.HttpStatusCode) && !string.IsNullOrEmpty(response.TaskToken)) { // Task is found context.Logger.LogLine($"InsufficientCreditHandler: Found a task. Input is: {response.Input}"); NumberPlateTrigger input = JsonConvert.DeserializeObject <NumberPlateTrigger>(response.Input); if (!input.numberPlate.detected) { context.Logger.LogLine("TESTING::input.numberPlate.detected is false which means this must be a test"); context.Logger.LogLine("Forcing number plate to test value"); input.numberPlate.detected = true; input.numberPlate.numberPlateString = "TESTPLATE"; } //Sign Image URL string imageLink = s3client.GetPreSignedURL(new GetPreSignedUrlRequest() { BucketName = input.bucket, Key = input.key, Expires = DateTime.Now.AddDays(1) }); // // Query DynamoDB to get the owner email // Table table = Table.LoadTable(dbClient, Environment.GetEnvironmentVariable("DDBTableName")); Document document = await table.GetItemAsync(input.numberPlate.numberPlateString); if (document == null) { context.Logger.LogLine($"Could not find plate {input.numberPlate.numberPlateString} in our records"); } var sendRequest = new SendEmailRequest { Source = Environment.GetEnvironmentVariable("TargetEmailAddress"), ReplyToAddresses = new List <string> { Environment.GetEnvironmentVariable("TargetEmailAddress") }, Destination = new Destination { ToAddresses = new List <string> { document["ownerEmail"] } }, Message = new Message { Subject = new Content("[ACTION] - Your account credit is exhausted"), Body = new Body { Html = new Content { Charset = "UTF-8", Data = $"Hello {document["ownerFirstName"]} {document["ownerLastName"]},<br/><br/>Your vehicle with number plate <b>{document["numberPlate"]}</b> was recently detected on a toll road, but your account has insufficient credit to pay the toll.<br/><br/>" + $"<img src='{imageLink}'/><br/><a href='{imageLink}'>Click here to see the original image</a><br/><br/>" + "Please update your account balance immediately to avoid a fine. <br/>" + $"<a href='{Environment.GetEnvironmentVariable("APIGWEndpoint")}topup/{document["numberPlate"]}?taskToken={HttpUtility.UrlEncode(response.TaskToken)}><b>Click this link to top up your account now.</b></a><br/>" + "<br/><br/> Thanks<br/><b>Toll Road Administrator.</b><br/><br/>" }, Text = new Content { Charset = "UTF-8", Data = $"Hello {document["ownerFirstName"]} {document["ownerLastName"]}, Your vehicle with number plate: {document["numberPlate"]} was recently detected on a toll road, but your account has insufficient credit to pay the toll." + "Please update your account balance immediately to avoid a fine." + $"Please access this link to top up: {Environment.GetEnvironmentVariable("APIGWEndpoint")}topup/{document["numberPlate"]}?taskToken={HttpUtility.UrlEncode(response.TaskToken)}" + ".. Thanks. Toll Road Administrator." } } }, // If you are not using a configuration set, comment // or remove the following line //ConfigurationSetName = configSet }; context.Logger.LogLine($"Sending email to ({Environment.GetEnvironmentVariable("TargetEmailAddress")})"); SendEmailResponse sendEmailResponse = await emailServiceClient.SendEmailAsync(sendRequest); if (sendEmailResponse.HttpStatusCode.Equals(HttpStatusCode.OK)) { context.Logger.LogLine("The email was successfully sent."); result = "success"; } else { context.Logger.LogLine("Internal Error: The email could not be sent."); result = "error"; } } return(result); }
private async Task <string> UnknownNumberPlateHandler(string unknowNumberActivityARN, ILambdaContext context) { string result = ""; GetActivityTaskResponse response = await stepFunctionsClient.GetActivityTaskAsync( new GetActivityTaskRequest() { ActivityArn = unknowNumberActivityARN }); if (HttpStatusCode.OK.Equals(response.HttpStatusCode) && !string.IsNullOrEmpty(response.Input)) { context.Logger.LogLine($"ManualAdminTaskHandler: Found a task. Input is: {response.Input}"); NumberPlateTrigger input = JsonConvert.DeserializeObject <NumberPlateTrigger>(response.Input); //Sign Image URL string imageLink = s3client.GetPreSignedURL(new GetPreSignedUrlRequest() { BucketName = input.bucket, Key = input.key, Expires = DateTime.Now.AddDays(1) }); var sendRequest = new SendEmailRequest { Source = Environment.GetEnvironmentVariable("TargetEmailAddress"), ReplyToAddresses = new List <string> { Environment.GetEnvironmentVariable("TargetEmailAddress") }, Destination = new Destination { ToAddresses = new List <string> { Environment.GetEnvironmentVariable("TargetEmailAddress") } }, Message = new Message { Subject = new Content("[ACTION] - Manual Decision Required!"), Body = new Body { Html = new Content { Charset = "UTF-8", Data = $"Hello {Environment.GetEnvironmentVariable("TargetEmailAddress")},< br />< br /> An image was captured at a toll booth, " + "but the Number Plate Processor could not be confident that it could determine the actual number plate on the vehicle. We need your help to take a look at the image," + "and make a determination.< br />< br />" + $"<img src='{imageLink}'/><br/><a href=' {imageLink}'>Click here to see the original image if it is not appearing in the email correclty.</a><br/><br/>" + $"<a href='{Environment.GetEnvironmentVariable("APIGWEndpoint")}parse/{input.bucket}/{input.key}/5?imageLink={HttpUtility.UrlEncode(imageLink)}&taskToken={HttpUtility.UrlEncode(response.TaskToken)}'><b>Click this link to help assess the image and provide the number plate.</b></a><br/>" + "<br/><br/>Thanks<br/><b>Toll Road Administrator.</b><br/><br/>" }, Text = new Content { Charset = "UTF-8", Data = $"Hello {Environment.GetEnvironmentVariable("TargetEmailAddress")}, An image was captured at a toll booth, " + "but the Number Plate Processor could not be confident that it could determine the actual number plate on the vehicle. We need your help to take a look at the image," + "and make a determination." + $"Please access this link to take a decision: {Environment.GetEnvironmentVariable("APIGWEndpoint")}parse/{input.bucket}/{input.key}/5?imageLink={HttpUtility.UrlEncode(imageLink)}&taskToken={HttpUtility.UrlEncode(response.TaskToken)}" + " .. Thanks. Toll Road Administrator" } } }, // If you are not using a configuration set, comment // or remove the following line //ConfigurationSetName = configSet }; context.Logger.LogLine($"Sending email to ({Environment.GetEnvironmentVariable("TargetEmailAddress")})"); SendEmailResponse sendEmailResponse = await emailServiceClient.SendEmailAsync(sendRequest); if (sendEmailResponse.HttpStatusCode.Equals(HttpStatusCode.OK)) { context.Logger.LogLine("The email was successfully sent."); result = "success"; } else { context.Logger.LogLine("Internal Error: The email could not be sent."); result = "error"; } } return(result); }