action SDK not working - google-cloud-functions

I have updated my index.js as below
'use strict';
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const {actionssdk} = require('actions-on-google');
const app = actionssdk({debug: true});
exports.dairyProduct = functions.https.onRequest((request, response) => {
console.log("request-------------->",request);
console.log("response----------------->",response);
function handleMainIntent(app) {
console.log("Inside Main Intent");
app.ask("Main Indent "+app.getRawInput());
}
function handleTextIntent() {
console.log("Inside Main Intent");
app.tell("First Text Indent");
}
let app = new ActionsSdkApp({request, response});
let actionMap = new Map();
console.log("app---------->",app);
actionMap.set(app.StandardIntents.MAIN, handleMainIntent)
actionMap.set(app.StandardIntents.TEXT, handleTextIntent);
app.ask("This sample application is developing by Thirumani Selvam.M ");
console.log("actionMap---------->",actionMap);
app.handleRequest(actionMap);
});
My updated action.json
{
"actions": [
{
"description": "Default Welcome Intent",
"name": "MAIN",
"fulfillment": {
"conversationName": "testapp"
},
"intent": {
"name": "actions.intent.MAIN",
"trigger": {
"queryPatterns": [
"Talk to Dairy Product"
]
}
}
}
],
"conversations": {
"testapp": {
"name": "testapp",
"url": "https://us-central1-samplejs6-id.cloudfunctions.net/dairyProduct",
"fulfillmentApiVersion": 2,
"inDialogIntents": [
{
"name": "actions.intent.CANCEL"
}
]
}
},
"locale": "en"
}
My package.json code
{
"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": {
"actions-on-google": "^2.0.1",
"firebase-admin": "~5.12.0",
"firebase-functions": "^1.0.1"
},
"devDependencies": {
"eslint": "^4.12.0",
"eslint-plugin-promise": "^3.6.0"
},
"private": true
}
i have deployed using "firebase deploy --only functions"
I have updated action using "gactions update --action_package action.json --project samplejs6-id"
I have updated test of actions using"gactions test --action_package action.json --project samplejs6-id"
I didn't get errors in firebase logs.
i have updated title and name in gactions as "Dairy team". it is recommending to type "Talk to Dairy team" . If i type "Talk to Dairy team" , i am getting response as "We're sorry, but something went wrong. Please try again."
Please let me know, how to fix this issue. Thanks in advance

The problem is that you're using the "actions-on-google" library version 2, but your code is written using the version 1 objects and functions. There have been some dramatic changes between the two versions. See the migration guide for details how to upgrade to the new version, or change your package.json file to use version 1.

Related

How to run front end and back end in same port to deploy in tomcat server?

I am using reactjs in the front end and node js ,express js in backend which connects to the MySQL DB.
Both are two different folders where generally I was using on different ports.
how to combine them to deploy in tomcat server . what is the right method to do this ?
This is the index file from the backend folder inside my front end folder
....
....
app.post('/data/tournament/registration',(request, response)=>{
console.log(request.body);
connection.query("INSERT INTO registrationdata VALUES ?"
,[request.body],function(error, result, fields){
if(error){
throw error;
}else{
console.log(JSON.stringify(result));
response.send(JSON.stringify(result))
}
});
});
app.listen(8080,()=>{
console.log("Connected to port 8080");
});
....
....
The package JSON file from the backend folder inside the front end folder
{
"name": "mysql_db",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node src/index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"mysql": "^2.18.1"
}
}
I use axios in the front end to fetch the data from backend.
import axios from 'axios';
export default axios.create({
baseURL:'http://localhost:8080'
});
I use the axios instance like in my front end to fetch from backend.
....
....
export const fetchTournaments=()=>async(dispatch)=>{
await mysqlDB.get('/data/tournament/fetch')
.then((response)=>{
console.log(response);
if(response.data !== {}){
dispatch( {
type: ActionTypes.FETCH_TOURNAMENT_SUCCESS,
payload:response.data
});
console.log("Fetched tournament")
History.push('/tournaments')
}else{
dispatch({type:ActionTypes.FETCH_TOURNAMENT_FAILED});
console.log("failed to Fetch tournament")
}
})
.catch((error)=>console.error(error));
};
....
....
This is the package json file of the front end which is basically the root folder
{
"name": "chess4loudoun",
"version": "0.1.0",
"private": true,
"dependencies": {
"#date-io/moment": "^1.3.13",
"#material-ui/core": "^4.9.14",
"#material-ui/icons": "^4.9.1",
"#material-ui/pickers": "^3.2.10",
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.3.2",
"#testing-library/user-event": "^7.1.2",
"axios": "^0.19.2",
"concurrently": "^5.2.0",
"material-table": "^1.57.2",
"moment": "^2.26.0",
"react": "^16.13.1",
"react-countdown": "^2.2.1",
"react-dom": "^16.13.1",
"react-google-login": "^5.1.20",
"react-material-ui-carousel": "^1.4.5",
"react-redux": "^7.2.0",
"react-router-dom": "^5.2.0",
"react-scripts": "3.4.1",
"redux": "^4.0.5",
"redux-persist": "^6.0.0",
"redux-thunk": "^2.3.0"
},
"scripts": {
"start": "react-scripts start ",
"build": "react-scripts build ",
"test": "react-scripts test",
"eject": "react-scripts eject",
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
UPDATE
As for your other question about where to put your xml file etc that we discussed in comments, you will want to use this in your web.xml file to handle any 404 as per https://create-react-app.dev/docs/advanced-configuration/
<error-page>
<error-code>404</error-code>
<location>/index.html</location>
</error-page>
For Development you can use:
Add this in your express server.js or w/e
app.listen(8080)
Then in your React App's package.json use:
"proxy": "http://localhost:8080"
In production you will want to use the path module to serve the static react files...
const express = require('express')
const path = require('path')
const app = express()
app.use(express.static(path.join(__dirname, 'build')))

Google cloud function sending command to IoT device

I am trying to write a function that will send command to a device defined in Google Cloud IoT core. Sending a command from the console works fine.
I’m using the following example from Sending commands to devices
// const cloudRegion = 'us-central1';
// const deviceId = 'my-device';
// const commandMessage = 'message for device';
// const projectId = 'adjective-noun-123';
// const registryId = 'my-registry';
const iot = require('#google-cloud/iot');
const iotClient = new iot.v1.DeviceManagerClient({
// optional auth parameters.
});
const formattedName = iotClient.devicePath(
projectId,
cloudRegion,
registryId,
deviceId
);
const binaryData = Buffer.from(commandMessage);
const request = {
name: formattedName,
binaryData: binaryData,
};
try {
const responses = await iotClient.sendCommandToDevice(request);
console.log('Sent command: ', responses[0]);
} catch (err) {
console.error('Could not send command:', err);
}
However, in my function log I get the message‘Error: Node.js v10.0.0 is a minimum requirement.‘
I have run node -v in my project and I get v10.16.3 as a result so I am confused. Can anyone offer any advice?
gcloud functions describe results :
availableMemoryMb: 256
entryPoint: updateDevice
eventTrigger:
eventType: providers/cloud.firestore/eventTypes/document.create
failurePolicy: {}
resource: projects/project/databases/(default)/documents/pubsubMessages/{docID}
service: firestore.googleapis.com
ingressSettings: ALLOW_ALL
labels:
deployment-tool: cli-firebase
name: projects/project/locations/us-central1/functions/updateDevice
runtime: nodejs8
serviceAccountEmail: email#appspot.gserviceaccount.com
sourceUploadUrl: https://storage.googleapis.com/gcf-upload-us-central1-xxx
status: ACTIVE
timeout: 60s
updateTime: '2020-05-18T09:56:29.144Z'
versionId: '46'
package.json:
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "eslint .",
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "8"
},
"dependencies": {
"#google-cloud/iot": "^2.0.0",
"#google-cloud/pubsub": "^1.7.3",
"express": "^4.17.1",
"firebase": "^7.14.4",
"firebase-admin": "^8.12.1",
"firebase-functions": "^3.6.1",
"google-auth-library": "^6.0.0",
"googleapis": "^51.0.0"
},
"devDependencies": {
"eslint": "^5.12.0",
"eslint-plugin-promise": "^4.0.1",
"firebase-functions-test": "^0.2.0"
},
"private": true
}

Received unknown parameters' when trying to update credit card optional details in Stripe

I'm trying to update the name and expiration date & year of a credit card in Stripe using Firebase Cloud Functions, based on the documentation https://stripe.com/docs/api/cards/update. Here's my code...
exports.updateStripeCard = functions.https.onRequest((request, response) => {
console.log(request.body);
return cors(request, response, () => {
stripe.customers.updateSource(
request.body.customerId,
request.body.cardId,
{ name: request.body.name
,exp_month: request.body.exp_month
,exp_year: request.body.exp_year}
).then((update) => {
// asynchronously called
response.send(update);
return true;
})
.catch(err =>{
console.log(err);
response.send(err);
return err;
});
})
});
When I test it with customerId, cardId, name, month and year, I get the error
'Received unknown parameters: name, exp_month, exp_year'
I even tried to update just the name, like the example in the documentation, and got a 'Received unknown parameters: name' error.
How on earth can I update the card's name, month and year? Is the syntax incorrect?
Here's my package.json
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"main": "index.js",
"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"
},
"engines": {
"node": "8"
},
"dependencies": {
"firebase": "7.14.0",
"firebase-admin": "^8.6.0",
"firebase-functions": "^3.6.0",
"geofire": "^4.1.2",
"stripe": "^8.39.2"
},
"devDependencies": {
"eslint": "^5.12.0",
"eslint-plugin-promise": "^4.0.1",
"firebase-functions-test": "^0.2.0"
},
"private": true
}
Any help would be greatly appreciated!
Thanks.

Angular Server Side Rendering - What to add to angular.json?

I'm following Firebase's Tutorial for SSR with Angular. Unfortunately the Tutorial is outdated, since as for Angular 6 angular-cli.json was replaced with angular.json. How do I translate the step, you can find by using the link, to the angular.json file?
For comparison, here's what my file looks like:
{
"$schema": "./node_modules/#angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"norebro": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"schematics": {
"#schematics/angular:component": {
"style": "scss"
}
},
"architect": {
"build": {
"builder": "#angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/norebro",
"index": "src/index.html",
"main": "src/main.ts",
...
To answer your question, since you are using Angular 8 you can run this command to make your project use SSR:
ng add #nguniversal/express-engine --clientProject norebro
After you run this command you should get everything setup properly by Angular for you. To check if everything is working first build your project:
npm run build:ssr
and then serve it (and check if everything is working correctly):
npm run serve:ssr
Specifically, the things that need to be modified inside angular.jsonare:
In "projects"->"norebro" (your_project_name)->"architect"->"build"->"options" section modify outputPath like this:
"architect": {
"build": {
"builder": "#angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/browser",
Then in "architect" section, add a new server section like this:
"server": {
"builder": "#angular-devkit/build-angular:server",
"options": {
"outputPath": "dist/server",
"main": "src/main.server.ts",
"tsConfig": "src/tsconfig.server.json"
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"sourceMap": false,
"optimization": {
"scripts": false,
"styles": true
}
}
}
}
As for publishing an Angular SSR application to Firebase please check this answer.
The key point is that we cannot deploy Angular SSR application using Firebase Hosting but we can do it by using Firebase Cloud Functions.
P.S: Initially, the answer was posted here but users may not find the answer to Firebase deployment here when querying (since the OP is not about deployment to Firebase). That's why I created a separate Q&A just to address that issue.
Hope this helps...
Immediately under build(but not inside) you should add the configuration for your server application. It would look something like this:
"server": {
"builder": "#angular-devkit/build-angular:server",
"options": {
"outputPath": "dist/server",
"main": "src/main.server.ts",
"tsConfig": "src/tsconfig.server.json",
"stylePreprocessorOptions": {
"includePaths": [
"src/styles"
]
}
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"outputHashing": "media"
},
"dev": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.dev.ts"
}
]
}
}
},
Note that this one has some irrelevant properties, such as stylePreprocessorOptions. Let's focus on those relevant, which are the builder, outputPath, main, and tsConfig. Those are the files that would potentially change between a Server Side Rendered application and a Client Side Rendered application.
I have also struggled with many tutorials while I was building a project with Angular Universal, the documentation of AngularFire2 was really straightforward.

Unexpected token import, can not set up ES6 in my JS project with Mocha, Chai and Sinon

I am trying to set up a simple ES6 project to play with mocha, chai and sinon in the Terminal. But whenever I try to npm run test, I keep getting an error:
/Users/UserName/workspace/UnitTesting/mocha-sinon-chai/src/App.spec.js:1
(function (exports, require, module, __filename, __dirname) { import should from "chai";
^^^^^^
SyntaxError: Unexpected token import
Here are my:
1. Package.json:
{
"name": "mocha-sinon-chai",
"version": "1.0.0",
"description": "",
"main": "index.js",
"directories": {
"test": "tests"
},
"dependencies": {
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
"sinon": "^4.4.1"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.6.1",
"mocha": "^5.0.1"
},
"scripts": {
"build": "babel src -d lib",
"test": "mocha ./tests/*.spec.js"
},
"author": "",
"license": "ISC"
}
2. .babelrc:
{
"presets": ["env"]
}
3. Root folder structure:
4. App.spec.js:
import should from "chai";
import { findOne } from "../src/App.js";
describe("App.js", function() {
it("should return true if predicate exists in the array", function() {
findOne([1, 2, 3], 3).should().be.true;
});
});
5. index.js is empty
6. App.js simply contains a normal JS function
I have checked many other similar issues here on Stackoverflow and GitHub and none of those solved my problem.
Adding this --require babel-core/register to test script inside package.json solved the problem:
"scripts": {
"build": "babel src -d lib",
"test": "mocha ./tests/*.spec.js --require babel-core/register"
},