I have used dialogflow fulfillment to get data from an external api. It works fine with the test console. But on being deployed on to an android app, it gives a blank response. How do I fix this? Thanks.
The code in fulfillment:
'use strict';
const axios = require('axios');
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
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 rhymingWordHandler(agent){
const word = agent.parameters.word;
agent.add(`Here are the rhyming words for ${word}`);
return axios.get(`https://api.datamuse.com/words?rel_rhy=${word}`)
.then((result) => {
result.data.map(wordObj => {
agent.add(wordObj.word);
});
});
}
let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Default Fallback Intent', fallback);
intentMap.set('Rhyme Scheme', rhymingWordHandler);
agent.handleRequest(intentMap);
});
The code in MainActivity.java
public void callback(AIResponse aiResponse) {
if (aiResponse != null) {
// process aiResponse here
String botReply = aiResponse.getResult().getFulfillment().getSpeech();
Log.d(TAG, "Bot Reply: " + botReply);
showTextView(botReply, BOT);
} else {
Log.d(TAG, "Bot Reply: Null");
showTextView("There was some communication issue. Please Try again!", BOT);
}
}
Related
I'm trying to call an external POST API from my firebase function but it's not working as expected. I'm calling the firebase function from my android app. Following are the code snippets for my function, android call and the error that I'm getting in firebase console:
Firebase Function:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const https = require('https');
admin.initializeApp(functions.config().firebase);
exports.generateToken = functions.https.onCall((data, context) => {
const variable = data.variable;
const uid = context.auth.uid;
console.log("Function Trigerred");
if(uid !== null) {
console.log("UID: " + uid);
const requestPayload = JSON.stringify({
"variable": variable
});
console.log(requestPayload);
const config = {
hostname: 'API-HOST',
port: 443,
path: '/API-PATH',
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
};
console.log(config);
const response = "";
const req = https.request(config, (res) => {
res.on('data', (data) => {
console.log(data);
res.send(data);
});
});
req.on('error', (error) => {
console.log(error);
res.end(error);
});
req.write(requestPayload);
req.end();
} else {
console.log("Invalid User");
res.send("Invalid User");
}
});
Call in App
Map<String, Object> data = new HashMap<>();
data.put("variable", "value");
FirebaseFunctions.getInstance()
.getHttpsCallable("generateToken")
.call(data)
.continueWith(task -> {
String result = (String) task.getResult().getData();
Log.e("1: ", result);
return result;
})
.addOnCompleteListener(task -> {
Log.e("2: ", "Complete");
if (!task.isSuccessful()) {
Exception e = task.getException();
if (e instanceof FirebaseFunctionsException) {
FirebaseFunctionsException ffe = (FirebaseFunctionsException) e;
FirebaseFunctionsException.Code code = ffe.getCode();
Object details = ffe.getDetails();
}
Log.e("3: ", task.getException().toString());
}
});
Firebase Console Log
{
Error: getaddrinfo ENOTFOUND API-HOST API-HOST:443 at GetAddrInfoReqWrap.onlookup [as oncomplete (dns.js:67:26)
errno: 'ENOTFOUND',
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'API-HOST',
host: 'API-HOST',
port: 443
}
However, when I tried hitting this API from Postman, I got the successful response.
I'm new to Node.js.
I am creating an App Where user can buy coins and for that I have been trying to integrate Razorpay into my Android App since a long time now. Razorpay can directly be used in Android. It sends Success or Failure results for payment and I can act accordingly (adding points to database in this case). But the problem with this approach is that I have to write points (after success) to database from the app. Which means I have to give write access for points node to user app which is not a good idea. So I wanted to use Razorpay with Firebase Cloud Functions and searching for a long time I came across this tutorial which is for web. I am quite new to Cloud Functions and hence wanted a little help for Android.
Here is the Index.js code but For Web
const functions = require("firebase-functions");
var express = require("express");
var cors = require("cors");
var request = require("request");
const crypto = require("crypto");
const key = "----insert yout key here----";
const key_secret = "----- insert key secret here ----";
var app = express();
app.use(cors({ origin: true }));
app.post("/", (req, res) => {
const amount = req.body.amount;
//Allow Api Calls from local server
const allowedOrigins = [
"http://127.0.0.1:8080",
"http://localhost:8080",
"https://-------YourFirebaseApp-----.firebaseapp.com/"
];
const origin = req.headers.origin;
if (allowedOrigins.indexOf(origin) > -1) {
res.setHeader("Access-Control-Allow-Origin", origin);
}
var options = {
method: "POST",
url: "https://api.razorpay.com/v1/orders",
headers: {
//There should be space after Basic else you get a BAD REQUEST error
Authorization:
"Basic " + new Buffer(key + ":" + key_secret).toString("base64")
},
form: {
amount: amount,
currency: "INR",
receipt:
"----- create a order in firestore and pass order_unique id here ---",
payment_capture: 1
}
};
request(options, function(error, response, body) {
if (error) throw new Error(error);
res.send(body);
});
});
app.post("/confirmPayment", (req, res) => {
const order = req.body;
const text = order.razorpay_order_id + "|" + order.razorpay_payment_id;
var signature = crypto
.createHmac("sha256", key_secret)
.update(text)
.digest("hex");
if (signature === order.razorpay_signature) {
console.log("PAYMENT SUCCESSFULL");
res.send("PAYMENT SUCCESSFULL");
} else {
res.send("something went wrong!");
res.end();
}
});
exports.paymentApi = functions.https.onRequest(app);
I think this will help you.
In my case, I am accessing items(Array of Product IDs) from the user's cart and reading the current price of the items then passing it as an argument to SendOrderId function which will return an OrderId to proceed.
The important thing to keep in mind is that you must have added razorpay in your dependencies inside package.json. You can do that by simply running
npm i razorpay
inside your functions folder (Which include index.js) which will automatically add the dependency to your project
const functions = require("firebase-functions");
const admin = require('firebase-admin');
const Razorpay = require('razorpay')
const razorpay = new Razorpay({
key_id: 'Your_razorpay_key_id',
key_secret: 'your_secret'
})
admin.initializeApp();
function SendOrderId(amountData, response) {
var options = {
amount: amountData, // amount in the smallest currency unit
currency: "INR",
};
razorpay.orders.create(options, function(err, order) {
console.log(order);
response.send(order);
});
}
exports.getOrderId = functions.https.onRequest((req, res) => {
return admin.firestore().collection('Users').doc(req.query.uid).get().then(queryResult => {
console.log(queryResult.data().Cart);
admin.firestore().collectionGroup("Products").where('ProductId', 'in', queryResult.data().Cart).get().then(result => {
var amount = 0;
result.forEach(element => {
amount += element.data().price;
});
SendOrderId(amount * 100, res);
})
})
});
How can i get the information from [object Promise]?
I am using GCF (cloud function) to process square payments.
As of right now I am getting back a response of { response="OK:[object Promise]" }
This is processing the cloud function on the cloud platform:
const functions = require('firebase-functions');
const SquareConnect = require('square-connect');
const crypto = require('crypto');
exports.fxtest = functions.https.onCall((data, context) => {
const defaultClient = SquareConnect.ApiClient.instance;
defaultClient.basePath = "https://connect.squareupsandbox.com";
const oauth2 = defaultClient.authentications['oauth2'];
oauth2.accessToken = 'sandbox-token-ommitted';
const idempotency_key = crypto.randomBytes(23).toString('hex');
const payments_api = new SquareConnect.PaymentsApi();
const item_source = data.source_id;
const item_price = 1.00;
const item_currency = 'USD';
const request_body = {
"idempotency_key": idempotency_key,
"source_id": item_source,
"amount_money": {
"amount": item_price,
"currency": item_currency
}
};
var rsp;
try{
const response = payments_api.createPayment(request_body)
.then(
r=> { return r; })
.catch(
e => { return e; });
const json = JSON.stringify('OK:' + response);
rsp = json;
} catch(error){
return rsp = 'ERROR:' + error;
}
return{
response: rsp
};
});
This is the processing the returned data on an Android device:
private FirebaseFunctions mFunctions;
private Task<HttpsCallableResult> fxtest(String text, Context ctx, CardDetails crds){
Map<String, Object> data = new HashMap<>();
data.put("source_id",crds.getNonce());
return this.mFunctions.getHttpsCallable("fxtest").call(data)
.addOnCompleteListener((Activity) ctx, new OnCompleteListener<HttpsCallableResult>() {
#Override public void onComplete(#NonNull Task<HttpsCallableResult> task) {
Toast.makeText(ctx, "result: " + task.getResult().getData(),Toast.LENGTH_LONG).show();
}
});
}
Some sources i was looking at:
connect-api-example using nodejs on github
square-conect on npm
I've found the solution to the question, basically my "credentials access token was using authorization access token" that was the first mistake, the other was due to the itempotency key which has a max limit of 45 characters according to the API reference square connect api, and the other was how i returned the response which is meant to be in JSON format as far as the promise i consumed was doing. Here is the source code, (the java is fine, no need to edit that) it was only on the nodejs side. The API keys are referenced in the environmental variables side on the GCF platform. This will effectively allow processing a square payment through android applications using "serverless approach".
const functions = require('firebase-functions');
const SquareConnect = require('square-connect');
const crypto = require('crypto');
exports.fxtest = functions.https.onCall(async (data, context) => {
/* testing url for sandbox */
//defaultClient.basePath = process.env.TESTING_SQUARE_CONNECT_URL;
const defaultClient = SquareConnect.ApiClient.instance;
defaultClient.basePath = process.env.PRODUCTION_SQUARE_CONNECT_URL;
const oauth2 = defaultClient.authentications["oauth2"];
oauth2.accessToken = process.env.PRODUCTION_APPLICATION_ACCESS_TOKEN;
const idempotency_key = crypto.randomBytes(16).toString("hex");
const payments_api = new SquareConnect.PaymentsApi() ;
/* value of amount is in cents as of 11/29/2019
, 1 is equal to 1 cent, 100 is equal to 100 cents */
const request_body = {
"idempotency_key": idempotency_key,
"source_id": data.source_id,
"amount_money": {
"amount": 100,
"currency": "USD"
},
};
try{
response = await payments_api.createPayment(request_body)
.then(
r=> {
if(r.ok) { return Promise.resolve(r); }
return Promise.reject(Error("TRY ERROR_ON_RESPONSE: " + JSON.stringify(r)))
})
.catch(
e=> {
return Promise.reject(Error("TRY ERROR_ON_EXCEPTION: " + JSON.stringify(e)))
});
return "TRY OKAY: " + JSON.stringify(response);
} catch(error){
return "CATCH ERROR: " + JSON.stringify(error);
}
});
I Making a chess app with react native, i sending & receiving my request with websocket,
when i run my app in ios every thing is ok,but when i run my app in android the web socket not open and return " Expected HTTP 101 response but was '403 Forbidden' ".
my create game code :
createGame() {
const { playConfig } = this.props;
fetch('https://en.lichess.org/setup/ai', {
method: 'POST',
headers: {
Accept: 'application/vnd.lichess.v2+json',
'Content-Type': 'application/json',
},
body: playConfig,
})
.then(res => res.json())
.then(this.onGameCreated);
}
onGameCreated = res => {
const { game } = this.state;
const socketUrl = res.url.socket;
const clientId = Math.random().toString(36).substring(2);
clearInterval(this.interval);
this.wsReady = false;
let url = `wss://socket.lichess.org${socketUrl}?sri=${clientId}&mobile=1`;
this.ws = new WebSocket(
url,
);
this.ws.onmessage = e => {
// a message was received
console.log(`received: ${e.data}`);
const data = JSON.parse(e.data);
let moveData;
let victor;
if (data.t === 'move' && data.v > game.history().length) {
moveData = data.d;
} else if (data.t === 'end') {
victor = data.d;
} else if (data.t === 'b') {
// b for batch
const first = data.d[0];
if (first) {
if (first.d.status && first.d.status.name === 'mate') {
moveData = first.d;
}
if (first.t === 'end') {
victor = first.d;
}
if (first.d.winner) {
victor = first.d.winner;
}
}
}
if (victor) {
dongSound.play();
this.setState({
victor,
});
this.ws = null;
} else if (moveData) {
const { uci, clock } = moveData;
const castle = moveData.castle;
let from = uci.substring(0, 2);
let to = uci.substring(2, 4);
if (castle && castle.king) {
from = castle.king[0];
to = castle.king[1];
}
this.board.movePiece(to, from);
if (clock) {
this.latestClock = clock;
}
}
};
this.ws.onerror = e => {
// an error occurred
console.log(e.message);
};
this.ws.onopen = () => {
this.wsReady = true;
dongSound.play();
this.setState({
initialized: true,
userColor: res.player.color === 'white' ? 'w' : 'b',
});
console.log('ws open');
// ping every second
this.interval = setInterval(
() => {
this.sendMessage({
t: 'p',
v: game.history().length,
});
},
1000,
);
};
};
any one has idea?
thank you in advance
Looks like you don't have permission to open a socket on this webserver.
I don't think the problem is in your Java code but the webserver configuration.
I have a link in the back-end, so I fetch a post request to that link and receive a response. When I alert that response it gives a body init and body text in which I receive datas I need. Everything is good. But..
When I enable remote debugging and console.log that response, it gives body init and body blob (and both are empty). It stucks when I eneble debugging..
Thanks for attention ))
My code:
logIn = async (username, password) => {
// alert(`username : ${username}\n password : ${password}`);
let loginFormData = new FormData();
loginFormData.append('LoginForm[username]', username);
loginFormData.append('LoginForm[password]', password);
loginFormData.append('MacAddress', '111');
loginFormData.append('loginType', 'mobile');
try {
fetch('http://192.168.2.115/araqich_client/general/default/logout', {
method: 'POST',
body: loginFormData
});
let request = fetch('http://192.168.2.115/araqich_client/general/default/login', {
method: 'POST',
body: loginFormData
});
let loginResponseJson = await request;
if (loginResponseJson && loginResponseJson != null ) {
// let loginResponse = JSON.parse(loginResponseJson._bodyInit);
alert(JSON.stringify(loginResponseJson._bodyInit));
let status = loginResponse.status;
if (status) {
let SyncFormData = new FormData();
let accessToken = loginResponse.ACCESS_TOKEN;
SyncFormData.append('ACCESS_TOKEN', accessToken);
SyncFormData.append('MacAddress', '111');
SyncFormData.append('loginType', 'mobile');
let syncRequest = fetch('http://192.168.2.115/araqich_client/mobile/defaultA/syncAndroid', {
method: 'POST',
body: SyncFormData
});
let syncResponseJson = await syncRequest;
if (syncResponseJson && syncResponseJson != null) {
let syncResponse = JSON.parse(syncResponseJson._bodyInit);
let status = syncResponse.status;
if (!status) {
alert('Sorry(( something went wrong...');
} else {
alert('Life is good)))');
}
}
} else {
alert('else1')
}
} else {
alert('else')
}
} catch (error) {
alert(error);
}
}
Instead of using console.log statements, I'd advise using your debugger.