Created
August 5, 2019 21:34
-
-
Save Fleker/21c0f23fc00a013582c8d568c6b86f22 to your computer and use it in GitHub Desktop.
Full search implementation
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
app.intent('Play', async (conv: Conv, params: {search: string}) => { | |
// "Forest: Day" -> "forest day" | |
const search = params.search.toLowerCase() | |
const searchResults = [] | |
for (const track of conv.data.json.tracks) { | |
const title = track.track_title.replace(/:/g, '').toLowerCase() | |
const genres = track.track_genre.map((g: string) => g.toLowerCase()) | |
const tags = track.tags.map((t: string) => t.toLowerCase()) | |
if (title.indexOf(search) > -1) { | |
conv.ask(`Here is ${track.track_title}.`) | |
conv.ask(generateMediaResponse(track)) | |
conv.ask(getSuggestions(conv.data.json.tracks)) | |
conv.data.currentTrack = track | |
return | |
} | |
if (genres.includes(search) || tags.includes(search)) { | |
// Pick a random item from the result | |
searchResults.push(track) | |
} | |
} | |
if (searchResults.length) { | |
const track = searchResults[Math.floor(Math.random() * searchResults.length)] | |
conv.ask(`I found several tracks for the category ${search}. Here is one at random: `+ | |
`${track.track_title}.`) | |
conv.ask(generateMediaResponse(track)) | |
conv.ask(getSuggestions(conv.data.json.tracks)) | |
conv.data.currentTrack = track | |
return | |
} | |
const suggestions: Suggestions = getSuggestions(conv.data.json.tracks) | |
conv.ask(`I can't find a track with that description, but I found others like ` + | |
`${suggestions.suggestions[0].title}. ` + | |
`What would you like to listen to?`) | |
conv.ask(suggestions) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment