private async Task <string> GetAndUpdateSubscriptionInfo(string teamId, AppMentionEvent appMentioned) { try { var team = await _teamRepo.GetTeam(teamId); var currentSubscriptions = team.Subscriptions; (var inputSubscriptions, var unableToParse) = ParseSubscriptionsFromInput(appMentioned); if (inputSubscriptions.Count() < 1 && unableToParse.Count() < 1) { return($"You need to pass some arguments\n {FormatAllSubsAvailable()}"); } if (inputSubscriptions.Count() < 1) { return($"I was not able to understand: *{string.Join(", ", unableToParse)}.* :confused: \n {FormatAllSubsAvailable()}"); } var newSubscriptions = appMentioned.Text.Contains("unsubscribe") ? UnsubscribeToEvents(inputSubscriptions, currentSubscriptions) : SubscribeToEvents(inputSubscriptions, currentSubscriptions); await _teamRepo.UpdateSubscriptions(teamId, newSubscriptions); return(FormatSubscriptionMessage(newSubscriptions, unableToParse)); } catch (Exception e) { _logger.LogError(e.Message, e); return($"Oops, could not update subscriptions."); } }
public async Task <IActionResult> OnPost(string teamId, int leagueId, string channel, EventSubscription[] subscriptions) { var league = await _leagueClient.GetClassicLeague(leagueId, tolerate404 : true); if (league == null) { TempData["msg"] = "⚠️ League does not exist.\n"; } var slackClient = await CreateSlackClient(teamId); var channelsRes = await slackClient.ConversationsListPublicChannels(500); var channelsFound = channelsRes.Channels.Any(c => channel == $"#{c.Name}" || channel == c.Id); if (!channelsFound) { var channelsText = string.Join(',', channelsRes.Channels.Select(c => c.Name)); TempData["msg"] += $"WARN. Could not find updated channel in via Slack API lookup. Channels: {channelsText}"; } await _teamRepo.UpdateLeagueId(teamId, leagueId); await _teamRepo.UpdateChannel(teamId, channel); await _teamRepo.UpdateSubscriptions(teamId, subscriptions); TempData["msg"] += "Updated!"; return(RedirectToPage("Edit")); }
public override async Task <EventHandledResponse> Handle(EventMetaData eventMetadata, AppMentionEvent message) { var newLeagueId = ParseArguments(message); if (string.IsNullOrEmpty(newLeagueId)) { var help = $"No leagueId provided. Usage: `@fplbot follow 123`"; await _publisher.PublishToWorkspace(eventMetadata.Team_Id, message.Channel, help); return(new EventHandledResponse(help)); } var couldParse = int.TryParse(newLeagueId, out var theLeagueId); if (!couldParse) { var res = $"Could not update league to id '{newLeagueId}'. Make sure it's a valid number."; await _publisher.PublishToWorkspace(eventMetadata.Team_Id, message.Channel, res); return(new EventHandledResponse(res)); } var failure = $"Could not find league {newLeagueId} :/ Could you find it at https://fantasy.premierleague.com/leagues/{newLeagueId}/standings/c ?"; try { var league = await _leagueClient.GetClassicLeague(theLeagueId); if (league?.Properties != null) { var team = await _slackTeamRepository.GetTeam(eventMetadata.Team_Id); await _slackTeamRepository.UpdateLeagueId(eventMetadata.Team_Id, theLeagueId); await _slackTeamRepository.UpdateChannel(eventMetadata.Team_Id, message.Channel); if (!team.Subscriptions.Any()) { await _slackTeamRepository.UpdateSubscriptions(eventMetadata.Team_Id, new[] { EventSubscription.All }); } var success = $"Thanks! You're now following the '{league.Properties.Name}' league (leagueId: {theLeagueId}) in {ChannelName()}"; await _publisher.PublishToWorkspace(eventMetadata.Team_Id, message.Channel, success); return(new EventHandledResponse(success)); string ChannelName() { return($"<#{message.Channel}>"); } } await _publisher.PublishToWorkspace(eventMetadata.Team_Id, message.Channel, failure); return(new EventHandledResponse(failure)); } catch (HttpRequestException e) { _logger.LogError(e.Message, e); await _publisher.PublishToWorkspace(eventMetadata.Team_Id, message.Channel, failure); return(new EventHandledResponse(failure)); } }