Good morning,
I'm having an annoying problem with some thing that's i'm trying to make using the youtube comment api , i'm trying to add automatically a comment when a video on my channel has been uploaded , it works as intended for the first 5 seconds and i get a notification that my comment was added , ,and i can see it after i uploaded my video , but after 3 - 5 seconds after , my comment gets deleted on it's own.
i looked for what could the cause of this could be , and i thought " oh maby it's considered as spam " , so i look in my uploaded video comments spam tab , and NOTHING , the comments are no where to be found.
i tried this process for around 10 times and still nothing , can someone help me out please ?
Thank you !
Here's what i do :
I subscribe to the youtube webhook to get a notification when a user uploads a new video
when i receive a notification , i send a push notification to my android app , so i can get an access_token from my android app using the google api.
once i get the token , isend it to my server so i can use it to comment on my new video.
Here is the code i use to insert a comment in the backend ( in node js ) :
function insertYoutubeComment(videoId,channel_id,message,access_token,comment){
return new Promise((resolve, reject) => {
request({
method: 'POST',
url: 'https://www.googleapis.com/youtube/v3/commentThreads',
headers: {
'User-Agent': 'Request-Promise'
},
body: {
"snippet": {
"videoId": videoId,
"channelId": channel_id,
"topLevelComment": {
"snippet": {
"textOriginal": message
}
}
}
},
qs: {
part: 'snippet',
access_token: access_token
},
json: true
}, function (error, response, body) {
console.log('body ==== ', body);
if (error) {
console.log('body', error.stack);
console.log('error in when posting comment ', error.stack);
resolve(error.stack);
// return reject(error);
}
console.log("Videos ==== ",comment.video);
let videos = comment.video
videos.push(videoId)
let bod = {}
console.log("Updating videos ");
Object.assign(bod, {
video: videos,
});
console.log("Body in update =====",body);
commentmodel.updateOne(
{ _id: comment._id },
bod,
function (err, user) {
if (err) {
console.log("Error updating ")
resolve("comment Error");
} else {
console.log("Updated user ===",user);
resolve("comment inserted");
}
}
);
//res.json({ message: "Comment inserted", status: 200, data: comment });
// return resolve(body);
});
});
}
Related
I've a photo and a paid upload service : http://example.com/in.php .
I'm not able to upload a given jpeg file using this code. It is telling me that invalid file format uploaded. But using file linux command I can see it is JPEG format. Is there any problem in this code?
fs.readFile('/tmp/photo.jpg'', 'utf8', function(err, contents) {
var b64 = new Buffer(contents);
var s = b64.toString('base64');
var request = require('request')
request.post('http://example.com/in.php', {
form: {
method:'base64',
key:'cplbhvnmvdn4bjxxchzgqyjz7rf9fy8w',
body:s,
}
}, function (err, res, body) {
console.log("body=",body);
console.log("res=",res);
})
});
I see no mistakes in your process of converting jpeg to base64. However, I would suggest a workaround to use a small image-to-base64 node package.
Also, there's a higher chance of something being wrong in the post request, maybe the upload service API is not accepting the base64 format, maybe the API key is not authorized. Please read their API docs carefully and do as it says & also console log the error code you are receiving.
After all, try something like this with Axios.
const image2base64 = require('image-to-base64');
image2base64("path/to/file.jpg") // you can also to use url
.then(
(response) => {
console.log(response); //cGF0aC90by9maWxlLmpwZw==
axios.post('http://example.com/in.php', base64, {
headers: {
"Content-Type": "multipart/form-data"
},
}).then((response) => {
console.log(response)
}).catch(function (error) {
console.log("Error in uploading.");
});
},
}
)
.catch(
(error) => {
console.log(error); //Exepection error....
}
)
I am using microservice architecture. Let say there are two services i.e A and B. I am trying to request from service A to service B which fetch some data from database and give that data as response to service A. But when there is huge amount of data then service B is unable to send in response but it prints on console. I tried many things but none were worked. Please help me on this.
SERVICE A
function makePostRequest(url, data, cb) {
let postContents = {
headers: {
'content-type': 'application/json'
},
url: url,
form: data,
timeout: 1200000
}
console.log('POST ==> ', postContents)
request.post(postContents, function(err, response, body) {
console.log(err)
if (err) {
return cb({
code: httpStatus.serverError,
message: err
})
}
else if (response.statusCode != 200) {
return cb({
code: response.statusCode,
message: body
})
}
else {
console.log(body, 'bodybodybodyPOST')
try {
var data = JSON.parse(body);
if (data && typeof data == 'object')
return cb(null, data)
else
return cb({
code: httpStatus.serverError,
message: 'invalid response'
})
}
catch(Ex) {
console.log(Ex)
return cb({
code: httpStatus.serverError,
message: Ex
})
}
}
})
}
exports.myapi = (req, res) => {
makePostRequest(SERVICE-B-URL, POST-DATA, (e, d) => {
if (e) res.status(500).json({msg: 'please try later'})
else res.status(200).json({msg: 'data fetched', result: d})
})
}
SERVICE B
console.log(result)
console.log('sending response ...', resTotal)
res.status(200).json({
total: resTotal,
result: result,
condition: req.query
});
[ RowDataPacket {
f_stamp: 2019-05-25T05:17:48.000Z,
f_player_id: 33370333,
amount: -0.5,
f_param_notes: null,
f_money_type: 'R',
f_type: 84 },
RowDataPacket {
f_stamp: 2019-05-25T05:14:44.000Z,
f_player_id: 31946955,
amount: 30.9,
f_param_notes: null,
f_money_type: 'R',
f_type: 70 },
RowDataPacket {
f_stamp: 2019-05-25T05:14:41.000Z,
f_player_id: 31035703,
amount: 258,
f_param_notes: null,
f_money_type: 'R',
f_type: 70 },
... 163783 more items ]
sending response .... 163883
A solution is to combine nodejs streams and websockets, here an example:
Streaming data in node.js with ws and websocket-stream
Usually when you have a big amount of data you want to fetch a little portion at time and send it. In the other end you can collect those little pieces and reconstruct the bigger data.
With a single HTTP request, you usually can a 413 error (Payload too large).
In order to avoid this error you should setup the server to accept bigger payloads.
ExpressJS example (more info here: Error: request entity too large)
app.use(express.json({limit: '50mb'}));
Nginx example
server {
client_max_body_size 100M;
...
}
A few months ago i started using Firebase's backend tools. they're awesome. So i had to move our current method of notifying users of their to-do list. We moved to Cloud Functions and it was 40x faster. Until a week ago i got a fresh copy of MacOS i had to delete everything in my Solid State Drive. When i did i also installed the firebase command line tools. I had my code which i used to notify users, it suddenly stopped working
Here's what happens:
When someone adds a new record to our daily list. the app notifies other family members about the new record. So i had to come up with a method to do so..
OLD CODE (worked seamlessly)
const functions = require('firebase-functions');
var admin = require("firebase-admin");
admin.initializeApp();
var registrationToken = 'egV-C3ItwJE:APA91bGf5ezSRQQFHkzCNzfqhS6tiKRbs6IXXs57DFTrNCWRrVY0pZ4PHsK8G3ZjvvvO4JCvd13j_jBcJkgRh06YJ5Jw6tohc81Ro0k4HdHG-Jlv4sbW5t1DNmJBDeGf48l05eDlfMGO';
var payload = {
data: {
title: 'TEST/2',
body: 'TEST/2'
}
};
// registration token.
admin.messaging().sendToDevice(registrationToken, payload)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response, payload);
return
})
.catch((error) => {
console.log('Error sending message:', error);
});
Back in my previous version of macOS this code had no issues. upgrading Firebase Command Line tools i had to make some edits to the code:
NEW CODE (NOT WORKING)
// This registration token comes from the client FCM SDKs.
var registrationToken = 'egV-C3ItwJE:APA91bGf5ezSRQQFHkzCNzfqhS6tiKRbs6IXXs57DFTrNCWRrVY0pZ4PHsK8G3ZjvvvO4JCvd13j_jBcJkgRh06YJ5Jw6tohc81Ro0k4HdHG-Jlv4sbW5t1DNmJBDeGf48l05eDlfMGO';
// See documentation on defining a message payload.
var message = {
data: {
title: '850',
body: '2:45'
},
token: registrationToken
};
// Don't use the legacy sendToDevice
admin.messaging().send(message)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
return
})
.catch((error) => {
console.log('Error sending message:', error);
});
The Problem is:
1- When this function hits. i get the "Successfully sent message:" response. but the message won't reach the device
Things i have tried:
1- The device token is correct and i have tested it using the manual push notification on single devices
2- The device receives notifications.
3- The device runs on iOS 11.1 same as before
Thanks
Can u check with this function,I had tried with this I am able to get notifications.
In you first code sample u had user sendToDevice but in second sample you had used send
function sendPushNotification(fcmtoken, notificationTitle, notificationMessage) {
if (fcmtoken != null && fcmtoken != "" && fcmtoken != " ") {
var payload = {
notification: {
title: notificationTitle,
body: notificationMessage,
sound: "default"
}
};
admin.messaging().sendToDevice(fcmtoken, payload)
.then(function (response) {
}).catch(function (error) {
console.log("Error sending message:", error);
});
}
}
I'm playing around with building a simple Facebook Messenger chatbot and I'm having trouble sending messages in sequence.
In the example above, it should have printed "Hello!", "1", "2", "3" in order. I'm currently following the Facebook docs found here to implement this simple text message function. I've included my Express Node.JS server code below:
Defining the sendTextMessage() function:
var request = require("request");
function sendTextMessage(user, text) {
messageData = {
text: text
};
request({
url: "https://graph.facebook.com/v2.6/me/messages",
qs: {access_token: PAGE_ACCESS_TOKEN},
method: "POST",
json: {
recipient: {id: user},
message: messageData
}
}, function(error, response, body) {
if (error) {
console.log("Error sending message: ", error);
} else if (response.body.error) {
console.log("Error: ", response.body.error);
} else {
console.log("Message successfully send.")
}
});
}
Using it to send a response:
sendTextMessage(user, "Hello!");
sendTextMessage(user, "1");
sendTextMessage(user, "2");
sendTextMessage(user, "3");
I even tried implementing a simple queue that queues messages and only sends one message at a time after each request's success callback. This is making me suspect that I'm not interacting with the Messenger API correctly.
Has anyone encountered this issue? How can I get messages to send in sequence? Thanks!
EDIT
Because I implemented a simple queue but still experiencing this problem, I'm including the code for my simple queue system here.
var queue = [];
var queueProcessing = false;
function queueRequest(request) {
queue.push(request);
if (queueProcessing) {
return;
}
queueProcessing = true;
processQueue();
}
function processQueue() {
if (queue.length == 0) {
queueProcessing = false;
return;
}
var currentRequest = queue.shift();
request(currentRequest, function(error, response, body) {
if (error || response.body.error) {
console.log("Error sending messages!");
}
processQueue();
});
}
queueRequest(/* Message 1 */);
queueRequest(/* Message 2 */);
queueRequest(/* Message 3 */);
UPDATE
This "bug" was reported to Facebook but it sounds like they aren't going to fix it. Please read the ticket thread on Facebook's post here for details on what they say is going on. (Thank you to Louise for getting Facebook's attention on this)
I submitted a bug report to Facebook about this because I was having the same problem. They acknowledged that it is indeed a bug and are working to fix it: https://developers.facebook.com/bugs/565416400306038
After you send a POST to /me/messages, you'll receive a response that has a message id (mine start with 'mid.' which maybe stands for message id?):
{ recipient_id: '1015411228555555',
message_id: 'mid.1464375085492:b9606c00ca33c12345' }
After being completely received by the FB Messenger API, you'll get a call to your webhook (with no message events) that confirms receipt:
{ sender: { id: '1015411228555555' },
recipient: { id: '566031806XXXXXX' },
delivery:
{ mids: [ 'mid.1464375085492:b9606c00ca33c12345' ],
watermark: 1464375085604,
seq: 176 } }
I think that delivery receipt is the best way to ensure delivery, then send the next message.
Implement the send request as a Promise and only send consequent messages once the previous one is resolved
const send = (userId, messageData) => {
return new Promise((resolve, reject) => {
request
(
{
url : BASE_URL + "me/messages",
qs : { access_token : PAGE_ACCESS_TOKEN },
method : "POST",
json :
{
recipient: { id : userId },
message: messageData,
}
}, (error, response, body) =>
{
if (error) { console.log("Error sending message: " + response.error); return reject(response.error); }
else if (response.body.error) { console.log('Response body Error: ' + response.body.error); return reject(response.body.error); }
console.log("Message sent successfully to " + userId);
return resolve(response);
}
);
});
};
You can achieve QUEUING by promises.
function delay(time) {
return new Promise(function(resolve, reject) {
setTimeout(resolve, time);
});
}
delay(2000).then(() => {
console.log('hi');
delay(2000).then(() => {
console.log('hello');
delay(2000).then(() => {
console.log('welcome');
})
})
})
Instead of adding static timeouts, I would create a queue data structure. When the bot wants to send a message, append the contents to the end of the queue. On the message post callback, check if there are any messages still in the queue and call the function again using recursion and remove from the queue accordingly.
They should be received in the order that they are sent. Make sure you're actually sending them in order and not calling an async function 4 times (and send order isn't guaranteed). (I read that you tested it but in all my testing I've never seen a receive come out of order if the send order was guaranteed.)
I added a messageId counter to the app that resets to 0 on every start of messagehandling. Then I delay with that number * 100 ms. This way I can add intentional delays as well with code like messageDelay += 15
receivedMessage(event) {
messageDelay = 0;
//...
sendMessage extend:
function sendTextMessage(recipientId, messageText) {
//...
setTimeout(function() {
callSendAPI(messageData);
}, messageDelay++ * 100)
}
The message is not sending in order because, the request is sent asynchronously to facebook, and can be sent in any order.
To solve this you have to call the next sendTextMessage when the message that should be sent before it has received a response.
Based on the recursive solution proposed by #user3884594, I kind of make it work using this (I removed the error handling in order to simplify):
send_messages (["message 01", "message 02", "message 03"]);
function send_messages (which, i = 0)
{
request({
url: 'https://graph.facebook.com/v2.10/me/messages',
qs: { access_token: FACEBOOK_ACCESS_TOKEN },
method: 'POST',
json: { recipient: { id: senderId }, message: { text: which [i] }
}, (error, response, body) =>
{
// You need to put your error handling logic here
if (i++ < which.length - 1)
send_messages (which, i);
});
}
I had exactly same problem, that solution worked for me:
function sendMessage(recipient, messages, accessToken, i) {
axios.post(baseURL + 'v2.11/me/messages/?access_token=' + accessToken,
Object.assign({}, {
messaging_type: "RESPONSE",
recipient: {
id: recipient
}
}, messages[i]['payload']) )
.then(response => {
if(i < messages.length) sendMessage( recipient, messages, accessToken, i+1 );
},
error => {})
.catch(error => {});
}
sendMessage(recipient, flow['messages'], flow['page']['accessToken'], 0);
That's my question: Sequential Message Sending Using Facebook Send-API
You can try putting them inside a setTimeout function so each one goes after a certain period of time.
So replace this:
sendTextMessage(user, "Hello!");
sendTextMessage(user, "1");
sendTextMessage(user, "2");
sendTextMessage(user, "3");
With this:
sendTextMessage(user, "Hello!");
// 1 second
setTimeout(function() {
sendTextMessage(user, "1");
}, 1000)
// 2 seconds
setTimeout(function() {
sendTextMessage(user, "2");
}, 2000)
// 3 seconds
setTimeout(function() {
sendTextMessage(user, "3");
}, 3000)
And they should go one after another. You could also embed the functions inside each other if need be.
I've got the following code that can send push notifications using Azure Notification Hubs. When a new item is inserted into the database, this code sends a push notification to the devices registered with the tag.
I'm using Ionic/Phonegap for the iOS app and the ngCordova Push Plugin. I want to add badge counts for iOS devices, but I can't seem to find a way to do this. I've tried using the push.apns.send function, but can't get it to work.
Azure Mobile Services
function insert(item, user, request) {
// Execute the request and send notifications.
request.execute({
success: function() {
// Create a template-based payload.
var payload = '{ "message" : "This is my message" }';
push.send("My Tag", payload, {
success: function(pushResponse){
// Send the default response.
request.respond();
},
error: function (pushResponse) {
console.log("Error Sending push:", pushResponse);
// Send the an error response.
request.respond(500, { error: pushResponse });
}
});
}
});
}
Phonegap
var iosConfig = {
"badge": true,
"sound": true,
"alert": true
};
$cordovaPush.register(iosConfig).then(function (deviceToken) {
var hub = new NotificationHub(mobileClient);
// This is a template registration.
var template = "{\"aps\":{\"alert\":\"$(message)\"}}";
// Register for notifications.
// (deviceId, ["tag1","tag2"], templateName, templateBody, expiration)
hub.apns.register(deviceToken, myTags, "myTemplate", template, null).done(function () {
// Registered with hub!
}).fail(function (error) {
alert("Failed registering with hub: " + error);
});
}, function (err) {
alert("Registration error: " + err)
});
I've searched through dozens of articles/tutorials and none of them work. Any help would be greatly appreciated.
I finally figured it out. The issue was that the template registration needed to include the badge. Here's what works:
Azure Mobile Services
function insert(item, user, request) {
// Execute the request and send notifications.
request.execute({
success: function() {
// Create a template-based payload.
var payload = '{ "message" : "' + originalMessage + '", "badge" : "100" }';
push.send("My Tag", payload, {
success: function(pushResponse){
// Send the default response.
request.respond();
},
error: function (pushResponse) {
console.log("Error Sending push:", pushResponse);
// Send the an error response.
request.respond(500, { error: pushResponse });
}
});
}
});
}
Phonegap
var iosConfig = {
"badge": true,
"sound": true,
"alert": true
};
$cordovaPush.register(iosConfig).then(function (deviceToken) {
var hub = new NotificationHub(mobileClient);
// This is a template registration.
var template = "{\"aps\":{\"alert\":\"$(message)\",\"badge\":\"#(badge)\" }}";
// Register for notifications.
// (deviceId, ["tag1","tag2"], templateName, templateBody, expiration)
hub.apns.register(deviceToken, myTags, "myTemplate", template, null).done(function () {
// Registered with hub!
}).fail(function (error) {
alert("Failed registering with hub: " + error);
});
}, function (err) {
alert("Registration error: " + err)
});