问题
This twitter script can currently tweet the selected user once they tweet, but instead of actually replying underneath the tweet, it tweets it as a standalone new tweet. How can I get it to actually reply instead of make a new tweet? I'm using Twit API: https://github.com/ttezel/twit
Here's what I have:
console.log('The bot is starting');
var Twit = require('twit');
var config = require('./config');
var T = new Twit(config);
//Setting up a user stream
var stream = T.stream('user');
stream.on('tweet', tweetEvent);
function tweetEvent(eventMsg) {
var replyto = eventMsg.user.screen_name;
var text = eventMsg.text;
var from = eventMsg.user.screen_name;
console.log(replyto + ' '+ from);
if (replyto =='other user's handle') {
var newtweet = '@' + from + ' Hello!';
tweetIt(newtweet);
}
}
function tweetIt(txt) {
var tweet = {
status: txt
}
T.post('statuses/update', tweet, tweeted);
function tweeted(err, data, response) {
if (err) {
console.log("Something went wrong!");
} else {
console.log("It worked!");
}
}
}
回答1:
In order for a reply to show up in the tweet conversation using the Twitter API, you need the following:
// the status update or tweet ID in which we will reply
var nameID = tweet.id_str;
Also needed is the parameter in_reply_to_status_id
in the tweet
status. See the updates to your code below and it should now preserve the conversation:
console.log('The bot is starting');
var Twit = require('twit');
var config = require('./config');
var T = new Twit(config);
//Setting up a user stream
var stream = T.stream('user');
stream.on('tweet', tweetEvent);
function tweetEvent(tweet) {
var reply_to = tweet.in_reply_to_screen_name;
var text = tweet.text;
var from = tweet.user.screen_name;
var nameID = tweet.id_str;
// params just to see what is going on with the tweets
var params = {reply_to, text, from, nameID};
console.log(params);
if (reply_to === 'YOUR_USERNAME') {
var new_tweet = '@' + from + ' Hello!';
var tweet = {
status: new_tweet,
in_reply_to_status_id: nameID
}
T.post('statuses/update', tweet, tweeted);
function tweeted(err, data, response) {
if (err) {
console.log("Something went wrong!");
} else {
console.log("It worked!");
}
}
}
}
来源:https://stackoverflow.com/questions/39008784/node-reply-script-for-twitter-does-not-actually-reply