Exemplo n.º 1
0
        public override async Task Process(AppMention callbackEvent)
        {
            if (callbackEvent.Text.Contains("hello"))
            {
                await _slackClient.SendMessage(callbackEvent.Channel, "hello back");

                return;
            }

            // var filter = 0;
            // var buffer = new StringBuilder();
            //
            // foreach (var item in callbackEvent.Text)
            // {
            //     switch (item)
            //     {
            //         case '<':
            //             filter++;
            //             break;
            //         case '>':
            //             filter--;
            //             break;
            //         default:
            //             if (filter == 0)
            //             {
            //                 buffer.Append(item);
            //             }
            //
            //             break;
            //     }
            // }


            var actualMessage = RemoveFormatting(callbackEvent.Text);

            // TODO: this handles only message which start with the bot name (ex: '<bot> do something')
            actualMessage = string.Join(
                ' ',
                actualMessage.Split(' ', StringSplitOptions.RemoveEmptyEntries).Skip(1)
                );
            await _slackCommandDispatcher.DecodeAndDispatch(callbackEvent.Channel, actualMessage);
        }
Exemplo n.º 2
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));
        }