Last active
April 29, 2017 03:13
-
-
Save Danetag/297cfd85a08464088fea489fcd96c942 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * requestPlaylist: Callback for the say_request_playlist intent | |
| * @param {Object} assistant Assistant instance. | |
| */ | |
| const requestPlaylist = function(assistant) { | |
| // formatted data from current intent | |
| let artists = assistant.data.artist || []; | |
| let genres = assistant.data.genre || []; | |
| let moods = assistant.data.mood || []; | |
| // raw data from current intent | |
| let genres_original = assistant.data['genre.original'] || []; | |
| let moods_original = assistant.data['mood.original'] || []; | |
| if (artists.constructor !== Array) artists = [artists]; | |
| if (genres.constructor !== Array) genres = [genres]; | |
| if (moods.constructor !== Array) moods = [moods]; | |
| if (genres_original.constructor !== Array) genres_original = [genres_original]; | |
| if (moods_original.constructor !== Array) moods_original = [moods_original]; | |
| // assistant.data has consistent data object (probably better using cookies) | |
| // Also in this case, probably better using context | |
| const savingPlaylist = assistant.data.saving_playlist || false; | |
| const hasStarted = assistant.data.hasStarted || false; | |
| assistant.data.currentStep = 'playlist'; | |
| // If it's an intent of adjusting the playlist during the review | |
| // we redirect to the correct action | |
| if (hasStarted) { | |
| if (moods.length > 0) { | |
| add_mood(assistant); | |
| return; | |
| } | |
| if (genres.length > 0) { | |
| add_genre(assistant); | |
| return; | |
| } | |
| if (artists.length > 0) { | |
| add_artist(assistant); | |
| return; | |
| } | |
| } | |
| // If you were trying to save but executes this action instead, | |
| // we redirect | |
| if (savingPlaylist) { | |
| assistant.data.name = assistant.getRawInput(); | |
| name_playlist(assistant); | |
| return; | |
| } | |
| // nothing given | |
| if (!Object.keys(assistant.data).length) { | |
| assistant.ask(ANSWERS.RESULT_FAILED); | |
| return; | |
| } | |
| // Build Spotify object based on current data | |
| const spotify = new Spotify(assistant.data); | |
| // nothing given | |
| if (!genres.length && !artists.length && !moods.length) { | |
| assistant.ask(ANSWERS.RESULT_FAILED); | |
| return; | |
| } | |
| // Construct answer | |
| let hasGivenSomething = false; | |
| // Mood | |
| let moodStr = ''; | |
| if (moods.length > 0) { | |
| const moodStrList = _addType(spotify, moods, moodsOriginal, 'MOOD'); | |
| moodStr = ANSWERS.REQUEST_MOOD_START(moodStrList); | |
| hasGivenSomething = true; | |
| } | |
| // Genre | |
| let genreStr = ''; | |
| if (genres.length > 0) { | |
| // add a comma on the previous string if has started already | |
| if (hasGivenSomething && moodStr) moodStr += ', '; | |
| const genreStrList = _addType(spotify, genres, genresOriginal, 'GENRE'); | |
| genreStr = hasGivenSomething ? ANSWERS.REQUEST_GENRE_CONTINUE(genreStrList) : ANSWERS.REQUEST_GENRE_START(genreStrList); | |
| hasGivenSomething = true; | |
| } | |
| // Artist | |
| let artistStr = ''; | |
| if (artists.length > 0) { | |
| // add a comma on the previous string if has started already | |
| if (hasGivenSomething && genreStr.length) genreStr += ', '; | |
| const artistStrList = _addType(spotify, artists, artists, 'ARTIST'); | |
| artistStr = hasGivenSomething ? ANSWERS.REQUEST_ARTIST_CONTINUE(artistStrList) : ANSWERS.REQUEST_ARTIST_START(artistStrList); | |
| hasGivenSomething = true; | |
| } | |
| const answer = ANSWERS.REQUEST(moodStr, genreStr, artistStr); | |
| // Call the spotify API | |
| spotify.call().then(function(result) { | |
| // Set the current content | |
| assistant.setContext(CONTEXT.PLAYLIST); | |
| assistant.data.hasStarted = true; | |
| // Save the data in the assistant.data | |
| _saveDataFromSpotify(assistant, spotify); | |
| // reset intent data | |
| _resetDataParams(assistant); | |
| // Finally, output the answer | |
| assistant.ask(answer); | |
| }).catch(function (reason) { | |
| error(assistant); | |
| }); | |
| } | |
| app.post('/', function (req, res) { | |
| // [...] | |
| actionMap.set(INTENT.REQUEST_PLAYLIST, requestPlaylist); | |
| assistant.handleRequest(actionMap); | |
| }); | |
| /* UTILS */ | |
| /** | |
| * _saveDataFromSpotify: Save Spotify params as stringified JSON | |
| * so we can reconstruct a similar instance through different intents, | |
| * without loosing any data. | |
| * @param {Object} assistant Assistant instance. | |
| * @param {Object} spotify Spotify instance. | |
| */ | |
| const _saveDataFromSpotify = function(assistant, spotify) { | |
| assistant.data.params = JSON.stringify(spotify.params); | |
| assistant.data.currentPlaylist = JSON.stringify(spotify.currentPlaylist); | |
| assistant.data.currentIdxSong = spotify.currentIdxSong; | |
| } | |
| /** | |
| * _resetDataParams: Reset the assistant intent data | |
| * @param {Object} assistant Assistant instance. | |
| */ | |
| const _resetDataParams = function(assistant) { | |
| // reset current data | |
| assistant.data.artist = []; | |
| assistant.data.genre = []; | |
| assistant.data.mood = []; | |
| assistant.data['genre_original'] = []; | |
| assistant.data['mood_original'] = []; | |
| } | |
| /** | |
| * _addType: Format answer and add genres, artists or moods to the current spotify instance params. | |
| * @param {Object} spotify Spotify instance. | |
| * @param {Array} arr Array of genres, artists or moods. | |
| * @param {Array} arrOriginal Array of raw genres, artists or moods. | |
| * @param {String} type Type of data (MOOD, GENRE or ARTIST). | |
| * @param {Boolean} add Optional, adds to the Spotify instance or not. | |
| * @return String | |
| */ | |
| const _addType = function addType(spotify, arr, arrOriginal, type, add = true) { | |
| let str = ''; | |
| if (arr.length > 0) { | |
| for (let i = 0; i < arr.length; i++) { | |
| str += arrOriginal[i]; | |
| if (add) { | |
| switch (type) { | |
| case 'MOOD': spotify.addMood(arr[i]); break; | |
| case 'GENRE': spotify.addGenre(arr[i]); break; | |
| case 'ARTIST': spotify.addArtist(arr[i]); break; | |
| default: break; | |
| } | |
| } | |
| if (arr.length > 1 && i < arr.length - 1) { | |
| str += (i === arr.length - 2) ? ' and ' : ', '; | |
| } | |
| } | |
| } | |
| return str; | |
| }; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment