public async Task <BotResponseCode> TryAddTrackToPlaylist(UpdateDto updateDto) { if (string.IsNullOrEmpty(updateDto.ParsedTrackId)) { return(BotResponseCode.NoAction); } var existingTrackInPlaylist = await _trackRepository.Get(updateDto.ParsedTrackId, updateDto.Chat.PlaylistId); // Check if the track already exists in the playlist. if (existingTrackInPlaylist != null) { string text; if (existingTrackInPlaylist.State == TrackState.RemovedByDownvotes) { text = $"This track was previously posted, but it was downvoted and removed from the {_spotifyLinkHelper.GetMarkdownLinkToPlaylist(updateDto.Chat.PlaylistId, "playlist")}."; } else { text = $"This track is already added to the {_spotifyLinkHelper.GetMarkdownLinkToPlaylist(updateDto.Chat.PlaylistId, "playlist")}!"; } await _sendMessageService.SendTextMessage(updateDto.Chat.Id, text); return(BotResponseCode.TrackAlreadyExists); } var spotifyClient = await _spotifyClientFactory.Create(updateDto.Chat.AdminUserId); // We can't continue if we can't use the spotify api. if (spotifyClient == null) { await _sendMessageService.SendTextMessage(updateDto.Chat.Id, "Spoti-bot is not authorized to add this track to the Spotify playlist."); return(BotResponseCode.NoAction); } // Get the track from the spotify api. var newTrack = await _spotifyClientService.GetTrack(spotifyClient, updateDto.ParsedTrackId); if (newTrack == null) { await _sendMessageService.SendTextMessage(updateDto.Chat.Id, $"Track not found in Spotify api :("); return(BotResponseCode.NoAction); } await AddTrack(spotifyClient, updateDto.ParsedUser, newTrack, updateDto.Chat.PlaylistId); // Reply that the message has been added successfully. await SendReplyMessage(updateDto, newTrack); // Add the track to my queue. await _spotifyClientService.AddToQueue(spotifyClient, newTrack); return(BotResponseCode.TrackAddedToPlaylist); }
public async Task <BotResponseCode> TryHandleCallbackQuery(UpdateDto updateDto) { // If the bot can't do anything with the update's callback query, we're done. if (!CanHandleCallbackQuery(updateDto)) { return(BotResponseCode.NoAction); } // Check if a vote should be handled and if so handle it. var voteResponseCode = await _voteService.TryHandleVote(updateDto); if (voteResponseCode != BotResponseCode.NoAction) { // Save users that voted. await _userService.SaveUser(updateDto.ParsedUser, updateDto.Chat.Id); return(voteResponseCode); } if (IsAddToQueueCallback(updateDto)) { if (updateDto.User == null || updateDto.Track == null) { return(BotResponseCode.CommandRequirementNotFulfilled); } var spotifyClient = await _spotifyClientFactory.Create(updateDto.User.Id); if (spotifyClient == null) { var text = $"You didn't connect your Spotify account yet, {updateDto.User.FirstName}. Please connect first."; // TODO: move to commandsservice, get username from telegram me apicall. var url = $"http://t.me/SpotiHenkBot?start={LoginRequestReason.AddToQueue}_{updateDto.Chat.Id}_{updateDto.Track.Id}"; await _sendMessageService.AnswerCallbackQuery(updateDto.ParsedUpdateId, text, url); return(BotResponseCode.AddToQueueHandled); } if (await _spotifyClientService.AddToQueue(spotifyClient, updateDto.Track)) { await _sendMessageService.AnswerCallbackQuery(updateDto.ParsedUpdateId, "Track added to your queue."); } else { await _sendMessageService.AnswerCallbackQuery(updateDto.ParsedUpdateId, "Could not add track to your queue, play something first!"); } return(BotResponseCode.AddToQueueHandled); } // This should never happen. throw new CallbackQueryNotHandledException(); }
// TODO: refactor. private async Task RespondInChat(LoginRequest loginRequest) { const string successMessage = "Spoti-bot is now authorized, awesome!"; switch (loginRequest.Reason) { case LoginRequestReason.AddToQueue: var trackId = loginRequest.TrackId; var chatId = loginRequest.GroupChatId; if (string.IsNullOrEmpty(trackId) || !chatId.HasValue) { await _sendMessageService.SendTextMessage(loginRequest.PrivateChatId, successMessage); return; } var chat = await _chatRepository.Get(chatId.Value); if (chat == null) { await _sendMessageService.SendTextMessage(loginRequest.PrivateChatId, successMessage); return; } var track = await _trackRepository.Get(trackId, chat.PlaylistId); if (track == null) { await _sendMessageService.SendTextMessage(loginRequest.PrivateChatId, successMessage); return; } var spotifyClient = await _spotifyClientFactory.Create(loginRequest.UserId); if (!await _spotifyClientService.AddToQueue(spotifyClient, track)) { await _sendMessageService.SendTextMessage(loginRequest.PrivateChatId, successMessage); return; } // Answer in the private chat. await _sendMessageService.SendTextMessage(loginRequest.PrivateChatId, $"{successMessage}\n\n_{track.Name}_ is now added to your queue."); return; case LoginRequestReason.AddBotToGroupChat: // Answer in the private chat. var privateChatText = $"{successMessage}\n\nPlease return to the group chat for the last step."; await _sendMessageService.SendTextMessage(loginRequest.PrivateChatId, privateChatText); // Answer in the group chat. var groupChatText = $"{successMessage}\n\nThe last step is to set the desired playlist with the {Command.SetPlaylist.ToDescriptionString()} command."; await _sendMessageService.SendTextMessage(loginRequest.GroupChatId.Value, groupChatText); return; case LoginRequestReason.LoginLinkCommand: // Answer in the private chat. await _sendMessageService.SendTextMessage(loginRequest.PrivateChatId, successMessage); return; } }