language-translator integration with Watson Assistant? - ibm-cloud

We created IBM Watson assistant using dialog skill and created language-translator API using Node js. But we don't getting proper doc or example. Let us know how to to integrated with IBM Watson assistant with ibm language-translator
app.post("/api/translate", async (req,res)=>{
try{
const translateParams = {
text: req.body.input,
modelId: req.body.lang,
};
var message = "";
const getData = await languageTranslator.translate(translateParams);
res.json(getData["result"]["translations"][0].translation);
}catch(err){
res.send("sry error");
console.log(err);
}
});

Related

How to send expo push notification to multiple devices?

I'm trying to send Expo push notifications to multiple devices. I'm retrieving the Expo tokens from Firestore. When I enter the tokens manually, it works! It sends the notification to both devices I'm using, but when I retrieve the data from Firestore, it only sends the notification to one device.
async function sendPushNotification(readx) {
const message = {
to: readx,
sound: "default",
title: "Original Title",
body: "And here is the body!",
data: { someData: "goes here" },
};
const retrieveNetwork = async () => {
try {
//const querySnapshot = await getDocs(collection(db, "cities"));
const q = query(collection(db, "users"));
const querySnapshot = await getDocs(q);
setRead(querySnapshot.docs.map((doc) => doc.data().expoUser));
setReadx(JSON.stringify(read));
} catch (e) {
alert(e);
}
};
The retrieving of data from the firestore seems to be an issue , as your code is using the Snapshot for querying the data ,it should get the token id for both the devices in the loop and then return to the await sync to call the notification function.As per the Firebase documentation on reading multiple documents, you'll see that it uses the data() function on each DocumentSnapshot to get at the fields of that document.
So try to modify accordingly,like use doc.role and doc.token instead of doc.data().role and doc.data().token.
Check this example code below:
let tokenList = []; const userNotificationTokenDocs = await db.collection("userToken").doc(userId).get() .then(querySnapshot => { querySnapshot.forEach((doc) => { console.log(doc.data().Tokens); tokenList.push(doc.data().Tokens); }); return null; });
Also you may try adding the below to your code:
userToken.forEach((token) => { console.log(token); tokens.push(token); });
Checkout these following with similar implementation:
Push notification firestore
Triggering expo sdk to push notification to users
Notification to a collection of token
Array token sending notification
Just solved. Need to change
<Button
title="Press to Send Notification"
onPress={async () => {
await sendPushNotification(expoPushToken);
}}
/>
to
<Button
title="Press to Send Notification"
onPress={async () => {
await sendPushNotification(readx);
}}
/>

Firebase: The registration token is not a valid FCM registration token

In my app's main.dart, I ran the following code:
final fcmToken = await FirebaseMessaging.instance.getToken();
I took the token and used it in my cloud function:
exports.notifyUserAddedToGroup = functions.firestore
.document("groups/{groupDocID}/groupMembers/{groupMembersDocID}")
.onWrite((change, context) => {
const FCMToken = `loooooooooooooooooooooooooooooooong
fcmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
tokennnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn`;
const payload = {
token: FCMToken,
notification: {
title: "Title",
body: "Body",
},
data: {
body: "data body",
},
};
admin.messaging().send(payload)
.then((response) => {
console.info("##MyApp## function executed successfully");
return {msg: "##MyApp## function executed succesfully"};
})
.catch((error) => {
console.info("##MyApp## error in execution");
console.log(error);
return {msg: "##MyApp## error in execution"};
});
});
I then went to Firestore and added a document into the correct collection to trigger the cloud function. When I went to the google cloud console logs, I saw the following error:
The registration token is not a valid FCM registration token
Why is my token invalid if I just generated it a few minutes before triggering the cloud function?
The problem was the hard coded FCM token in the function. I put the token in Firestore instead and queried it to use it in the function and it worked.

How can I insert data to MongoDB from Next JS page without api?

Hi I was trying to post/insert data from next js page to mongodb. I want to learn CRUD with out any api implementations. I was able to get data with getServerSidePropsbut I can't post any data to mongodb. I am using next-os's with-mongodb example.
const [username, setUsername] = useState();
const handleChangeUsername = (event) => {
setUsername(e.target.value);
event.preventDefault();
};
const handleSubmit = async (event) => {
console.log(username);
if (await db.collection("users").insertOne({username:})) {
console.log("done");
event.preventDefault();
}
event.preventDefault();
};

how to use social sharing plugin in browser(pwa) in ionic 4?

how to use social sharing plugin in browser(pwa) in ionic 4 ?
in social sharing document browser is platform supported.
but when use in browser throw warning:
Native: tried calling SocialSharing.share, but Cordova is not available. Make sure to include cordova.js or run in a device/simulator
If your aim is to support social sharing, It's now possible to access the native share data of a mobile phone
async function share(title, text, url) {
if (window.Windows) {
const DataTransferManager = window.Windows.ApplicationModel.DataTransfer.DataTransferManager;
const dataTransferManager = DataTransferManager.getForCurrentView();
dataTransferManager.addEventListener("datarequested", (ev) => {
const data = ev.request.data;
data.properties.title = title;
data.properties.url = url;
data.setText(text);
});
DataTransferManager.showShareUI();
return true;
} else if (navigator.share) {
try {
await navigator.share({
title: title,
text: text,
url: url,
});
return true;
} catch (err) {
console.error('There was an error trying to share this content');
return false;
}
}
}
You can read more Here

How can save data using cloud functions to firestore?

I saw some other answers and I tried some of those unsuccessfully.
In my case, I created an iOS app and integrated Stripe payment method, so it reaches my javascript function at cloud functions. I'm actually able to see the payments I realizes within my stripe account but I couldn't manage to save it into our firestore database.
this is my setup at Google side:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const express = require('express');
const cors = require('cors')({origin: true});
const app = express();
const stripe = require('stripe')(functions.config().stripe.token);
//I've preset this data to firebase functions middleware
function charge(req, res) {
const body = (req.body);
const userId = body.userId;
const token = body.token;
const amount = body.amount;
const currency = body.currency;
// Charge card
stripe.charges.create({
amount,
currency,
description: 'Firebase Example',
source: token,
}).then(charge => {
send(res, 200, {
// I WANNA RECORD DATA INTO MY DATABASE HERE!!
message: 'Success',
charge,
});
}).catch(err => {
console.log(err);
send(res, 500, {
error: err.message,
});
});
}
function send(res, code, body) {
res.send({
statusCode: code,
body: JSON.stringify(body),
});
}
app.use(cors);
app.post('/', (req, res) => {
// Catch any unexpected errors to prevent crashing
try {
charge(req, res);
} catch(e) {
console.log(e);
send(res, 500, {
error: `The server received an unexpected error. Please
try again and contact the site admin if the error persists.`,
});
}
});
exports.charge = functions.https.onRequest(app);
And this is our database setup where I want to save like this:
- into payments > userId... I'll save each transaction this userId does with the fields: "token", "currency" and "amount".
Note: I already have all this values in my function and also have userId.
enter image description here