public string Post([FromBody] SlackEventDto data) { var result = data?.Challenge; var sender = _slackService.Users?[data.Event?.User]; if (sender?.is_bot ?? true) { return(result); } _slackService.ProceedReceviedMessage(sender.id, data.Event.Text); return(result); }
public async Task ProcessEvent(SlackEventDto slackEventDto) { if (slackEventDto.Type != "event_callback") { return; } string eventData = slackEventDto.EventRaw.Value.ToString(); EventType eventType = ParseEventType(eventData); if (eventType.SubType == "channel_join") { return; } switch (eventType.Type) { // team_join and user_change use same event type, TeamJoinEventDto. case "team_join": SlackEventFullDto <TeamJoinEventDto> teamJoinEvent = MapSlackEventObject <TeamJoinEventDto>(slackEventDto, eventData); await ProcessTeamJoinEvent(teamJoinEvent); break; case "user_change": SlackEventFullDto <TeamJoinEventDto> userChangeEvent = MapSlackEventObject <TeamJoinEventDto>(slackEventDto, eventData); await ProcessUserChangeEvent(userChangeEvent); break; case "message": SlackEventFullDto <MessageChannelsEventDto> messageEvent = MapSlackEventObject <MessageChannelsEventDto>(slackEventDto, eventData); await ProcessMessageEvent(messageEvent); break; // manage user technologie through reaction events case "reaction_added": SlackEventFullDto <ReactionEventDto> reactionAddedEvent = MapSlackEventObject <ReactionEventDto>(slackEventDto, eventData); await ProcessReactionAddedEvent(reactionAddedEvent); break; case "reaction_removed": SlackEventFullDto <ReactionEventDto> reactionRemovedEvent = MapSlackEventObject <ReactionEventDto>(slackEventDto, eventData); await ProcessReactionRemovedEvent(reactionRemovedEvent); break; default: break; } }
private SlackEventFullDto <T> MapSlackEventObject <T>(SlackEventDto slackEventDto, string innerEventData) { SlackEventFullDto <T> slackEventFullDto = new SlackEventFullDto <T>(); slackEventFullDto.ApiAppId = slackEventDto.ApiAppId; slackEventFullDto.AuthedUsers = slackEventDto.AuthedUsers; slackEventFullDto.EventId = slackEventDto.EventId; slackEventFullDto.EventTime = slackEventDto.EventTime; slackEventFullDto.TeamId = slackEventDto.TeamId; slackEventFullDto.Token = slackEventDto.Token; slackEventFullDto.Type = slackEventFullDto.Type; slackEventFullDto.Event = JsonConvert.DeserializeObject <T>(innerEventData); return(slackEventFullDto); }
public async Task <ActionResult> RecieveEvent([FromBody] SlackEventDto slackEventDto) { if (await _slackRequestValidator.IsValid(Request)) { var eventType = slackEventDto.Type; if (eventType == "url_verification") { var urlVerificationResponseDto = new UrlVerificationResponseDto { challenge = slackEventDto.Challenge }; return(Ok(urlVerificationResponseDto)); } await _messageQueue.SendMessageAsync(slackEventDto, "event"); return(Ok()); } return(BadRequest()); }
/// <summary> /// Message to process incoming messages from service bus queue. /// </summary> /// <param name="message"></param> /// <param name="token"></param> /// <returns></returns> private async Task ProcessMessagesAsync(Microsoft.Azure.ServiceBus.Message message, CancellationToken token) { try { string messageBody = Encoding.UTF8.GetString(message.Body); switch (message.Label) { case "command": SlackCommandDto slackCommandDto = JsonConvert.DeserializeObject <SlackCommandDto>(messageBody); await _commandHandler.ProcessCommand(slackCommandDto); await _queueClient.CompleteAsync(message.SystemProperties.LockToken); break; case "event": SlackEventDto slackEventDto = JsonConvert.DeserializeObject <SlackEventDto>(messageBody); await _eventHandler.ProcessEvent(slackEventDto); await _queueClient.CompleteAsync(message.SystemProperties.LockToken); break; case "projectpost": await _apiEventHandler.ProcessProjectPost(messageBody); await _queueClient.CompleteAsync(message.SystemProperties.LockToken); break; case "registration": RegistrationDto registration = JsonConvert.DeserializeObject <RegistrationDto>(messageBody); await _apiEventHandler.ProcessRegistration(registration); await _queueClient.CompleteAsync(message.SystemProperties.LockToken); break; case "feedback": FeedbackDto feedback = JsonConvert.DeserializeObject <FeedbackDto>(messageBody); await _apiEventHandler.ProcessFeedback(feedback); await _queueClient.CompleteAsync(message.SystemProperties.LockToken); break; default: _logger.LogWarning($"No label associated with message:{message.SystemProperties.SequenceNumber}"); break; } ; _logger.LogInformation($"Processed message: SequenceNumber:{message.SystemProperties.SequenceNumber}"); } catch (Exception ex) { // failed to process message, mark as Abandoned _logger.LogError(ex, $"Error processing message: SequenceNumber:{message.SystemProperties.SequenceNumber}"); await _queueClient.DeadLetterAsync(message.SystemProperties.LockToken); } }