tweepy を使用して、twitter のストリーミング API からツイートをプルしています。次に、これを使用してそのユーザーに自動返信します。
たとえば、ライブ ツイートをプルしてからドナルド トランプに返信したい場合は、次のようにします。
import tweepy
from tweepy import Stream
from tweepy.streaming import StreamListener
import json
class StdOutListener(StreamListener):
def on_data(self, data):
clean_data = json.loads(data)
tweetId = clean_data["id"]
tweet = "YOUR MESSAGE HERE"
respondToTweet(tweet, tweetId)
def setUpAuth():
auth = tweepy.OAuthHandler("consumer_token", "consumer_secret")
auth.set_access_token("access_token", "Access_token_secret")
api = tweepy.API(auth)
return api, auth
def followStream():
api, auth = setUpAuth()
listener = StdOutListener()
stream = Stream(auth, listener)
stream.filter(follow=["25073877"], is_async=True)
def respondToTweet(tweet, tweetId):
api, auth = setUpAuth()
api.update_status(tweet, in_reply_to_status_id=tweetId, auto_populate_reply_metadata=True)
if __name__ == "__main__":
followStream()
上記のコードを実行すると、Donald Trump に返信するだけでなく、彼のツイートへのすべての新しい返信にも返信することがわかります。
彼のツイートへの返信をストリームから除外するには、何を追加する必要がありますか?
助けてくれてありがとう。