How can I create a fulfillment for intent in Google Actions? - actions-on-google

I created a Google Action using the Google Actions console, then pulled it using the gactions CLI and now I am trying to connect my intents to a fulfillment webhook but don't know how.
I tried using the following code for the fulfillment from the Google Action help:
const { conversation } = require('actions-on-google');
const functions = require{'firebase-functions'};
const app = conversation();
app.handle('sayHello', conv => {
conv.add("Hi there! It\'s good to see you!");
})
exports.ActionsOnGoogleFulfillment = functions.https.onRequest(app)
Conversation doesn't seem to exist and I am having trouble finding other ways of accepting requests.
This is my current directory:
How can I send requests to the fulfillment webhook from intents?

The conversation is the term for the container when using Actions Builder or Actions SDK along with the #assistant/conversation library.
If you are using either one, you should change libraries.
If you are using Dialogflow, you would want to keep actions-on-google as the library and switch the method from app.handle to app.intent.
Additionally, this may be an encoding problem, but when your handler contains conv => { there should be a 'greater than' symbol instead:
conv => {

Related

Can I search a specific webpage in google action console?

I am trying to create a google action for the assistant to receive a name from the user and search for it on a specific website https://www.findyello.com/barbados/white-pages/?search=$person_name
I am currently unable to use the SDK and as a result, am using the Google Actions Console. Is there a way to do this solely using the Action Console?
If you want to make external API calls, you will need to have an intent that captures the search query and use a webhook to make an external API call, process the result, and return it to the user.
The code for the webhook would look similar to this:
app.handle('search', async conv => {
const query = conv.intent.params.['param_name'].original
const res = await fetch(`https://www.findyello.com/barbados/white-pages/?search=${query}`)
// Parse res into text
conv.add(`Here is your first result. ${text}`)
})

How to interact with html response from http request in Flutter

I have a Flutter app where I am running a Google Apps Script through an http request. The purpose of the script is to create a Form and link the responses to a spreadsheetID that is passed in. The script is configured to only allow Google accounts access it and I've set up the flutter app to use a Service Account to access the script using the format:
getCredentials().then( (AuthClient client){
response = client.get(url, headers{"Authorization": "Bearer ${client.access_token}");
});
Issue: The issue is that the first time that the Service Account makes a request it will get an HTML response saying that it the account needs to give permission to the script to access its data and I'm not sure how to do that.
I'm fairly new to making http requests and using it with the GoogleAPI so I'm stuck. Any advice?
Goal
Create a web page which anyone can use to submit a Google sheet link and for the app to create a form and link the sheet to that.
Authorization
For this users will require a google account and they will be required to go through the OAuth process to authorize your app.
To create the form and link it from client-side JavaScript you would indeed need to call the Apps Script API, though you cannot do this with a service account.
From: https://developers.google.com/apps-script/api/how-tos/execute
Warning: The Apps Script API doesn't work with service accounts.
Luckily, you don't need a service account to do this.
Instructions
Create an Apps Script project with a function something like:
function createForm(ssID){
form = FormApp.create("Your New Form");
form.setDestination(FormApp.DestinationType.SPREADSHEET, ssID);
let formLink = form.getPublishedUrl();
return formLink;
}
Save and take a note of the ID of the script project.
Set up a GCP project (sounds like you already have one).
Make sure the Apps Script API is enabled in your GCP.
Configure the OAuth consent screen and add the scope - https://www.googleapis.com/auth/forms.
Create an API key and a Client ID - add http://localhost:8000 or whatever port you are testing on to the "Authorized JavaScript Origins"
Create OAuth credentials "web browser (JavaScript)".
Link your Apps Script project to the same GCP project - Instructions
Deploy the Apps Script project as an API executable - take not of the deployment ID, although the documentation says that you need the script ID, it is wrong, at least with the new Apps Script IDE.
Write the client-side JavaScript in your app like what is found in the quickstart. Which will enable users to authorize the app. You need to add in the scopes and keys there too. I recommend just following the quick start steps first to get a feel for it. You can use the authorization parts without modification.
Then add in the function that will call your Apps Script, something like this:
function appsScriptCreateForm(ssId) {
var scriptId = "<DEPLOYMENT_ID>";
// Run your Apps Script function
gapi.client.script.scripts
.run({
scriptId: scriptId,
resource: {
function: "createForm",
parameters: [ssId],
},
})
.then(function (resp) {
var result = resp.result;
// ERROR HANDLING
if (result.error && result.error.status) {
appendPre("Error calling API:");
appendPre(JSON.stringify(result, null, 2));
} else if (result.error) {
var error = result.error.details[0];
appendPre("Script error message: " + error.errorMessage);
if (error.scriptStackTraceElements) {
appendPre("Script error stacktrace:");
for (var i = 0; i < error.scriptStackTraceElements.length; i++) {
var trace = error.scriptStackTraceElements[i];
appendPre("\t" + trace.function + ":" + trace.lineNumber);
}
}
// IF SUCCESSFUL
} else {
console.log("success", resp);
}
});
}
Write your HTML with the buttons and inputs necessary.
Add event listeners where appropriate.
Profit!
Please note
This set up is your project running with the authorization of other accounts.
The API requests count against your quota.
You can see details of all the executions in your GCP Project Dashboard.
Users require a Google account and need to authorize the app.
In the Apps Script function above, you just need to pass in the Spreadsheet ID. Not the whole link. You could ask for the whole link and then use Regex to extract the ID if you wanted.
This can be quite tricky and easy to miss a step or make a mistake, so double check your work.
If, after successful authorization, when trying to run the script you get a 404 error, the request has been built wrong, check your IDs. If you get a 500 error, that can mean that the Apps Script function has successfully been called, but, there was an error within Apps Script and failed, check the executions page of the Apps Script editor.
References
Apps Script How to Execute
Apps Script JS Quickstart - Highly recommended you follow these steps first and get that working!
How to link your Apps Script to GCP

Google Assistant - Action not approved

I have recently create an action and tested it in web simulator and on my Pixel 2 device. It is working fine for me. But during the review process the team at Google mentioned that while reviewing they found that error saying that my app isn't responding right now. Try again soon. (Screenshot attached). Can someone from the community please assist me on how to resolve the issue.
Below is the code in fullfillment, if this helps.
'use strict';
// Import the Dialogflow module from the Actions on Google client library.
const {dialogflow} = require('actions-on-google');
// Import the firebase-functions package for deployment.
const functions = require('firebase-functions');
// Instantiate the Dialogflow client.
const app = dialogflow({debug: true});
// Handle the Dialogflow intent named 'favorite color'.
// The intent collects a parameter named 'color'.
app.intent('think number', (conv, {nextStep}) => {
conv.close('The result is 9.');
});
// Set the DialogflowApp object to handle the HTTPS POST request.
exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);
Screenshot of the response from Review Team:
Sometimes they are a bit quick to reject if there are network problems between them and Dialogflow, or if Dialogflow isn't responding. Make sure you turn on Dialogflow's logs to make sure there is no problem.
In general, the easiest thing to do if you haven't seen any errors and things appear to be working on your end is to:
Resubmit
Reply and tell them that you're using Dialogflow and there should be a reply.

How can I use Botkit Middleware with Watson Assistant dialog server actions?

I followed this tutorial to deploy a Slackbot with Watson Assistant. The tutorial uses server actions in the dialog to directly interface with a database. To connect Slack with Watson Assistant the tutorial uses the Conversation connector. That works fine, but I am interested in how to do the same with Botkit and the Botkit Middleware provided by Watson Developer Cloud.
How can I use the serverless actions, how do I obtain and pass the necessary API key?
There is actually code that demonstrates how to configure the API key for IBM Cloud Functions and pass it as context variable to Watson Assistant. It makes use of the before method to add the API key to the context variable. The value is configured in a separate file together with the other app-related credentials. The code tests whether the context variable and the key exist, else it is added:
middleware.before = function(message, conversationPayload, callback) {
// Code here gets executed before making the call to Conversation.
// retrieve API Key from environment and split it into user / password
var arr=process.env.ICF_KEY.split(":");
// check if context exists
if (typeof(conversationPayload.context) === 'undefined') {
var context={context: {}}
Object.assign(conversationPayload, context);
}
// if credentials already exists, we don't have to add them
// else add credentials under private element in context
if (typeof(conversationPayload.context.icfcreds) === 'undefined') {
var privcontext = {"private": {icfcreds: {user: arr[0], password: arr[1]}}};
Object.assign(conversationPayload.context, privcontext);
}
// log the payload structure for debugging
// console.log(conversationPayload)
callback(null, conversationPayload);
}

How to wrap an existing chatbot for Google Assistant (Google Home)

We have a chatbot for our website today, that is not build using Google technology. The bot has a JSON REST API where you can send the question to and which replies with the corresponding answers. So all the intents and entities are being resolved by the existing chatbot.
What is the best way to wrap this functionality in Google Assistant / for Google Home?
To me it seems I need to extract the "original" question from the JSON that is send to our webservice (when I enable fullfilment).
But since context is used to exchange "state" I have to find a way to exchange the context between the dialogflow and our own chatbot (see above).
But maybe there are other ways ? Can it (invoke our chatbot) be done directly (without DialogFlow as man in the middle) ?
This is one of the those responses that may not be enough for someone who doesn't know what I am talking about and too much for someone who does. Here goes:
It sounds to me as if you need to build an Action with the Actions SDK rather than with Dialog flow. Then you implement a text "intent" in your Action - i.e. one that runs every time the user speaks something. In that text intent you ask the AoG platform for the text - see getRawInput(). Now you do two things. One, you take that raw input and pass it to your bot. Two, you return a promise to tell AoG that you are working on a reply but you don't have it yet. Once the promise is fulfilled - i.e. when your bot replies - you reply with the text you got from your bot.
I have a sample Action called the French Parrot here https://github.com/unclewill/french_parrot. As far as speech goes it simply speaks back whatever it hears as a parrot would. It also goes to a translation service to translate the text and return the (loose) French equivalent.
Your mission, should you choose to accept it, is to take the sample, rip out the code that goes to the translation service and insert the code that goes to your bot. :-)
Two things I should mention. One, it is not "idiomatic" Node or JavaScript you'll find in my sample. What can I say - I think the rest of the world is confused. Really. Two, I have a minimal sample of about 50 lines that eschews the translation here https://github.com/unclewill/parrot. Another option is to use that as a base and add code to call your bot and the Promise-y code to wait on it to it.
If you go the latter route remove the trigger phrases from the action package (action.json).
So you already have a Backend that process user inputs and sends responses back and you want to use it to process a new input flow (coming from Google Assistant)?
That actually my case, I've a service as a Facebook Messenger ChatBot and recently started developing a Google Home Action for it.
It's quite simple. You just need to:
Create an action here https://console.actions.google.com
Download GActions-Cli from here https://developers.google.com/actions/tools/gactions-cli
Create a JSON file action.[fr/en/de/it].json (choose a language). The file is your mean to define your intents and the URL to your webhook (a middleware between your backend and google assistant). It may look like this:
{
"locale": "en",
"actions": [
{
"name": "MAIN",
"description": "Default Welcome Intent",
"fulfillment": {
"conversationName": "app name"
},
"intent": {
"name": "actions.intent.MAIN",
"trigger": {
"queryPatterns": [
"Talk to app name"
]
}
}
}
],
"conversations": {
"app name": {
"name": "app name",
"url": "https://your_nodejs_middleware.com/"
}
}
}
Upload the JSON file using gactions update --action_package action.en.json --project PROJECT_ID
AFAIK, there only a Node.js client library for Actions-on-google https://github.com/actions-on-google/actions-on-google-nodejs that why you need a Node.js middleware before hitting your backend
Now, user inputs will be sent to your Node.js middleware (app.js) hosted at https://your_nodejs_middleware.com/ which may look like:
//require express and all required staff to build a Node.js server,
//look on internet how to build a simple web server in Node.js
//if you a new to this domain. const {
ActionsSdkApp } = require('actions-on-google');
app.post('/', (req, res) => {
req.body = JSON.parse(req.body);
const app = new ActionsSdkApp({
request: req,
response: res
});
// Create functions to handle requests here
function mainIntent(app) {
let inputPrompt = app.buildInputPrompt(false,
'Hey! Welcome to app name!');
app.ask(inputPrompt);
}
function respond(app) {
let userInput = app.getRawInput();
//HERE you get what user typed/said to Google Assistant.
//NOW you can send the input to your BACKEND, process it, get the response_from_your_backend and send it back
app.ask(response_from_your_backend);
}
let actionMap = new Map();
actionMap.set('actions.intent.MAIN', mainIntent);
actionMap.set('actions.intent.TEXT', respond);
app.handleRequest(actionMap); });
Hope that helped!
Thanks for all the help, the main parts of the solution are already given, but I summarize them here
action.json that passes on everything to fullfilment service
man in the middle (in my case IBM Cloud Function) to map JSON between services
Share context/state through the conversationToken property
You can find the demo here: Hey Google talk to Watson