DialogFlow in Fulfillment - DeepLink is not defined - node.js

I'm trying to use DeepLinks in Fulfillment Inline Editor in DialogFlow but I get error "DeepLink is not defined". The question is how to import DeepLink into project?
more info about deeplink class:
deeplink class
My index.js:
'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('favorite color', (conv, {color}) => {
here I have error "DeepLink is not defined"
conv.ask(new DeepLink({
destination: 'Google',
url: 'http://my.deep.link',
package: 'my.package.name',
reason: 'handle this for you',
}))
})
// Set the DialogflowApp object to handle the HTTPS POST request.
exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);
My package.json
{
"name": "dialogflowFirebaseFulfillment",
"description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase",
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"author": "Google Inc.",
"engines": {
"node": "~6.0"
},
"scripts": {
"start": "firebase serve --only functions:dialogflowFirebaseFulfillment",
"deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment"
},
"dependencies": {
"actions-on-google": "^2.2.0",
"firebase-admin": "^4.2.1",
"firebase-functions": "^0.5.7",
"dialogflow": "^0.1.0",
"dialogflow-fulfillment": "^0.4.1"
}
}
Thanks!

I've go it! :)
The line 4 should looks like:
const {dialogflow, DeepLink} = require('actions-on-google');

Related

Dialogflow: Can't send Custom Payload from inline editor

I'm trying to send custom payload from inline editor, to be displayed at DialogFlow Messenger integration. Here's my code:
// See https://github.com/dialogflow/dialogflow-fulfillment-nodejs
// for Dialogflow fulfillment library docs, samples, and to report issues
'use strict';
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const {Payload} = require('dialogflow-fulfillment');
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
function test_accordion (agent) {
const payload ={"messages": [{"richContent": [
[
{
"text": "Text to be displayed at accordion",
"type": "accordion",
"title": "Detalles:",
"image": {
"src": {
"rawUrl": "https://img2.freepng.es/20180621/ewt/kisspng-trello-logo-slack-atlassian-trello-5b2bcdc85e4d36.2783338815295973843863.jpg"
}
}
}
]
]
}]};
agent.add(new Payload(agent.UNSPECIFIED , payload, {rawPayload: true, sendAsMessage: true}));
}
let intentMap = new Map();
intentMap.set('test_accordion', test_accordion);
agent.handleRequest(intentMap);
});
And here is my packages file:
{
"name": "dialogflowFirebaseFulfillment",
"description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase",
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"author": "Google Inc.",
"engines": {
"node": "10"
},
"scripts": {
"start": "firebase serve --only functions:dialogflowFirebaseFulfillment",
"deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment"
},
"dependencies": {
"actions-on-google": "^2.2.0",
"firebase-admin": "^6.0.0",
"firebase-functions": "^2.0.2",
"dialogflow": "^0.6.0",
"dialogflow-fulfillment": "^0.5.0",
"#google-cloud/common":"^0.25.3",
"#google-cloud/projectify":"^0.3.1",
"#google-cloud/promisify":"^0.3.1",
"#google-cloud/translate":"^2.1.2",
"#google-cloud/firestore": "^0.16.1"
}
}
I'm not able to make this work. I've been searching the net for hours and I can't find any solution.
I've also tried some solutions from stackoverflow, but they seem to be outdated, as Dialogflow keeps changing their environment.
Any help would be really appreciated.
Thanks in advance!
for me, change "dialogflow-fulfillment": "^0.5.0", to "dialogflow-fulfillment": "^0.6.1" in package.json.
And in your test_accordion function
need to return agent:
return agent;

Slow response in Dialog flow with the warning message

I'm trying to use dialogflow fulfillment with the inline editor. But occasionally I get a slow response, and whenever that slow response happens, I get the following warning in the logs
Warning, estimating Firebase Config based on GCLOUD_PROJECT. Initializing firebase-admin may fail
It's weird, since I haven't required firebase-admin into my code, it's simply there in package.json as a dependency (and cannot be removed)
Here are the only things that are getting required
'use strict';
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
And this is my package.json
{
"name": "dialogflowFirebaseFulfillment",
"description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase",
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"author": "Google Inc.",
"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": "^5.13.1",
"firebase-functions": "^2.0.2",
"dialogflow": "^0.6.0",
"dialogflow-fulfillment": "^0.5.0"
}
}
The warning message is only that - a warning. It does not mean there is a problem. It is probably happening during environment initialization when the Cloud Function is cold started - hence the slightly slower start.
If you are not using firebase-admin calls, then you have nothing to worry about.
If you are using firebase-admin, then you just need to be sure that the environment is referencing the project you expect.

How to add new dependency in Package.json file in DialogFlow?

I just started creating a chatbot and I want it to be multilingual. So, I am using i18n module for the same. I added it's dependency in the package.json but now it is showing an error:
The deployment of your Cloud Function failed:
Build failed: Build error details not available
This is my package.json file:
[{
"name": "dialogflowFirebaseFulfillment",
"description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase",
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"author": "Google Inc.",
"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": "^5.13.1",
"firebase-functions": "^2.0.2",
"dialogflow": "^0.6.0",
"dialogflow-fulfillment": "^0.5.0"
}
},
{
"name": "i18n",
"description": "lightweight translation module with dynamic json storage",
"version": "0.8.4",
"homepage": "http://github.com/mashpie/i18n-node",
"repository": {
"type": "git",
"url": "http://github.com/mashpie/i18n-node.git"
},
"author": "Marcus Spiegel <marcus.spiegel#gmail.com>",
"main": "./index",
"keywords": [
"template",
"i18n",
"l10n"
],
"directories": {
"lib": "."
},
"dependencies": {
"debug": "*",
"make-plural": "^6.0.1",
"math-interval-parser": "^2.0.1",
"messageformat": "^2.3.0",
"mustache": "*",
"sprintf-js": "^1.1.2"
},
"devDependencies": {
"async": "^3.1.0",
"cookie-parser": "^1.4.4",
"express": "^4.16.4",
"jshint": "*",
"mocha": "^6.2.2",
"should": "*",
"sinon": "*",
"url": "^0.11.0",
"zombie": "*"
},
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"jshint": "jshint --verbose .",
"test": "npm run jshint && mocha --exit",
"test-ci": "npm run jshint && istanbul cover mocha -- --exit"
},
"license": "MIT"
}]
And this is my index.js file:
// See https://github.com/dialogflow/dialogflow-fulfillment-nodejs
// for Dialogflow fulfillment library docs, samples, and to report issues
'use strict';
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const i18n= require('i18n');
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
i18n.configure({
locales : ['en-IN','hi-IN-1'],
directory : '',
defaultLocale : 'en-IN'
});
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
function welcome(agent) {
agent.add(`Welcome to my agent!`);
}
function fallback(agent) {
agent.add(`I didn't understand`);
agent.add(`I'm sorry, can you try again?`);
}
function About(agent){
agent.add(`We are a company!`);
}
// // Uncomment and edit to make your own intent handler
// // uncomment `intentMap.set('your intent name here', yourFunctionHandler);`
// // below to get this function to be run when a Dialogflow intent is matched
// function yourFunctionHandler(agent) {
// agent.add(`This message is from Dialogflow's Cloud Functions for Firebase editor!`);
// agent.add(new Card({
// title: `Title: this is a card title`,
// imageUrl: 'https://developers.google.com/actions/images/badges/XPM_BADGING_GoogleAssistant_VER.png',
// text: `This is the body text of a card. You can even use line\n breaks and emoji! 💁`,
// buttonText: 'This is a button',
// buttonUrl: 'https://assistant.google.com/'
// })
// );
// agent.add(new Suggestion(`Quick Reply`));
// agent.add(new Suggestion(`Suggestion`));
// agent.setContext({ name: 'weather', lifespan: 2, parameters: { city: 'Rome' }});
// }
// // Uncomment and edit to make your own Google Assistant intent handler
// // uncomment `intentMap.set('your intent name here', googleAssistantHandler);`
// // below to get this function to be run when a Dialogflow intent is matched
// function googleAssistantHandler(agent) {
// let conv = agent.conv(); // Get Actions on Google library conv instance
// conv.ask('Hello from the Actions on Google client library!') // Use Actions on Google library
// agent.add(conv); // Add Actions on Google library responses to your agent's response
// }
// // See https://github.com/dialogflow/fulfillment-actions-library-nodejs
// // for a complete Dialogflow fulfillment library Actions on Google client library v2 integration sample
// Run the proper function handler based on the matched Dialogflow intent name
let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Default Fallback Intent', fallback);
intentMap.set('About the company', About);
// intentMap.set('your intent name here', googleAssistantHandler);
agent.handleRequest(intentMap);
});
Most probably, the error is in the JSON file. Can anybody please help me with this?
You can add the npm package: i18n to your dependencies object in package.json like this:
"dependencies": {
"actions-on-google": "^2.2.0",
"firebase-admin": "^5.13.1",
"firebase-functions": "^2.0.2",
"dialogflow": "^0.6.0",
"dialogflow-fulfillment": "^0.5.0",
"i18n" : "^0.8.4"
}
You appear to be using multiple objects in a list inside your package.json file, I'm not sure if this is how package.json files are meant to work.
[{
},{
}]
Could you try compress all your sections down into a single object, eg.
{
}
ie, move dependencies into a single section etc.

google.docs is not a function

I want to use Google's Docs API but am running into an issue. I followed https://developers.google.com/docs/api/quickstart/nodejs but am having trouble setting it up.
My index.js looks like this:
const functions = require('firebase-functions');
const {google} = require('googleapis');
const {WebhookClient} = require('dialogflow-fulfillment');
const {SimpleResponse, BasicCard, SignIn, Image} = require('actions-on-google');
const calendar = google.calendar('v3');
const people = google.people('v1');
const drive = google.drive('v3');
const docs = google.docs('v1');
process.env.DEBUG = 'dialogflow:*'; // Enable lib debugging statements
My package.json:
"name": "DialogflowFirebaseWebhook",
"description": "Firebase Webhook dependencies for a Dialogflow agent.",
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"author": "Google Inc.",
"engines": {
"node": "6"
},
"scripts": {
"lint": "semistandard --fix \"**/*.js\"",
"start": "firebase deploy --only functions, node index.js",
"deploy": "firebase deploy --only functions"
},
"dependencies": {
"firebase-functions": "^2.0.2",
"firebase-admin": "^5.13.1",
"googleapis": "^27.0.0",
"actions-on-google": "2.2.0",
"dialogflow-fulfillment": "0.6.1",
"dialogflow": "0.6.0",
"client-oauth2": "4.2.5"
}
}
Showing only the relevant parts of my script, since it's pretty long, I get this error when I try to run the actual script.
Detailed stack trace: TypeError: google.docs is not a function
Any ideas what I am doing wrong?
Turning my comment into an answer...
It doesn't look like you have anywhere near the current version39 of google apis installed.
You have version 27.
Please run npm install googleapis#39 --save like the doc says.

Firebase functions cannot deploy : SyntaxError: Unexpected token function

I am trying to deploy a function to firebase and I get an error during deployment
Error: Functions did not deploy properly.
Could it be linked with the async function ?
Actual behavior
Functions get deployed with errors, the cli shows me the following message:
================ console log ================
> eslint .
✔ functions: Finished running predeploy script.
i functions: ensuring necessary APIs are enabled...
✔ functions: all necessary APIs are enabled
i functions: preparing functions directory for uploading...
i functions: packaged functions (56.39 KB) for uploading
✔ functions: functions folder uploaded successfully
i functions: updating function sendContactEmailOAuth...
⚠ functions[sendContactEmailOAuth]: Deployment error.
Function load error: Code in file index.js can't be loaded.
Is there a syntax error in your code?
Detailed stack trace: /user_code/index.js:13
async function getJwt() {
^^^^^^^^
================ functions index.js file ================
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const { JWT } = require('google-auth-library/build/src/index');
exports.sendContactEmailOAuth = functions.https.onRequest((req, res) => {
const sender_msg = 'just a test'
const email = 'contact#lechorodescharentes.org'
async function getJwt() {
const client = new JWT(
functions.config().service_key.client_email,
null,
functions.config().service_key.private_key,
['https://www.googleapis.com/auth/cloud-platform', 'https://mail.google.com'],
);
await client.authorize();
const url = `https://www.googleapis.com/dns/v1/projects/${functions.config().service_key.project_id}`;
const res = await client.request({ url });
console.log(res.data);
}
getJwt();
/* send email with nodemailer to be inserted here */
});
================ package.json file ================
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "eslint .",
"serve": "firebase serve --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"dependencies": {
"firebase-admin": "~5.12.0",
"firebase-functions": "^1.0.2",
"firebase-tools": "^3.18.4",
"google-auth-library": "^1.4.0",
"nodemailer": "^4.6.4"
},
"devDependencies": {
"eslint": "^4.12.0",
"eslint-plugin-promise": "^3.6.0"
},
"private": true
}
As of September 2019:
Update firebase-admin : npm install --save firebase-admin
Update firebase-functions : npm install --save firebase-functions
Add "engines": { "node": "10" } to your /functions/package.json
...
"dependencies": {
"firebase-admin": "^8.5.0",
"firebase-functions": "^3.2.0"
},
"devDependencies": {
"tslint": "~5.19.0",
"typescript": "~3.6.2"
},
"engines": {
"node": "10"
}
...
As of August 2018:
Cloud Functions now support Node 8 (8.11.1). Check out this blog post.
Upgrade to Node 8
As suggested in this blog post, follow these steps to upgrade to Node 8:
Upgrade your firebase-functions version via npm install --save firebase-functions#latest
Upgrade firebase-tools via npm update -g firebase-tools
Add "engines": { "node": "8" } to your /functions/package.json
If you are still having the issue on a recent version (such as node 12), use the ecmaVersion parser option in your .eslintrc.js file.
Here's a sample:
module.exports = {
root: true,
env: {
es6: true,
node: true,
},
parserOptions: {
ecmaVersion: 8,
},
extends: [
"eslint:recommended",
"google",
],
rules: {
quotes: ["error", "double"],
},
};
h/t to Dean for the original suggestion.

Resources