Example #1
0
 public void Process(Amazon.Lambda.SNSEvents.SNSEvent snsEvent, ILambdaContext lambdaContext)
 {
     foreach (var snsEventRecord in snsEvent.Records)
     {
         lambdaContext.Logger.LogLine(snsEventRecord.Sns.Message);
     }
 }
        public async Task FunctionHandler(Amazon.Lambda.SNSEvents.SNSEvent input, ILambdaContext context)
        {
            foreach (var record in input.Records)
            {
                var manualIntervention = Newtonsoft.Json.JsonConvert.DeserializeObject <ManualInterventionMessage>(record.Sns.Message);

                var message = new
                {
                    Blocks = new object[]
                    {
                        new { Type = "divider" },
                        new { Type = "section", Text = new { Type = "mrkdwn", Text = $"*{record.Sns.Subject}*\n\n<{manualIntervention.Approval.ApprovalReviewLink}|Click to Approve/Reject>" } },
                        new { Type = "section", Fields = new object[]
                              {
                                  new { Type = "mrkdwn", Text = $"*Region*\n{manualIntervention.Region}" },
                                  new { Type = "mrkdwn", Text = $"*Pipeline Name*\n{manualIntervention.Approval.PipelineName}" },
                                  new { Type = "mrkdwn", Text = $"*Stage*\n{manualIntervention.Approval.StageName}" },
                                  new { Type = "mrkdwn", Text = $"*Action*\n{manualIntervention.Approval.ActionName}" },
                                  new { Type = "mrkdwn", Text = $"*Custom Data*\n{manualIntervention.Approval.CustomData}" },
                                  new { Type = "mrkdwn", Text = $"*Time*\n{manualIntervention.Approval.Expires}" },
                              } },
                    }
                };

                var json = Newtonsoft.Json.JsonConvert.SerializeObject(message, new JsonSerializerSettings
                {
                    ContractResolver = new DefaultContractResolver {
                        NamingStrategy = new CamelCaseNamingStrategy()
                    }
                });

                logger.LogInformation(webhookUrl);
                var requestMessage = new HttpRequestMessage(HttpMethod.Post, webhookUrl);
                requestMessage.Content = new StringContent(json, Encoding.UTF8, "application/json");
                try
                {
                    // TODO: add some retry logic
                    using (var response = await httpClient.SendAsync(requestMessage))
                    {
                        if (!response.IsSuccessStatusCode)
                        {
                            logger.LogError((int)response.StatusCode, "Failed to send message to Slack. StatusCode was {StatusCode}", response.StatusCode);
                        }
                    }
                }
                catch (Exception e)
                {
                    logger.LogError(500, e, "Failed to send message to Slack");
                }
            }
        }