Esempio n. 1
0
        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);
        }
        /// <summary>
        /// Check if a message contains a command and if so, handle it.
        /// </summary>
        /// <returns>A BotResponseCode if a command was handled, or NoAction if no matching command was found.</returns>
        public async Task <BotResponseCode> TryHandleCommand(UpdateDto updateDto)
        {
            foreach (var command in Enum.GetValues(typeof(TCommand)).Cast <TCommand>())
            {
                if (_commandsService.IsCommand(updateDto.ParsedTextMessage, command))
                {
                    var errorText = await ValidateRequirements(command, updateDto);

                    if (!string.IsNullOrEmpty(errorText))
                    {
                        // If there is a chat, answer with the errorText.
                        if (updateDto.ParsedChat != null)
                        {
                            await _sendMessageService.SendTextMessage(updateDto.ParsedChat.Id, errorText);
                        }

                        return(BotResponseCode.CommandRequirementNotFulfilled);
                    }

                    var responseCode = await HandleCommand(command, updateDto);

                    if (responseCode != BotResponseCode.NoAction)
                    {
                        return(responseCode);
                    }
                }
            }

            return(BotResponseCode.NoAction);
        }
Esempio n. 3
0
 private Task <int> SendMessage(string text, bool isPrivateChat = false, int replyToMessageId = 0)
 {
     return(_sendMessageService.SendTextMessage(isPrivateChat
         ? _testOptions.PrivateTestChatId
         : _testOptions.GroupTestChatId
                                                , text, replyToMessageId: replyToMessageId));
 }
Esempio n. 4
0
        public async Task <BotResponseCode> TryRemoveTrackFromPlaylist(UpdateDto updateDto)
        {
            if (updateDto.Chat == null ||
                updateDto.Track == null)
            {
                return(BotResponseCode.CommandRequirementNotFulfilled);
            }

            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 remove this track from the Spotify playlist.");

                return(BotResponseCode.CommandRequirementNotFulfilled);
            }

            await RemoveTrack(spotifyClient, updateDto.Track);

            return(BotResponseCode.TrackRemovedFromPlaylist);
        }
Esempio n. 5
0
        // 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;
            }
        }