[英]Using nTwitter to reply to a Tweet with node.js
我使用node.js
和nTwitter
创建了一个非常简单的推特机器人。
它搜索“某个词”并用随机引用回复用户。 我已经设法把所有事情都做对了。 代码运行和流完美。
但是有一个很大的缺点。 当机器人回复一条推文时,它不会在 Twitter 上将其显示为对话。
它所做的只是在推特上发布“特定单词”的推文。 如下图所示,虽然它回复了用户,但这不是对话,用户也没有删除他的推文。
这是我正在谈论的图片:
这是我的代码。 令牌由 auth.js 处理
var ntwitter = require('ntwitter');
var auth = require('../auth');
var bot = new ntwitter(auth);
var callback = function handleError(error) {
if (error) {
console.error('response status:', error.statusCode);
console.error('data:', error.data);
}
};
function startStreaming() {
bot.stream('statuses/filter', { track: 'certain word' }, function(stream) {
console.log('Listening for Tweets...');
stream.on('data', function(tweet) {
if (tweet.text.match(/certain\sword/)) {
bot.updateStatus('@' + tweet.user.screen_name + ' True that' ,
tweet.user.screen_name , callback);
}
});
});
}
startStreaming();
我想问题出在bot.updateStatus()
。
请帮帮我。 提前致谢。
您必须在您的请求中添加一个参数 in_reply_to_status_id(这是您正在回复的用户推文的状态 ID。参考: POST statuses/update
这是一个例子。
function startStreaming() {
bot.stream('statuses/filter', { track: 'certain word' }, function(stream) {
console.log('Listening for Tweets...');
stream.on('data', function(tweet) {
if (tweet.text.match('certain word')) {
bot.updateStatus('@' + tweet.user.screen_name + ' True that' ,
{in_reply_to_status_id: tweet.id_str}, callback);
}
});
});
}
startStreaming();
您必须添加一个参数in_reply_to_status_id
才能开始与 Twitter 用户的对话。
function startStreaming() {
bot.stream('statuses/filter', { track: 'certain word' }, function(stream) {
console.log('Listening for Tweets...');
stream.on('data', function(tweet) {
if (tweet.text.match('certain word')) {
bot.updateStatus('@' + tweet.user.screen_name + ' True that' ,
{in_reply_to_status_id: tweet.id_str}, callback);
}
});
}
startStreaming();
需要注意的是in_reply_to_status_id
如果你不提的是行不通的username
你是谁在回答tweet_text
。 此外,您不能使用相同的tweet_text
,否则会被视为垃圾邮件。
来源: https : //developer.twitter.com/en/docs/twitter-api/v1/tweets/post-and-engage/api-reference/post-statuses-update
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.