【问题标题】:Python - Tweepy - What do I need to add to make this code reply to an account rather than a keyword?Python - Tweepy - 我需要添加什么才能使此代码回复帐户而不是关键字?
【发布时间】:2020-12-11 10:48:38
【问题描述】:

这是我当前使用的代码,它会立即响应包含任何关键字的任何推文。相反,我想更改此代码以自动响应特定帐户(无论何时发布推文)。我需要添加或删除什么才能做到这一点?这个当前的代码有效,我只是想知道如何改变它的回复。

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 REPLY HERE"
        respondToTweet(tweet, tweetId)

def setUpAuth():
    auth = tweepy.OAuthHandler("consumer_key", "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(track=["KEYWORDS"])

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()

【问题讨论】:

    标签: python twitter tweepy twitterapi-python


    【解决方案1】:

    解决了!

    对于其他想知道这是如何完成的人,这是我所做的:

    我在代码中更改了这一行:

    stream.filter(track=["KEYWORDS"])
    

    到:

    stream.filter(follow=["TWITTER_ID"])
    

    TWITTER_ID = 您要自动回复的用户的推特 ID,可以通过将其句柄插入 tweeterid.com 来找到。

    例如 realdonaldtrump 的 twitter id 是 25073877,因此您可以将 TWITTER_ID 替换为 25073877。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多