Table of Contents (🔎 Click to expand/collapse)
Quest Outline (🔎 Click to expand/collapse)
Level | Code | Name | Note |
---|---|---|---|
Introductory | GSP263 |
Google Assistant: Qwik Start - Dialogflow | EN |
Introductory | GSP294 |
Introduction to APIs in Google | EN |
Fundamental | GSP174 |
Google Assistant: Build an Application with Dialogflow and Cloud Functions | EN |
Advanced | GSP487 |
Google Assistant: Build a Youtube Entertainment App | EN |
Advanced | GSP486 |
Google Assistant: Build a Restaurant Locator with the Places API | EN |
Expert | GSP325 |
Build Interactive Apps with Google Assistant: Challenge Lab |
This lab is recommended for students who have enrolled in the Build Interactive Apps with Google Assistant quest.
Topics tested:
- Creating an Actions project
- Setup Dialogflow
- Configure Dialogflow intents
- Use a webhook in a Dialogflow intent
- Adding code to the right place to call the Google Translate API
As a junior developer in Jooli Inc. and recently trained with Google Cloud and Dialogflow you have been asked to help a new team (Taniwha) set up their environment. The team has asked for your help and has done some work, but needs you to complete the work.
You are expected to have the skills and knowledge for these tasks so don’t expect step-by-step guides.
You need to help the team with some of their initial work creating two new Dialogflow apps.
As soon as you sit down at your desk and open your new laptop you receive the following request to complete these tasks. Good luck!
Create a python Cloud Function that will produce the result. The code is below, make sure you set the Entry point name to magic_eight_ball
. Make sure you grant Cloud Functions Invoker to allUsers
.
Cloud Functions Invoker
main.py
import random
import logging
import google.cloud.logging
from google.cloud import translate_v2 as translate
from flask import Flask, request, make_response, jsonify
def magic_eight_ball(request):
client = google.cloud.logging.Client()
client.get_default_handler()
client.setup_logging()
choices = [
"It is certain.", "It is decidedly so.", "Without a doubt.",
"Yes - definitely.", "You may rely on it.", "As I see it, yes.",
"Most likely.", "Outlook good.", "Yes.","Signs point to yes.",
"Reply hazy, try again.", "Ask again later.",
"Better not tell you now.", "Cannot predict now.",
"Concentrate and ask again.", "Don't count on it.",
"My reply is no.", "My sources say no.", "Outlook not so good.",
"Very doubtful."
]
magic_eight_ball_response = random.choice(choices)
logging.info(magic_eight_ball_response)
return make_response(jsonify({'fulfillmentText': magic_eight_ball_response }))
requirements.txt
google-cloud-translate
google-cloud-logging
- Click Navigation Menu > Cloud Functions.
- Click CREATE FUNCTION.
- Name:
magic_eight_ball
- Name:
- Click Save > Next
- Entry Point:
magic_eight_ball
- Runtime:
Python 3.7
- Entry Point:
- Click DEPLOY
Use the Actions Console to start to creating an action that has the following flow:
- App: Welcome to the lab magic 8 ball, ask me a yes or no question and I will predict the future!
- User: Will I complete this challenge lab?
- App: Cannot predict now.
You need to configure:
- a fulfillment that enables a webhook to your cloud function created in task 1.
- the Default Welcome Intent Text Response to
Welcome to the lab magic 8 ball, ask me a yes or no question and I will predict the future!
- the Default Fallback Intent to enable Set this intent as end of conversation and enable Enable webhook call for this intent
First, we're going to create the Action Project.
- Go to Actions Console.
- Click Build Your Action.
- Display Name:
<INITIAL> magic 8 ball
.
- Display Name:
- Click Save.
Let's build the Actions in the project.
- Click Actions > Get Started > Custom Intent > BUILD.
- Accept the agreements and click Accept.
- Click CREATE in the Dialogflow agent creation page.
After buliding the Actions, we're going to setup the action.
- Setup Fulfillment.
- Click Fulfillment from the left-hand menu.
- Move the silder for Webhook to ENABLED
- URL:
<CLOUD_FUNCTION_URL>
(Copy from Task 1)
- Setup Default Welcome Intent Text Response
- Click Intents from the left-hand menu.
- Click Default Welcome Intent.
- Move to the Response section and delete all text responses.
- Click Add Response > Text Response.
- Add
Welcome to the lab magic 8 ball, ask me a yes or no question and I will predict the future!
- Click Save
- Setup Default Fallback Intent
- Click Intents from the left-hand menu.
- Click Default Fallback Intent.
- Enable Set this intent as end of conversation.
- Move to the Fulfillment section and click Enable fulfillment.
- Move the silder for Enable webhook call for this intent to the right.
- Click Save
Now we can test the Assistant application.
- Click Integration from the left-hand menu.
- Click Integration Settings > TEST.
- Enter
Will I complete this challenge lab?
.
Add the following code to your magic_eight_ball
cloud function. You need to determine where to insert the code.
request_json = request.get_json()
if request_json and 'queryResult' in request_json:
question = request_json.get('queryResult').get('queryText')
# try to identify the language
language = 'en'
translate_client = translate.Client()
detected_language = translate_client.detect_language(question)
if detected_language['language'] == 'und':
language = 'en'
elif detected_language['language'] != 'en':
language = detected_language['language']
# translate if not english
if language != 'en':
logging.info('translating from en to %s' % language)
translated_text = translate_client.translate(
magic_eight_ball_response, target_language=language)
magic_eight_ball_response = translated_text['translatedText']
Test with the following sentences in non-English languages:
我会完成这个挑战实验室吗?
¿Completaré este laboratorio de desafío?
இந்த சவால் ஆய்வகத்தை நான் முடிக்கலாமா?
First, update the Cloud Function code to add multilingual support.
- Go to Cloud Console.
- Click Navigation Menu > Cloud Functions.
- Click the cloud function magic_eight_ball create in Task 1.
- Click Edit > Next
- Insert the given Python code after
magic_eight_ball_response = random.choice(choices)
(line 18).
- Insert the given Python code after
- Click DEPLOY
After the status of deployment is OK, test the Assistant application with sentences in non-English languages:
- Go back to the Dialogflow Page.
- Click Integration from the left-hand menu.
- Click Integration Settings > TEST.
- Test with following sentences:
我会完成这个挑战实验室吗?
¿Completaré este laboratorio de desafío?
இந்த சவால் ஆய்வகத்தை நான் முடிக்கலாமா?