如何检查流.为特定用户读取的数据



所以我已经编写了一个twitter bot来回应一个特定的人用一个特定的消息假设这是一个原始的推文

代码如下:(id隐藏)


import { TwitterApi } from 'twitter-api-v2';
import { TwitterV2IncludesHelper } from 'twitter-api-v2';
var xdsds = ""
const client = new TwitterApi({
appKey: 'censored',
appSecret: 'censored',
accessToken: 'censored',
accessSecret: 'censored',
});
async function loop(){
const stream = await client.v2.userTimeline('censored',{
'tweet.fields': ['referenced_tweets', 'author_id'],
expansions: ['referenced_tweets.id', 'author_id'],
});
for await (const tweet of stream) {
if(stream.data.author_id == 'censored'){
console.log('you found a yeeyeeass mayank tweet')
}
xdsds = stream.data;
console.log(xdsds)
}
}
loop();

我现在的主要问题是,我不能根据作者id对事情进行排序,因为它不起作用。什么好主意吗?我怎么能阻止它回复同一条推文两次/回复回复而不是回复原始推文

重申一下,您希望回复某个特定用户发布的任何tweet,这既不是回复,也不是转发。为了方便回答,我假设client.v2.userTimeline调用和stream.data.author_id比较中'censored'的值是相同的。

您的条件没有检查tweet的author_id值,您正在访问stream.data.author_id,但您应该检查:tweet.author_id == 'censored'

然而,可能有更好的解决方案。查看用户时间轴API文档,有exclude参数。如果您排除了retweetsreplies,您不需要完全检查author_id,您将不会收到任何回复:

const stream = await client.v2.userTimeline('censored', {
exclude: 'retweets,replies'
});

为了避免两次响应相同的tweet,使用since_idstart_time参数只接收自上次请求以来发布的tweet。您可以将该值持久化到磁盘或数据库中,以便在脚本运行之间保留该值。

例如(未经测试):

let lastTweetSince = undefined;
async function loop(){
const stream = await client.v2.userTimeline('censored',{
exclude: 'retweets,replies',
since_id: lastTweetSince,
});
// Get the ID of the newest tweet in the response
lastTweetSince = stream.tweets[0].id;

// ...
}

(参见分页器文档)

最新更新