Пример #1
0
        private async Task ProcessMissingScopes(MBUser user, Message message, bool isAuthorizationCallback, ILogger log)
        {
            var scopesRequierd = GetNetSpotifyScopesRequired(user);
            var missingScopes  = scopesRequierd.Except(user.SpotifyScopesList);

            if (isAuthorizationCallback)
            {
                // User already setup, this is an auth callback.. they must have denied scopes?
                // TODO: Send through error state from auth call

                log.LogWarning("User {user} denied scopes {scopes} for commmand {command}", user, string.Join(" ", missingScopes), this);

                await TelegramClient.SendTextMessageAsync(
                    message.Chat.Id,
                    $@"Sorry {user.ToTelegramUserLink()} but you need to grant additional permissions in order for us to run this command.",
                    ParseMode.Html);

                return;
            }

            // Request additional scopes
            var state = new AuthorizationState()
            {
                Message = message,
                UserId  = user.Id
            };

            log.LogInformation("User {user} needs to grant additional scopes for {command} ({misingScopes})", user, this, string.Join(' ', missingScopes));

            if (message.Chat.Id != message.From.Id)
            {
                await TelegramClient.SendTextMessageAsync(
                    message.Chat.Id,
                    $"Sure thing {user.ToTelegramUserLink()}... we need a few more permissions from you first. Check your private messages.",
                    ParseMode.Html
                    );
            }

            var response = string.IsNullOrWhiteSpace(user.SpotifyId)
                ? $"Sure! lets get you connected first. Click this link and authorize me to manage your Spotify player."
                : $"We need additional Spotify permissions from you to run the command {this.Command}. Please click this link and we'll get that sorted";

            await TelegramClient.SendTextMessageAsync(
                message.From.Id,
                response,
                ParseMode.Html,
                replyMarkup : new InlineKeyboardMarkup(
                    new InlineKeyboardButton()
            {
                Text = "Connect Account",
                Url  = SpotifyService.GetAuthorizationUri(user, state, GetNetSpotifyScopesRequired(user)).ToString()
            })
                );
        }
Пример #2
0
        private async Task <bool> TryHandleRequiresSpotifyConnection(MBUser user, ILogger logger, Message message, bool isAuthorizationCallback = false)
        {
            if (RequiresSpotify && UserIsMissingSpotify(user))
            {
                logger.LogInformation("User {user} was missing spotify authorization for command {command}", user, this);
                await RequestSpotifyAuthForUser(user, message, logger);

                return(true);
            }

            if (RequiresSpotify && UserIsMissingScopes(user))
            {
                logger.LogInformation("User {user} was missing scopes {scopes} for command {command}", user, string.Join(" ", ScopesRequired), this);
                await ProcessMissingScopes(user, message, isAuthorizationCallback, logger);

                return(true);
            }

            if (RequiresSpotify && RequiresSpotifyPremium)
            {
                var client = await SpotifyService.GetClientAsync(user);

                try
                {
                    var profile = await client.UserProfile.Current();

                    if (profile.Product != "premium")
                    {
                        logger.LogInformation("User {user} tried to use command requiring spotify premium, but found {product}", user, profile.Product);
                        await TelegramClient.SendTextMessageAsync(
                            message.Chat.Id,
                            $"Sorry {user.ToTelegramUserLink()} but you need spotify premium to use this command. Consider upgrading your account and try again.",
                            ParseMode.Html);

                        return(true);
                    }
                }
                catch (SpotifyAPI.Web.APIException ex)
                {
                    if (ex.Response.StatusCode == HttpStatusCode.BadRequest)
                    {
                        var error = JsonConvert.DeserializeObject <SpotifyError>(ex.Response.Body as string);
                        if (error.ErrorDescription == "Refresh token revoked")
                        {
                            // TODO: Make this more language natural.. currently acts like a new user
                            logger.LogWarning("User {user} revoked Spotify credentials", user);
                            await RequestSpotifyAuthForUser(user, message, logger);

                            return(true);
                        }
                    }

                    throw;
                }
            }

            return(false);
        }