Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node-red-node-twitter retweet support #835

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 19 additions & 39 deletions social/twitter/27-twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ module.exports = function(RED) {
});

node.on("input", function(msg) {
if (msg.hasOwnProperty("payload")) {
if (msg.hasOwnProperty("payload") || msg.hasOwnProperty("retweet")) {
node.status({fill:"blue",shape:"dot",text:"twitter.status.tweeting"});
if (msg.payload.slice(0,2).toLowerCase() === "d ") {
var dm_user;
Expand Down Expand Up @@ -605,6 +605,24 @@ module.exports = function(RED) {
node.error(err,msg);
node.status({fill:"red",shape:"ring",text:"twitter.status.failed"});
})
} else if (msg.retweet) {
node.twitterConfig.post("https://api.twitter.com/1.1/statuses/retweet/" + msg.retweet,{},msg.params || {})
.then(function(result) {
if (result.status === 200) {
node.status({});
} else {
node.status({fill:"red",shape:"ring",text:"twitter.status.failed"});

if ('error' in result.body && typeof result.body.error === 'string') {
node.error(result.body.error,msg);
} else {
node.error(result.body.errors[0].message,msg);
}
}
}).catch(function(err) {
node.status({fill:"red",shape:"ring",text:"twitter.status.failed"});
node.error(err,msg);
})
} else {
if (msg.payload.length > 280) {
msg.payload = msg.payload.slice(0,279);
Expand Down Expand Up @@ -657,44 +675,6 @@ module.exports = function(RED) {
node.status({fill:"red",shape:"ring",text:"twitter.status.failed"});
node.error(err,msg);
});
// if (msg.payload.length > 280) {
// msg.payload = msg.payload.slice(0,279);
// node.warn(RED._("twitter.errors.truncated"));
// }
// if (msg.media && Buffer.isBuffer(msg.media)) {
// var apiUrl = "https://api.twitter.com/1.1/statuses/update_with_media.json";
// var signedUrl = oa.signUrl(apiUrl,credentials.access_token,credentials.access_token_secret,"POST");
// var r = request.post(signedUrl,function(err,httpResponse,body) {
// if (err) {
// node.error(err,msg);
// node.status({fill:"red",shape:"ring",text:"twitter.status.failed"});
// }
// else {
// var response = JSON.parse(body);
// if (response.errors) {
// var errorList = response.errors.map(function(er) { return er.code+": "+er.message }).join(", ");
// node.error(RED._("twitter.errors.sendfail",{error:errorList}),msg);
// node.status({fill:"red",shape:"ring",text:"twitter.status.failed"});
// }
// else {
// node.status({});
// }
// }
// });
// var form = r.form();
// form.append("status",msg.payload);
// form.append("media[]",msg.media,{filename:"image"});
//
// } else {
// if (typeof msg.params === 'undefined') { msg.params = {}; }
// twit.updateStatus(msg.payload, msg.params, function (err, data) {
// if (err) {
// node.status({fill:"red",shape:"ring",text:"twitter.status.failed"});
// node.error(err,msg);
// }
// node.status({});
// });
// }
}
}
});
Expand Down
6 changes: 6 additions & 0 deletions social/twitter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,9 @@ To send a Direct Message (DM) - use a payload like.
If `msg.media` exists and is a Buffer object, this node will treat it as an image and attach it to the tweet.

If `msg.params` exists and is an object of name:value pairs, this node will treat it as parameters for the update request.

#### Retweet

To retweet an existing tweet, set `msg.retweet` to the tweet id. The tweet id can be found in the `id_str` field of the tweet object.

Before retweeting, make sure the tweet is not a retweet itself.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, if you're using the Twitter Input Node with all the #some_hashtag, it will retweet the tweet with the #some_hashtag, and then, it will get it again, as a retweet.

If you will not filter it, it will try to retweet it again, and you will get an error "you already retweeted this".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this check not be done within the node ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you suggest doing that?

We can accept the whole tweet object. If we want to keep it simple with just retweet with the tweet ID, I guess we need to use the Twitter API to first check if the tweet ID has already been retweeted by us. I think it is complicated, and I wanted to start with simpler solution.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don’t know how to do it then advising the user that they should is just passing the buck isn't it ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest changing the wording from:

Before retweeting, make sure the tweet is not a retweet itself.

to

You cannot retweet a retweet. You can check if it is a retweet using the `msg.tweet.XYZ` property.

(assuming these is a property under msg.tweet that does the job)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will continue to work on it next week

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don’t know how to do it then advising the user that they should is just passing the buck isn't it ?

Originally posted by @dceejay in #835 (comment)

Not exactly. I don't know and I didn't research exactly what is the right way to validate we are not retweeting the same tweet twice, so I leave it to the user to implement a custom workaround, based on its use case.

For example, in my use-case, I'm retweeting tweets by hashtag, and I filtering all the tweets from me, so that it will never try to retweet twice, since the second tweet will be always by me.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest changing the wording from:

Before retweeting, make sure the tweet is not a retweet itself.

to

You cannot retweet a retweet. You can check if it is a retweet using the `msg.tweet.XYZ` property.

(assuming these is a property under msg.tweet that does the job)

Originally posted by @knolleary in #835 (comment)

I'm very welcome to documentation improvements, my English is not so good. But about this specific suggestion, please see the previous comment. You can retweet a retweet, AFAIK, but you can't retweet tweet you already retweeted yourself. And I'm not sure how to check this (possibly by checking if one of the retweets of a tweets made by me...)