twitter 的节点回复脚本实际上并没有回复
Posted
技术标签:
【中文标题】twitter 的节点回复脚本实际上并没有回复【英文标题】:Node reply script for twitter does not actually reply 【发布时间】:2016-12-24 19:53:26 【问题描述】:这个推特脚本目前可以在选定用户发推文后发推文,但它不会在推文下方实际回复,而是将其作为独立的新推文发布。我怎样才能让它真正回复而不是发布新的推文?我正在使用 Twit API:https://github.com/ttezel/twit
这是我所拥有的:
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】:为了使用 Twitter API 在推文对话中显示回复,您需要以下内容:
// the status update or tweet ID in which we will reply
var nameID = tweet.id_str;
还需要tweet
状态中的参数in_reply_to_status_id
。请参阅下面的代码更新,它现在应该保留对话:
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!");
【讨论】:
机器人使用新代码启动,但是当其他用户发推文并且机器人应该回复时,我收到一个错误,即 nameID 未定义?以上是关于twitter 的节点回复脚本实际上并没有回复的主要内容,如果未能解决你的问题,请参考以下文章