Exemplo n.º 1
0
        public static IMessageActivity NowPlayingNoContext(ConversationInfo info)
        {
            var heroCard = NewHeroCard();

            heroCard.Buttons.Add(
                new CardAction
            {
                Title = "Try again!",
                Value = $"{RingoBotHelper.RingoHandleIfGroupChat(info)}play",
                Type  = ActionTypes.ImBack,
            });

            return(MessageAttachment(
                       heroCard,
                       "Unfortunately Ringo does not support playing single Tracks or endless Playlists (including Daily Mixes) 😢 Play a Playlist or an Album in Spotify and try again."));
        }
Exemplo n.º 2
0
        public static IMessageActivity NotPlayingAnything(ConversationInfo info)
        {
            var heroCard = NewHeroCard();

            heroCard.Buttons.Add(
                new CardAction
            {
                Title = "Spotify is playing - Try again!",
                Value = $"{RingoBotHelper.RingoHandleIfGroupChat(info)}play",
                Type  = ActionTypes.ImBack,
            });

            return(MessageAttachment(
                       heroCard,
                       "You are not currently playing anything 🤔 Play some music in Spotify and try again."));
        }
Exemplo n.º 3
0
        public static IMessageActivity SpotifyError(ConversationInfo info, SpotifyApiErrorException ex, string command)
        {
            var heroCard = NewHeroCard();

            heroCard.Buttons.Add(
                new CardAction
            {
                Title = $"Try again",
                Value = $"{RingoBotHelper.RingoHandleIfGroupChat(info)}{command}",
                Type  = ActionTypes.ImBack,
            });

            return(MessageAttachment(
                       heroCard,
                       $"Ringo can't talk to Spotify right now 🤔 Please try again in a minute. Spotify says: \"{ex.Message}\""));
        }
Exemplo n.º 4
0
        public static IMessageActivity StationNoLongerPlaying(ConversationInfo info, Station station)
        {
            var    heroCard = NewHeroCard();
            string uri      = station.Album?.Uri ?? station.Playlist?.Uri;

            heroCard.Buttons.Add(
                new CardAction
            {
                Title = $"Play {station.Name}",
                Value = $"{RingoBotHelper.RingoHandleIfGroupChat(info)}play {uri}",
                Type  = ActionTypes.ImBack,
            });

            return(MessageAttachment(
                       heroCard,
                       $"{station.HashtagHandle} is no longer playing. Would you like to play \"{station.Name}\"?"));
        }
Exemplo n.º 5
0
        public static IMessageActivity NowPlayingNotSupported(ConversationInfo info, string type)
        {
            var heroCard = NewHeroCard();

            heroCard.Buttons.Add(
                new CardAction
            {
                Title = "Try again!",
                Value = $"{RingoBotHelper.RingoHandleIfGroupChat(info)}play",
                Type  = ActionTypes.ImBack,
            });

            return(MessageAttachment(
                       heroCard,
                       type == "artist"
                    ? "Unfortunately Ringo does not currently support playing Artists in Spotify 😢 Play a Playlist or an Album and try again."
                    : $"Ringo does not currently support playing a {type} in Spotify 😢 Play a Playlist or an Album and try again."));
        }
Exemplo n.º 6
0
        public static IMessageActivity CouldNotFindStation(ConversationInfo info, string query)
        {
            var heroCard = NewHeroCard();

            string messageText = string.IsNullOrEmpty(query)
                ? "Could not find a Station to join 🤔"
                : $"Could not find Station {query} 🤔";

            if (info.IsGroup)
            {
                messageText += $" Would you like to start a Station? Play some music in Spotify. Then click/tap **Play**";

                heroCard.Buttons.Add(
                    new CardAction
                {
                    Title = "Play",
                    Value = $"{RingoBotHelper.RingoHandleIfGroupChat(info)}play",
                    Type  = ActionTypes.ImBack,
                });
            }

            return(MessageAttachment(heroCard, messageText));
        }
Exemplo n.º 7
0
        public async Task Play(
            ITurnContext turnContext,
            UserProfile userProfile,
            string query,
            CancellationToken cancellationToken,
            TokenResponse token = null)
        {
            var info = RingoBotHelper.NormalizedConversationInfo(turnContext);

            // User authorised?
            token = token ?? await _authService.Authorize(turnContext, cancellationToken);

            if (token == null)
            {
                // resume after auth
                userProfile.ResumeAfterAuthorizationWith = (PlayCommand[0], query);
                return;
            }

            // Device active?
            if (!await IsDeviceActive(
                    turnContext,
                    token.Token,
                    $"{RingoBotHelper.RingoHandleIfGroupChat(turnContext)}play {query}",
                    cancellationToken))
            {
                return;
            }

            Station station = null;

            if (string.IsNullOrEmpty(query))
            {
                // Play whatever is now playing on Spotify
                station = await PlayNowPlaying(turnContext, token.Token, cancellationToken);

                if (station == null)
                {
                    return;
                }
            }
            else
            {
                // Search for a Playlist and command Spotify to Play it

                // playlist query
                string search = null;

                //if (query.Contains('#'))
                //{
                //    search = query.Substring(0, query.IndexOf('#'));
                //    hashtag = query.Substring(query.IndexOf('#') + 1);
                //}
                //else
                //{
                //    search = query;
                //}

                search = query;

                Playlist[] playlists = await _spotifyService.FindPlaylists(
                    search,
                    token.Token,
                    cancellationToken);

                if (playlists == null || !playlists.Any())
                {
                    await turnContext.SendActivityAsync($"No playlists found!", cancellationToken : cancellationToken);

                    return;
                }

                // TODO: Carousel
                Playlist playlist = playlists[0];

                if (!await IsDeviceActive(
                        turnContext,
                        token.Token,
                        $"{RingoBotHelper.RingoHandleIfGroupChat(turnContext)}play {playlist.Uri}",
                        cancellationToken,
                        playlist: playlist))
                {
                    return;
                }

                // Command Spotify to play the Playlist
                await _spotifyService.PlayPlaylist(
                    playlist.Id,
                    token.Token,
                    cancellationToken);

                if (playlist == null)
                {
                    return;
                }

                // Playlist is playing, now create a station
                station = info.IsGroup
                    ? await _ringoService.CreateConversationStation(info, playlist : playlist)
                    : await _ringoService.CreateUserStation(info, playlist : playlist);
            }

            await turnContext.SendActivityAsync(RingoBotMessages.NowPlayingStation(info, station), cancellationToken);
        }
Exemplo n.º 8
0
        public async Task Join(
            ITurnContext turnContext,
            UserProfile userProfile,
            string query,
            CancellationToken cancellationToken,
            TokenResponse token = null)
        {
            ConversationInfo info = RingoBotHelper.NormalizedConversationInfo(turnContext);

            if (string.IsNullOrEmpty(query) || (!query.StartsWith('#') && !query.StartsWith('@')))
            {
                // must specify #channel or @username in DM to join a Station
                await turnContext.SendActivityAsync(
                    RingoBotMessages.JoinWhat(),
                    cancellationToken : cancellationToken);

                return;
            }

            Station station = null;

            //if (string.IsNullOrEmpty(query))
            //{
            //    // just `join` in group chat: Play the current channel station
            //    station = await _ringoService.GetChannelStation(info);
            //}
            //else if (query.StartsWith('@'))
            if (query.StartsWith('@'))
            {
                // user station
                station = await _ringoService.GetUserStation(info, query.Substring(1));
            }
            else if (query.StartsWith('#'))
            {
                // could either be a channel station or hashtag station
                station = await _ringoService.GetChannelStation(info, query.Substring(1));
            }

            //if (station == null && query.StartsWith('@'))
            //{
            //    await CreateAndJoinUserStation(turnContext, query, cancellationToken);
            //    return;
            //}

            if (station == null)
            {
                await turnContext.SendActivityAsync(
                    RingoBotMessages.CouldNotFindStation(info, query),
                    cancellationToken : cancellationToken);

                return;
            }

            TokenResponse stationToken = await _authService.GetAccessToken(station.Owner.Id);

            // get user and their token
            token = token ?? await _authService.Authorize(turnContext, cancellationToken);

            if (token == null)
            {
                // resume after auth
                userProfile.ResumeAfterAuthorizationWith = (JoinCommand[0], query);
                return;
            }

            if (!await IsDeviceActive(
                    turnContext,
                    token.Token,
                    $"{RingoBotHelper.RingoHandleIfGroupChat(turnContext)}join {query}",
                    cancellationToken,
                    station.Playlist))
            {
                return;
            }

            // Join
            try
            {
                if (await _spotifyService.JoinPlaylist(
                        query,
                        token.Token,
                        station,
                        stationToken.Token,
                        cancellationToken))
                {
                    await turnContext.SendActivityAsync(
                        RingoBotMessages.UserHasJoined(info, station),
                        cancellationToken : cancellationToken);
                }
                else
                {
                    // station no longer playing. Start it up!
                    //await turnContext.SendActivityAsync(
                    //    RingoBotMessages.StationNoLongerPlaying(info, station),
                    //    cancellationToken: cancellationToken);

                    _logger.LogInformation($"RingobotNet.RingoBotCommands: User {station.Owner.Username} is no longer playing station \"{station.Name}\". Starting station up again with {info.FromName} as owner.");

                    switch (station.SpotifyContextType)
                    {
                    case Station.SpotifyContextTypeAlbum:
                        await _spotifyService.PlayAlbum(station.SpotifyUri, token.Token, cancellationToken);

                        break;

                    case Station.SpotifyContextTypePlaylist:
                        await _spotifyService.PlayPlaylist(station.SpotifyUri, token.Token, cancellationToken);

                        break;

                    default:
                        throw new NotSupportedException($"{station.SpotifyContextType} is not a supported SpotifyContextType");
                    }

                    await _ringoService.ChangeStationOwner(station, info);

                    await turnContext.SendActivityAsync(RingoBotMessages.NowPlayingStation(info, station), cancellationToken);
                }
            }
            catch (SpotifyApi.NetCore.SpotifyApiErrorException ex)
            {
                await turnContext.SendActivityAsync(RingoBotMessages.SpotifyError(info, ex, $"join {query}"), cancellationToken : cancellationToken);
            }
        }