Ejemplo n.º 1
0
        /// <summary>
        /// A Lambda function to respond to HTTP Get methods from API Gateway
        /// </summary>
        /// <param name="request"></param>
        /// <returns>The list of blogs</returns>
        public async Task <APIGatewayProxyResponse> Validate(APIGatewayProxyRequest request, ILambdaContext context)
        {
            context.Logger.LogLine("Received:" + request.Body);
            if (request.IsSlackChallengeRequest())
            {
                var challengeRequest = request.Body.As <ChallengeRequest>();
                return(Respond(challengeRequest.Challenge, context));
            }

            var slackEvent = request.Body.As <SlackEventAPIPayload>();

            if (slackEvent == null || slackEvent.Event == null)
            {
                return(Respond("IGNORED", context));
            }

            if (string.IsNullOrEmpty(slackEvent.Event.Text))
            {
                return(Respond("IGNORED", context));
            }

            if (slackEvent.Event != null && slackEvent.Event.Text != null && !string.IsNullOrEmpty(slackEvent.Event.Bot_Id))
            {
                return(Respond("BOT", context));
            }

            if (!string.IsNullOrEmpty(slackEvent.Event.SubType) && slackEvent.Event.SubType == "bot_message")
            {
                return(Respond("BOT", context));
            }

            var urls = RegexHelper.FindURl(slackEvent.Event.Text);

            if (urls.Any())
            {
                var firstUrl    = urls.First();
                var cleansedUrl = UrlCleaner.CleanForTrackingQueryParams(firstUrl.Value);
                cleansedUrl = cleansedUrl.TrimEnd('/');
                var searchResults = await _slackClient.SearchMessagesAsync(cleansedUrl, SearchSort.timestamp, count : 1, direction : SearchSortDirection.asc);

                if (searchResults != null && searchResults.messages.matches.Any())
                {
                    context.Logger.LogLine(JsonConvert.SerializeObject(searchResults.messages.matches));
                    var r = searchResults.messages.matches.FirstOrDefault();

                    if (r == null)
                    {
                        return(Respond($"NO-URL-IN-MSG", context));
                    }

                    if (r.ts == slackEvent.Event.Ts)
                    {
                        return(Respond("NEW", context));
                    }

                    if (r.user == slackEvent.Event.User)
                    {
                        return(Respond("OLD-BUT-SAME-USER-SO-IGNORING", context));
                    }

                    var reactionResponse = await _slackClient.AddReactions(slackEvent.GetChannel(), slackEvent.Event.Ts);

                    var alreadyReacted = reactionResponse.Any(AlreadyReactedTo);

                    if (!alreadyReacted)
                    {
                        var message  = $"postet av @{r.username} for {TimeSpanExtensions.Ago(r.ts)} siden.";
                        var response = await _slackClient.SendMessage(slackEvent.GetChannel(), message, slackEvent.Event.Ts, r.permalink);

                        var body = await response.Content.ReadAsStringAsync();

                        context.Logger.LogLine("Sent message. Response:" + JsonConvert.SerializeObject(body));

                        var reactionResponseBody = await reactionResponse.First().Content.ReadAsStringAsync();

                        context.Logger.LogLine("Sent reaction. Response:" + JsonConvert.SerializeObject(reactionResponseBody));


                        return(Respond($"OLD", context));
                    }
                    return(Respond($"ALREADY-OLDED", context));
                }
            }

            return(Respond($"NO-URL-IN-MSG", context));
        }