Esempio n. 1
0
        private async Task <APIGatewayProxyResponse> HandleRequest(SlackRequest request)
        {
            Console.WriteLine("HandleRequest invoked with Type " + request.Type);
            Console.WriteLine("HandleRequest invoked with Event text " + request.Event.Text);
            Console.WriteLine("HandleRequest invoked with Event_id " + request.Event_Id);

            switch (request.Type)
            {
            case "url_verification":
                return(new APIGatewayProxyResponse
                {
                    Headers = GetCorsHeaders(),
                    StatusCode = 200,
                    Body = JsonConvert.SerializeObject(new
                    {
                        Challenge = request.Challenge
                    })
                });

            case "event_callback":
                EventId existingId = _eventIdList.Find(i => i.id == request.Event_Id);
                Console.WriteLine("event_callback Existing in the list " + "  " + existingId?.id + "  ", existingId?.count);
                if (existingId == null)
                {
                    Console.WriteLine("Adding new event to list and processing the message ");
                    _eventIdList.Add(new EventId
                    {
                        id    = request.Event_Id,
                        count = 1
                    });

                    await _slackMessage.ProcessMessage(request.Event);
                }
                else if (existingId.count > 5)
                {
                    _eventIdList.Remove(existingId);
                }
                else
                {
                    existingId.count = existingId.count + 1;
                }

                return(new APIGatewayProxyResponse
                {
                    Headers = GetCorsHeaders(),
                    StatusCode = 200,
                    Body = JsonConvert.SerializeObject(new
                    {
                        Message = request.Event.Text
                    })
                });

            default:
                return(new APIGatewayProxyResponse
                {
                    Headers = GetCorsHeaders(),
                    StatusCode = 200,
                    Body = JsonConvert.SerializeObject(new
                    {
                        Error = "unhandled"
                    })
                });
            }
        }