Technology is the greatest equalizer of this century. You can build natural and rich conversational experiences, you can build a computer program that uses machine learning techniques to perform human-like conversations.
There are many use cases we can take advantage of the use of voice. In this codelab we will use Google Cloud Platform, the YouTube API and Actions on Google to create a karaoke action.
In this codelab, you're going to build a Karaoke Action. Your app will use:
|
Actions is how we name the apps for the assistant, we'll get started by creating a Dialogflow project
The welcome intent is the first message your users will get from your agent, usually a greeting. The welcome intent is also responsible for informing your users about their options and what they could do with your agent.
You've just created the welcoming response to the first interaction of the users with your karaoke app.
Entities are powerful tools used for keyword extraction. Let's create the songs entity.
"Hotel California","Hotel California" "Shooting Star","Shooting Star" "When I See You Smile","When I See You Smile" "No Matter What","No Matter What" "Walks Like A Woman","Walks Like A Woman" "No Sleep Till Brooklyn","No Sleep Till Brooklyn" "Loser","Loser" "In a Big Country","In a Big Country" "Rock Around the Clock","Rock Around the Clock" "AIN'T NO SUNSHINE","AIN'T NO SUNSHINE" "Cradle of Love","Cradle of Love" "Dancing With Myself","Dancing With Myself" "Eyes Without a Face","Eyes Without a Face" "My Life","My Life" "SHE'S ALWAYS A WOMAN","SHE'S ALWAYS A WOMAN"
"@sys.music-artist","@sys.music-artist" "Eagles","Eagles" "Supertramp","Supertramp" "Survivor","Survivor" "The Beach Boys","The Beach Boys" "The Beatles","The Beatles" "The Clash","The Clash" "The Doors","The Doors" "The Goo Goo Dolls","The Goo Goo Dolls" "The Offspring","The Offspring" "The Police","The Police" "The Pretenders","The Pretenders" "The Ramones","The Ramones" "The White Stripes","The White Stripes" "The Who","The Who" "Twisted Sister","Twisted Sister" "U2","U2" "Van Halen","Van Halen" "Whitesnake","Whitesnake" "ZZ Top","ZZ Top"
Let's create the intent that parses user input for fields that are required by the Youtube Data API.
We will use the Youtube Data API v3 for searching videos. You can find the methods supported in this link to better understand the queries used in our code.
To make Youtube Data API requests from your Cloud Function, you will need to login with your Google Account to access the Google API Console, request an API key, and register your application.
When you set up your webhook, you pass information from a matched intent into a web service and get a result from it.
index.js
as seen below."use strict";
const {
dialogflow,
actionssdk,
Image,
Button,
BasicCard,
Table,
Carousel
} = require('actions-on-google');
const functions = require('firebase-functions');
const app = dialogflow({ debug: true });
const API_KEY = "ADD_YOUR_API_KEY";
app.intent("search", (conv, {song, artist}) => {
let query = `${artist} ${song}`;
console.log(song);
if (!song || song == 'undefined') {
query = conv.query;
}
const url = "https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=5&q=" + encodeURIComponent(query + " karaoke|lyrics")+ "&type=video&order=relevance&videoCategoryId=10&key=" + API_KEY;
console.log(url);
const axios = require('axios');
return axios.get(url)
.then(response => {
var output = JSON.stringify(response.data);
var song_fields = response.data.items[1];
return song_fields;
}).then(output => {
var song_title = output.snippet.title;
song_title = song_title.replace(/&/g, '&');
song_title = song_title.replace(/"/g, '\"');
var song_link = JSON.stringify(output.id.videoId);
var song_thumbnail = output.snippet.thumbnails.high.url;
conv.ask(`Finding your song...`);
conv.ask(new BasicCard({
title: `${song_title}`,
buttons: new Button({
title: `Let's sing`,
url: `https://www.youtube.com/watch?v=${song_link.slice(1, -1)}`,
}),
image: new Image({
url: song_thumbnail,
alt: 'Song thumbnail'
}),
display: 'CROPPED',
}));
conv.close(`${song_title}. Link: https://www.youtube.com/watch?v=${song_link.slice(1, -1)}. Enjoy! Bye`);
});
});
exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);
app.js
file:const API_KEY = "ADD_YOUR_API_KEY";
package.json
file:{ "name": "dialogflowFirebaseFulfillment", "description": "karaoke.", "version": "0.0.1", "author": "Amanda Cavallaro", "engines": { "node": "8" }, "scripts": { "start": "firebase serve --only functions:dialogflowFirebaseFulfillment", "deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment" }, "dependencies": { "actions-on-google": "^2.2.0", "firebase-admin": "^7.0.0", "firebase-functions": "^2.2.0", "dialogflow": "^0.8.0", "dialogflow-fulfillment": "^0.6.1", "axios": "0.16.2" } }
Check your Google Permission Settings
It's time to use the Actions simulator to begin testing your agent.
It is important to check that necessary permissions are enabled.
Visit the
Ensure that the following permissions are enabled:
You can now close the Activity Controls page.
Test the action with the simulator
To test out your Action in the Actions console simulator:
Notice that when your Action's welcome intent is invoked, the Assistant responds with the greeting message and prompts the user to provide the music artist and song title.
Return to the Actions console. Then from the left-hand menu, under the TEST header click Simulator. You should now be on the following page:
Through the Actions simulator you are able test your action using an intuitive interface that lets you simulate hardware devices and their settings
Reply by type/ saying a music artist and a song such as Eagles - Hotel California. You should receive a similar output in the right-hand panel, which includes the song name, link, and thumbnail:
Try talking with your test app with new music artists and songs!
Congratulations!
You built a karaoke action with Actions on Google.