我一直在努力学习如何制作一个Twitter机器人,我做了一个简单的搜索和回复机器人,它搜索每当有人推特我的名字并回复,但我的机器人一直在回复相同的推文一遍又一遍地。我尝试在循环中添加一个自变量,每当我通过所述推文的ID回复推文时,我用self_id =改进了搜索查询,但是没有做任何建议?这是我的代码:
import os
import random
import tweepy
consumer_key = os.environ.get('TWITTER_CONSUMER_KEY')
consumer_secret = os.environ.get('TWITTER_CONSUMER_SECRET')
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
access_token = os.environ.get('TWITTER_ACCESS_TOKEN')
access_token_secret = os.environ.get('TWITTER_ACCESS_TOKEN_SECRET')
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
replies = ["example 1",
"example 2",
"example 3"
]
if __name__ == "__main__":
twitter = TwitterAPI()
since = 0
while True:
tweets = api.search(q="shalash -shalashtein -OsamaAhmadSalas -ShalashteinBot -ghghgu38678638 -athy_shalash",since_id=since) #I added these exclusions because the bot was searching screen names too
for s in tweets:
since = s.id
rand = random.choice(replies)
sn = s.user.screen_name
m = "@%s %s" % (sn,rand)
s = api.update_status(m, s.id)
time.sleep(60)
答案 0 :(得分:0)
将您已回答的推文保存到数据库或文本文件中,然后检查您是否已经回复了它。