I want to develop a system to get notifications for google business review. I set up google business sign in with appropriate scopes. I went through documentation and was able to pull from a subscription, into a node js server i made. It worked on local as intended. I pushed it to Production, but i found it is not working
async function pubsubReview() {
const starNum = (str) => {
str = str.toUpperCase();
if (str == "ONE" || str == "STAR_RATING_UNSPECIFIED") {
return 1;
} else if (str == "TWO") return 2;
else if (str == "THREE") return 3;
else if (str == "FOUR") return 4;
return 5;
};
try {
const [subs] = await pubSubClient.getSubscriptions();
console.log("Subscriptions:");
subs.forEach((s) => console.log(s.name));
const subscription = pubSubClient.subscription(subscriptionNameOrId);
// Receive callbacks for new messages on the subscription
subscription.on("message", async (message) => {
await message.ackWithResponse();
console.log("Received message:", message.data.toString());
try {
console.log("nn", JSON.parse(message.data.toString()), "nn");
const noti = JSON.parse(message.data.toString());
const accountName = noti.location.split("/locations")[0];
const reviewName = noti.review;
const review_name_parts = reviewName.split("/");
const location_name = review_name_parts[2] + "/" + review_name_parts[3];
const query = `
SELECT c.company_id, st.page_token, st.selected_template_url, st.enable_bg, c.location_ids FROM
(SELECT location_ids, company_id FROM google_my_business WHERE account_name = '${accountName}' ) c
JOIN
(SELECT * FROM socials_token ) st
ON
c.company_id = st.company_id
`;
const queryRes = await promiseQuery(query);
const pageToken = queryRes[0].page_token;
const companyId = queryRes[0].company_id;
let enable_bg = queryRes[0].enable_bg;
const locationsAddedCheckQuery = `SELECT locations_added FROM google_my_business WHERE company_id = ${companyId}`;
const locationsAddedCheckQueryRes = await promiseQuery(
locationsAddedCheckQuery
);
let locationsAddedCheck =
locationsAddedCheckQueryRes[0].locations_added;
if (!locationsAddedCheck) {
return res
.status(200)
.send({ data: "[]", success: true, toggle: false });
}
let toggleCheck = `SELECT location_ids FROM google_my_business WHERE company_id = ${companyId}`;
let toggleCheckRes = await promiseQuery(toggleCheck);
toggleCheck = JSON.parse(toggleCheckRes[0].location_ids);
toggleCheck = toggleCheck.filter((item) => {
return item.toggle && item.name == location_name;
});
console.log("nntoggle Check isn", toggleCheck);
if (toggleCheck.length > 0) {
console.log("nntrying to make post to fbn");
try {
const reviewData = await fetchReview(reviewName, companyId);
const revieweeName = reviewData.reviewer.displayName;
const reviewText = reviewData.comment;
const stars = starNum(reviewData.starRating);
if (stars >= 4) {
let templateUrl = queryRes[0].selected_template_url;
console.log("n", templateUrl, "n", pageToken, "n");
const response = await axios.get(templateUrl, {
responseType: "arraybuffer",
});
const bufferTemp = Buffer.from(response.data, "binary");
let image = await Jimp.read(bufferTemp);
let buffer = null;
if (enable_bg) {
buffer = await generateReviewImageWithBG(
image,
stars,
revieweeName,
reviewText
);
} else {
buffer = await generateReviewImage(
image,
stars,
revieweeName,
reviewText
);
}
const publicUrl = await uploadToGCP(buffer, reviewName);
console.log(
"nnreview created successfullyn",
publicUrl,
"nn"
);
const imgPost = await postImageToFB(
pageToken,
publicUrl,
"demo-post"
);
await deleteFromGCP(reviewName);
console.log("nnposted review to FB and deleted from GCP");
}
} catch (err) {
console.log("nnfailed to make post with errorn", err, "nn");
throw err; // Rethrow the error to be caught in the listener's catch block
}
} else {
console.log("nnwill not post to fbnn");
}
} catch (err) {
console.log("nGot error While running PubSub Listenern", err, "nn");
}
});
// Receive callbacks for errors on the subscription
subscription.on("error", (error) => {
console.error("Received error:", error);
});
} catch (err) {
console.log("nn", err, "nn");
}
}
pubsubReview();
I did this code, I only get posts on FB only when Im running server on my own PC.