The Google Assistant give users their own personal Google that helps them find, organize, and get things done in their world. The main way users interact with the Google Assistant is by carrying on a conversation with it. You can extend the Google Assistant by building apps that let users get things done with your products and services.
In this post, we are going to build a new app for the Google Assistant that will give us the current bitcoin price. The main idea here is to show how to use external services from an Assistant app.
If you prefer a video, you can watch the one below:
and I also wrote a code lab so you could test it on your own with code.
How does it work?
The user needs to invoke your app. You say a phrase like “Ok Google, talk to bitcoin info”. This tells Google the name of the app to talk to.
From this point onwards, the user is now talking to your app.conversation action. Your app generates dialog output, which is then spoken to the user. The user then makes requests, your app processes it, and replies back again. The user has a two way dialog until the conversation is finished.
first, open API.AI console – Once you clicked on the ‘Google’ button you will get an option to create your first app – or agent in API.AI terminology. API.AI agent will enable our app to parse the user’s requests and trigger the fulfillment so our app will be able to return the right answer.
After the login you can create your first API.AI agent. Start by giving it a name and a short description as you can see in the image below.
Create our first Entity
Entities are the value type. They help us group items by their type.
We are trying to capture from the user phrases. Kind of like filling out a form, requesting details from the user.
API.AI looks to extract these out, and will do follow up prompts until done.
This is how an entity looks in API.AI
Build The Webhook
We got for you two options here:
- If you don’t have a server running right now, You can use our demo live webhook: https://us-central1-bitcoin-info-io17.cloudfunctions.net/bitcoinInfo by just plug it to your Fulfilment.
- If you wish to test the webhook from your own server. You can take the code below and use your own server with NodeJS.
(!) Please remember that this code was build to run inside Google cloud functions or firebase functions. If you wish to run something similar with NodeJS, you can check my example on glitch.
This is the index.js that contain all the logic for our webhook.
Create our intents
An Intent is triggered by a series of “user says” phrases. This could be something like “Please tell me what is the bitcoin value in USD” or “What is the block chain size today?”.
In most cases, you need to specify 10-12 phrases to train API.AI’s machine learning algorithm. Then even if the user doesn’t say exactly the words you typed here, API.AI can still understand them!
You should create separate intents for different types of actions though.
Don’t try to combine all of them together.
In our example, we will create only 2 intents:
- Price intent – This intent will handle the main actions: fetching the bitcoin price.
- Quit intent – This intent will handle the part when the user wishes to exit the app.
Build the “price” intent
It’s time to create the intent that will fetch us bitcoin information.
First, click on the ‘Create Intent’ button.
Second, start typing few sentences that you will want to use to get information on bitcoin. For example, “please tell me what is the value of bitcoin today in USD”.
Type a few sentences so that API.AI can start training its algorithms.
You can see that while you type, API.AI automatically recognizes that the phrase includes one of the entities, so it highlights it.
Next, we are skipping the ‘events’ part, and in the API.AI ‘Action’ section we need:
- Type ‘price’ as the action name that we will pass our webhook. You can think on it as our ‘key’ for this intent when invoking the webhook. This information will let us run the right functionality in the webhook.
- Make sure that our @currency entity is not required. As in this video, we aren’t going to use it, due to time limitations. However, if you wish to extend it later, you could require it and have several options to get the value of the bitcoin in different currencies.
Finally, in the ‘Fulfillment’ section you need to check the ‘webhook’ checkbox.
But, before you can do that, you need to click on the ‘Fulfilment’ menu item and enable the ‘Webhook’.
After you enabled it you will get the option to enter your webhook parameters.
In this phase, you can just fill the URL with: https://us-central1-bitcoin-info-io17.cloudfunctions.net/bitcoinInfo
Now, click ‘Save’ and return to the ‘Intents’
and click on ‘Price’ intent.
Scroll all the way down and you will see this new option ‘Fulfilment’:
Please make sure to check the two checkboxes so API.AI will know to send its information to our webhook.
Build the “Quit” intent
A good design principle is to allow our user to end the conversation.
You should click again on ‘Create Intent’ button. Than, start typing few sentences that will end the conversation. For example, “bye bye” or “bye bitcoin info”.
Below is how this intent should look like.
You need to check the ‘end conversation’ checkbox so that it will know to really end the conversation at this point.
Test ‘price’ intent
It’s very important to check our work while we are developing it. Luckily for us, it’s very easy to do it with API.AI. All you need to do after you created the new intent is to look at the right side of the screen. Type what you wish to test and you will get the response.
In the example below, we type “please tell me what is the price of bitcoin in USD” and as you can see it’s working nicely, since we got a price in the response.
A quick way to test how our new app works on Google home and other devices will be to use the web simulator.
We need to click on ‘Integrations’ in the left side menu and then click:
- On the publish checkbox in the top-right side.
- On “Actions on Google” as this will be our integration.
Click on the ‘TEST’ button. If this is the first time you are enabling this integration, you need to click on the switch at the top-right corner to enable this integration.
The next step is to click on ‘VIEW’ in order to open the Actions on Google simulator.
You can see it in the image below.
You will get a screen that lets you talk with the simulator.
On the left side, you can type (or talk) your commands and on the right side, you can see the responses (in JSON format). Here we gave a full conversation that test our 2 intents (price and total).
We can also test our action with the automated page that API.AI created for our new app.
You need to click on ‘Deploy’ so this new app will be available to the world. Then, you can customize the url so it will be easier to remember it. In our case, we typed: “bitcoin-info”. Now, you can try it. You will see this screen:
It’s very powerful, as you can now share your creation with the world and it will work on any device that is connected to the internet and got a browser.
That it for today!