Amazon Alexa Error "The response is invalid" - firebase

Facing problem with aws lambda and alexa, I have aws lambda function:
function firebaseData(callback)
{
if(firebase.apps.length == 0) {
firebase.initializeApp({
serviceAccount: "******",
databaseURL:"********"
});
}
var ref = firebase.database().ref();
var usersRef = ref.child('users');
usersRef.once('value').then(function (snap) {
snap.forEach(function(childSnapshot) {
var childKey = childSnapshot.val();
var contact = childKey.Contact;
firebase.database().goOffline();
callback(contact);
});
});
}
**************************************************
function getContactFromSession(intent, session, callback) {
const repromptText = null;
const sessionAttributes = {};
let shouldEndSession = false;
firebaseData(function (contact) {
let speechOutput = "amy's contact is " + contact;
callback(
sessionAttributes,
buildSpeechletResponse(intent.name, speechOutput, repromptText, shouldEndSession)
);
})
}
When using contact value by calling firebaseData(function(contact){}) in
getContactFromSession(intent, session, callback) using callback(
sessionAttributes,
buildSpeechletResponse(intent.name, speechOutput, repromptText, shouldEndSession)
);
Giving error in Alexa skill output as:
Unable to call remote endpoint or the response is invalid
What might be wrong with the callback? Can't figure it out.

Related

Solana Transferring SPL Token On Demand

I am attempting to transfer tokens when a user clicks a button. Once a user connects their wallet I have this method that gets called when a user clicks a button
async function claimTokens() {
setShowModal(false);
setIsLoading(true);
var data = await axios.get(`/api/claim?wallet=${wallet.publicKey.toBase58()}`)
if(data.status != 200)
{
notify({ type: 'error', message: `Error with our API, please retry in a few minutes` });
return
} else {
let sig;
try {
var newTransaction = new Transaction();
newTransaction.signatures = data.data.data.signers;
newTransaction.instructions = data.data.data.instructions;
newTransaction.recentBlockhash = data.data.data.recentBlockhash;
newTransaction.feePayer = data.data.data.feePayer;
console.log(newTransaction)
sig = await sendTransaction(newTransaction, connection);
notify({ type: 'error', message: `Claimed tokens!` });
} catch (error) {
console.log(error)
}
}
}
when I console.log the newTransaction it shows a transaction that looks like this transaction screenshot
When I call the API to get the transaction here is what the method looks like.
try {
const connection = new web3.Connection(
"OMITTING URL",
'confirmed',
);
const { wallet } = req.query
const key = <OMITTED-FOR-OBVIOUS-REASONS>
const account = web3.Keypair.fromSeed(key.slice(0, 32));
console.log(account.publicKey.toBase58())
const wantedTokenAddress = new web3.PublicKey(process.env.TOKEN_ADDRESS)
const transaction = new web3.Transaction()
const toPublicKey = new web3.PublicKey(wallet);
const tokenAccount = await getOrCreateAssociatedTokenAccount(
connection,
account,
wantedTokenAddress,
account.publicKey
)
let toATA = await splToken.getOrCreateAssociatedTokenAccount(
connection,
account,
wantedTokenAddress,
toPublicKey,
false,
).catch(() => {/*nothing*/ });
if (!toATA) {
const associatedToken = await splToken.getAssociatedTokenAddress(
wantedTokenAddress,
account.publicKey,
false,
splToken.TOKEN_PROGRAM_ID,
splToken.ASSOCIATED_TOKEN_PROGRAM_ID
);
transaction.add(
splToken.createAssociatedTokenAccountInstruction(
account.publicKey,
associatedToken,
account.publicKey,
wantedTokenAddress,
splToken.TOKEN_PROGRAM_ID,
splToken.ASSOCIATED_TOKEN_PROGRAM_ID
)
);
} else {
transaction.add(
splToken.createTransferInstruction(
tokenAccount.address,
toATA.address,
account.publicKey,
1,
[],
TOKEN_PROGRAM_ID
)
)
}
let blockhash = await (await connection.getLatestBlockhash('finalized')).blockhash;
transaction.recentBlockhash = blockhash;
transaction.feePayer = toPublicKey;
transaction.sign(account)
console.log(transaction)
res.status(200).json({ data: transaction })
} catch (error) {
console.log(error)
res.status(500).json({ data: null })
}
There error I am getting is when I try to send the transaction I am getting
x.pubkey.toBase58 is not a function
That error is thrown inside of the sendTransaction method

FCM very slow and unreliable when sending to a group of recipients through Cloud Function

I have the following Function that:
Listens for document (text message) creation
Grab IDs of members of a group chat
Get the FCM Tokens for each member
With a for-loop, send messages to group members
exports.sendChatMessage = functions.firestore
.document("chats/{mealID}/messages/{messageID}")
.onCreate((snap, context) => {
const data = snap.data();
const mealID = context.params.mealID;
const senderID = data.senderID;
const senderName = data.senderName;
const messageContent = data.content;
var docRef = db.collection("chats").doc(mealID);
docRef
.get()
.then((doc) => {
if (doc.exists) {
const docData = doc.data();
const mealName = docData.name;
const userStatus = docData.userStatus;
var users = docData.to;
var eligibleUsers = users.filter(
(user) => userStatus[user] == "accepted"
);
eligibleUsers.push(docData.from);
// get fcmTokens from eligibleUsers and send the messagme
db.collection("users")
.where("uid", "in", eligibleUsers)
.get()
.then((snapshot) => {
var fcmTokens = [];
var thumbnailPicURL = "";
// get thumbnailpic of the sender and collect fcmTokens
snapshot.forEach((doc) => {
if (doc.data().uid == senderID) {
thumbnailPicURL =
doc.data().thumbnailPicURL == null
? "https://i.imgur.com/8wSudUk.png"
: doc.data().thumbnailPicURL;
} else {
fcmTokens.push(doc.data().fcmToken);
}
});
// send the message fcmTokens
fcmTokens.forEach((token) => {
if (token != "") {
const fcmMessage = {
message: {
token: token,
notification: {
title: mealName,
body: senderName + ": " + messageContent,
image: thumbnailPicURL,
},
apns: {
payload: {
aps: {
category: "MESSAGE_RECEIVED",
},
MEAL_ID: mealID,
},
},
},
};
tokenManger.sendFcmMessage(fcmMessage);
}
});
return true;
});
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
return false;
}
})
.catch((error) => {
console.log("Error getting document:", error);
return false;
});
return true;
});
My send function comes from a helper file that uses the HTTP V1 protocol to build the send-request:
const { google } = require("googleapis");
const https = require("https");
const MESSAGING_SCOPE = "https://www.googleapis.com/auth/firebase.messaging";
const SCOPES = [MESSAGING_SCOPE];
const PROJECT_ID = MY_PROJECT_ID;
const HOST = "fcm.googleapis.com";
const PATH = "/v1/projects/" + PROJECT_ID + "/messages:send";
exports.getAccessToken = () => {
return new Promise(function (resolve, reject) {
const key = require("./service-account.json");
var jwtClient = new google.auth.JWT(
key.client_email,
null,
key.private_key,
SCOPES,
null
);
jwtClient.authorize(function (err, tokens) {
if (err) {
reject(err);
return;
}
resolve(tokens.access_token);
});
});
};
//send message
exports.sendFcmMessage = (fcmMessage) => {
this.getAccessToken().then(function (accessToken) {
var options = {
hostname: HOST,
path: PATH,
method: "POST",
headers: {
Authorization: "Bearer " + accessToken,
},
// … plus the body of your notification or data message
};
var request = https.request(options, function (resp) {
resp.setEncoding("utf8");
resp.on("data", function (data) {
console.log("Message sent to Firebase for delivery, response:");
console.log(data);
});
});
request.on("error", function (err) {
console.log("Unable to send message to Firebase");
console.log(err);
});
request.write(JSON.stringify(fcmMessage));
request.end();
});
};
It worked all fine in the emulator but once deployed, there're significant delays (~3 mins):
I also noticed that the console says the cloud function finishes execution BEFORE sendFcmMessage logs success messages.
I did some research online, it appears that it might have something to do with the usage of Promise but I wasn't sure if that's the sole reason or it has something to do with my for-loop.
The Problem
To summarize the issue, you are creating "floating promises" or starting other asynchronous tasks (like in sendFcmMessage) where you aren't returning a promise because they use callbacks instead.
In a deployed function, as soon as the function returns its result or the Promise chain resolves, all further actions should be treated as if they will never be executed as documented here. An "inactive" function might be terminated at any time, is severely throttled and any network calls you make (like setting data in database or calling out to FCM) may never be executed.
An indicator that you haven't properly chained the promises is when you see the function completion log message ("Function execution took...") before other messages you are logging. When you see this, you need to look at the code you are running and confirm whether you have any "floating promises" or are using callback-based APIs. Once you have changed the callback-based APIs to use promises and then made sure they are all chained together properly, you should see a significant boost in performance.
The fixes
Sending the message data to FCM
In your tokenManger file, getAccessToken() could be reworked slightly and sendFcmMessage should be converted to return a Promise:
exports.getAccessToken = () => {
return new Promise(function (resolve, reject) {
const key = require("./service-account.json");
const jwtClient = new google.auth.JWT(
key.client_email,
null,
key.private_key,
SCOPES,
null
);
jwtClient.authorize(
(err, tokens) => err ? reject(err) : resolve(tokens.access_token)
);
});
};
//send message
exports.sendFcmMessage = (fcmMessage) => {
// CHANGED: return the Promise
return this.getAccessToken().then(function (accessToken) {
const options = {
hostname: HOST,
path: PATH,
method: "POST",
headers: {
Authorization: "Bearer " + accessToken,
},
// … plus the body of your notification or data message
};
// CHANGED: convert to Promise:
return new Promise((resolve, reject) => {
const request = https.request(options, (resp) => {
resp.setEncoding("utf8");
resp.on("data", resolve);
resp.on("error", reject);
});
request.on("error", reject);
request.write(JSON.stringify(fcmMessage));
request.end();
});
});
};
However, the above code was built for googleapis ^52.1.0 and google-auth-library ^6.0.3. The modern versions of these modules are v92.0.0 and v7.11.0 respectively. This means you should really update the code to use these later versions like so:
// Import JWT module directly
const { JWT } = require('google-auth-library');
// FIREBASE_CONFIG is a JSON string available in Cloud Functions
const PROJECT_ID = JSON.parse(process.env.FIREBASE_CONFIG).projectId;
const FCM_ENDPOINT = `https://fcm.googleapis.com/v1/projects/${PROJECT_ID}/messages:send`;
const FCM_SCOPES = ["https://www.googleapis.com/auth/firebase.messaging"];
exports.sendFcmMessage = (fcmMessage) => {
const key = require("./service-account.json"); // consider moving outside of function (so it throws an error during deployment if its missing)
const client = new JWT({
email: key.client_email,
key: key.private_key,
scopes: FCM_SCOPES
});
return client.request({ // <-- this uses `gaxios`, Google's fork of `axios` built for Promise-based APIs
url: FCM_ENDPOINT,
method: "POST",
data: fcmMessage
});
}
Better yet, just use the messaging APIs provided by the Firebase Admin SDKs that handle the details for you. Just feed it the message and tokens as needed.
import { initializeApp } from "firebase-admin/app";
import { getMessaging } from "firebase-admin/messaging";
initializeApp(); // initializes using default credentials provided by Cloud Functions
const fcm = getMessaging();
fcm.send(message) // send to one (uses the given token)
fcm.sendAll(messagesArr) // send to many at once (each message uses the given token)
fcm.sendMulticast(message) // send to many at once (uses a `tokens` array instead of `token`)
The Cloud Function
Updating the main Cloud Function, you'd get:
exports.sendChatMessage = functions.firestore
.document("chats/{mealID}/messages/{messageID}")
.onCreate((snap, context) => {
const mealID = context.params.mealID;
const { senderID, senderName, content: messageContent } = snap.data();
const docRef = db.collection("chats").doc(mealID);
/* --> */ return docRef
.get()
.then((doc) => {
if (!doc.exists) { // CHANGED: Fail fast and avoid else statements
console.log(`Could not find "chat:${mealID}"!`);
return false;
}
const { userStatus, to: users, name: mealName, from: fromUser } = doc.data();
const eligibleUsers = users.filter(
(user) => userStatus[user] == "accepted"
);
eligibleUsers.push(fromUser);
// get fcmTokens from eligibleUsers and send the message
/* --> */ return db.collection("users")
.where("uid", "in", eligibleUsers) // WARNING: This will only work for up to 10 users! You'll need to break it up into chunks of 10 if there are more.
.get()
.then(async (snapshot) => {
const fcmTokens = [];
let thumbnailPicURL = "";
// get thumbnailpic of the sender and collect fcmTokens
snapshot.forEach((doc) => {
if (doc.get("uid") == senderID) {
thumbnailPicURL = doc.get("thumbnailPicURL"); // update with given thumbnail pic
} else {
fcmTokens.push(doc.get("fcmToken"));
}
});
const baseMessage = {
notification: {
title: mealName,
body: senderName + ": " + messageContent,
image: thumbnailPicURL || "https://i.imgur.com/8wSudUk.png", // CHANGED: specified fallback image here
},
apns: {
payload: {
aps: {
category: "MESSAGE_RECEIVED",
},
MEAL_ID: mealID,
},
}
}
// log error if fcmTokens empty?
// ----- OPTION 1 -----
// send the message to each fcmToken
const messagePromises = fcmTokens.map((token) => {
if (!token) // handle "" and undefined
return; // skip
/* --> */ return tokenManger
.sendFcmMessage({
message: { ...baseMessage, token }
})
.catch((err) => { // catch the error here, so as many notifications are sent out as possible
console.error(`Failed to send message to "fcm:${token}"`, err);
})
});
await Promise.all(messagePromises); // wait for all messages to be sent out
// --------------------
// ----- OPTION 2 -----
// send the message to each fcmToken
await getMessaging().sendAll(
fcmTokens.map((token) => ({ ...baseMessage, token }))
);
// --------------------
return true;
})
.catch((error) => {
console.log("Error sending messages:", error);
return false;
});
})
.catch((error) => {
console.log("Error getting document:", error);
return false;
});
});
I found out that the culprit is my queries to db. Like #samthecodingman commented, I was creating floating Promises.
Originally, I have codes like:
db.collection("users")
.where("uid", "in", eligibleUsers)
.get()
.then((snapshot) => {...}
All I needed to do is to return that call:
return db.collection("users")
.where("uid", "in", eligibleUsers)
.get()
.then((snapshot) => {...}
Although it's still not instant delivery, it's much faster now.

Unity Firebase functions.https.onRequest call error is always Internal with error message: Response is not valid JSON object

I'm trying to handle Firebase cloud function errors in Unity but I cannot always receive an error with error code "Internal" and error message "Response is not valid JSON object."
After reading through the Firebase documentation and some other stack overflow questions I understand that there is a difference between functions.https.onCall and functions.https.onRequest. We are currently using onRequest because we need to access these functions from web, in addition to android and iOS.
The problematic cloud code:
app.post("/test", (req, res) => {
res.status(500).send("This message will never appear in the editor");
});
exports.app = functions.https.onRequest(app);
exports.ThisTestWorks = functions.https.onCall((data, context) => {
throw new functions.https.HttpsError('invalid-argument', 'This message will appear in the editor');
});
Unity code:
FirebaseProcess retVal = new FirebaseProcess();
FunctionInst.GetHttpsCallable("app/test").CallAsync(request).ContinueWith(t => {
if (t.IsFaulted || t.IsCanceled) {
foreach (var inner in t.Exception.InnerExceptions) {
if (inner is FunctionsException) {
var e = (FunctionsException) inner;
Debug.Log("e.ErrorCode: " + e.ErrorCode + ", e.Message: " + e.Message);
} else {
Debug.Log("inner.Message: " + inner.Message);
}
}
Debug.Log("t.Exception.Message " + t.Exception.Message);
Debug.Log("t.Status " + t.Status);
} else {
retVal.Success = true;
retVal.ResultJSON = t.Result.Data as string;
}
retVal.IsRunning = false;
});
This code will return the following logs:
e.ErrorCode: Internal, e.Message: Response is not valid JSON object.
t.Exception.Message One or more errors occurred.
t.Status Faulted
Full (relevant) cloud code:
'use strict';
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const cors = require('cors')({origin: true});
const express = require('express');
const cookieParser = require('cookie-parser')();
const app = express();
admin.initializeApp();
const validateFirebaseIdToken = async (req, res, next) => {
if ((!req.headers.authorization || !req.headers.authorization.startsWith('Bearer ')) &&
!(req.cookies && req.cookies.__session)) {
console.error('No Firebase ID token was passed as a Bearer token in the Authorization header.',
'Make sure you authorize your request by providing the following HTTP header:',
'Authorization: Bearer <Firebase ID Token>',
'or by passing a "__session" cookie.');
res.status(403).send('Unauthorized');
return;
}
let idToken;
if (req.headers.authorization && req.headers.authorization.startsWith('Bearer ')) {
// Read the ID Token from the Authorization header.
idToken = req.headers.authorization.split('Bearer ')[1];
} else if(req.cookies) {
// Read the ID Token from cookie.
idToken = req.cookies.__session;
} else {
// No cookie
res.status(403).send('Unauthorized');
return;
}
try {
const decodedIdToken = await admin.auth().verifyIdToken(idToken);
req.user = decodedIdToken;
next();
return;
} catch (error) {
console.error('Error while verifying Firebase ID token:', error);
res.status(403).send('Unauthorized');
return;
}
};
app.use(cors);
app.use(cookieParser);
app.use(validateFirebaseIdToken);
app.post("/test", (req, res) => {
res.status(500).send("This message will never appear in the editor");
});
exports.ThisTestFails = functions.https.onRequest(app);
exports.ThisTestWorks = functions.https.onCall((data, context) => {
throw new functions.https.HttpsError('invalid-argument', 'This message will appear in the editor');
});
Is there a reason that this doesn't return a custom error in the Unity editor? What am I missing here?
Thanks in advance!

firebase function on db trigger throws Function returned undefined, expected Promise or value

my firebase function based on realtime database trigger looks like below
exports.on_user_created = functions.database.ref("/users/{id}")
.onCreate((change, context) => {
console.log("start of on_user_created ")
const user = change.val();
console.log("New user:::" + JSON.stringify(user))
const uid = user._uid
const referralCode = user._referralCode
console.log("creating referral node for uid:" + uid + " with code:" + referralCode)
if(referralCode === undefined){
console.error("No referral code created for the user while sign up. Referral node cannot be created.")
return true
}
var db = admin.database();
var ref = db.ref('referrals')
ref.child(referralCode).set({"uid": uid}).then(
(resp) => {
console.log("referral node created")
return true
}
).catch(
(err) => {
console.error("unable to create referral node on user create:" + err)
return true
}
)
})
it on run throws
5:47:02.035 AM on_user_created Function returned undefined, expected Promise or value
I am failing to understand why
Adapted following Doug's comment below: "If you have no async work to be done, it's typical to return null"
This is because you don't return the Promise returned by the set() asynchronous operation.
You should do something like:
exports.on_user_created = functions.database.ref("/users/{id}")
.onCreate((change, context) => {
console.log("start of on_user_created ")
const user = change.val();
console.log("New user:::" + JSON.stringify(user))
const uid = user._uid
const referralCode = user._referralCode
console.log("creating referral node for uid:" + uid + " with code:" + referralCode)
if(referralCode === undefined){
console.error("No referral code created for the user while sign up. Referral node cannot be created.")
return null // <-- See Doug's comment below.
}
var db = admin.database();
var ref = db.ref('referrals')
return ref.child(referralCode).set({"uid": uid}).then( // <-- !! Here we return
(resp) => {
console.log("referral node created")
return null
}
).catch(
(err) => {
console.error("unable to create referral node on user create:" + err)
return null
}
)
})
Note that you could streamline your code as follows if you don't need the console.log()s, e.g. for production.
exports.on_user_created = functions.database.ref("/users/{id}")
.onCreate((change, context) => {
const user = change.val();
const uid = user._uid
const referralCode = user._referralCode
if (referralCode === undefined) {
return null;
} else {
var db = admin.database();
var ref = db.ref('referrals')
return ref.child(referralCode).set({"uid": uid});
}
});
For more detail on the importance of returning the promises in a Cloud Function, I would suggest you watch the official video series and in particular the ones titled "Learn JavaScript Promises": https://firebase.google.com/docs/functions/video-series/

How to use Mollie Api with firebase cloud functions

I could need some help setting up the node.js api from mollie with firebase cloud functions. I tried to use parts of the cloudfunction setting up paypal guide but didn't get it to work yet. I'm new to cloud functions and node.js so i have a hard time getting it done. I am using hard coded payment properties for testing purposes. I am using a blaze subscribtion to be able to do requests to non-Google services The code i have so far:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
Mollie = require("mollie-api-node");
mollie = new Mollie.API.Client;
mollie.setApiKey("test_GhQyK7Gkkkkkk**********");
querystring = require("querystring");
fs = require("fs");
exports.pay = functions.https.onRequest((req, res) => {
console.log('1. response', res)
mollie.payments.create({
amount: 10.00,
method: Mollie.API.Object.Method.IDEAL,
description: "My first iDEAL payment",
redirectUrl: "https://dummyse-afgerond",
webhookUrl: "https://us-c90e9d.cloudfunctions.net/process",
testmode: true
}, (payment)=> {
if (payment.error) {
console.error('errrr' , payment.error);
return response.end();
}
console.log('3. payment.getPaymentUrl()', payment.getPaymentUrl());
res.redirect(302, payment.getPaymentUrl());
});
});
exports.process = functions.https.onRequest((req, res) => {
let _this = this;
this.body = "";
req.on("data", (data)=> {
console.log('_this.body += data', _this.body += data)
return _this.body += data;
});
req.on("end", ()=> {
console.log('hier dan?')
let mollie, _ref;
_this.body = querystring.parse(_this.body);
if (!((_ref = _this.body) !== null ? _ref.id : void 0)) {
console.log('res.end()', res.end())
return res.end();
}
})
mollie.payments.get(
_this.body.id
, (payment) => {
if (payment.error) {
console.error('3a. err', payment.error);
return response.end();
}
console.log('4a. payment', payment);
console.log('5a. payment.isPaid()', payment.isPaid());
if (payment.isPaid()) {
/*
At this point you'd probably want to start the process of delivering the
product to the customer.
*/
console.log('6a. payment is payed!!!!!!!!!!')
} else if (!payment.isOpen()) {
/*
The payment isn't paid and isn't open anymore. We can assume it was
aborted.
*/
console.log('6a. payment is aborted!!!!!!!!!!')
}
res.end();
});
});
this is mollies api guide:
https://github.com/mollie/mollie-api-node
this is paypal cloud function guide:
https://github.com/firebase/functions-samples/tree/master/paypal
UPDATE:
I updated the code. The error i get now is that all properties of the payment variable are undefined in the procces function (webhook). and the payment.isPaid() function says false while it should say true.
I did the same when I first tried to get mollie working in firebase cloud functions, this:
let _this = this;
this.body = "";
req.on("data", (data)=> {
console.log('_this.body += data', _this.body += data)
return _this.body += data;
});
req.on("end", ()=> {
console.log('hier dan?')
let mollie, _ref;
_this.body = querystring.parse(_this.body);
if (!((_ref = _this.body) !== null ? _ref.id : void 0)) {
console.log('res.end()', res.end())
return res.end();
}
})
is not necessary.
my webhook endpoint is much simpler, directly using the request & response that functions provides:
exports.paymentsWebhook = functions.https.onRequest((request, response) => {
// ...
console.log("request.body: ", request.body);
console.log("request.query: ", request.query);
mollie.payments.get(request.body.id, function (payment) {
console.log("payment", payment);
if (payment.error) {
console.error('payment error: ', payment.error);
response.end();
}
//checking/processing the payment goes here...
response.end();
});
});

Resources