简体   繁体   English

Twitter机器人-使用Tweepy回复特定用户

[英]Twitter bot - Replying to specific users using Tweepy

I'm trying to write my first twitter bot, and what I specifically want it to do is to reply when a specific user tweets something. 我正在尝试编写我的第一个Twitter机器人,而我特别希望它做的是在特定用户发布消息时回复。 Instead of searching for every user that mentions the word 'taco' I want to only search for when a small list of people mention 'taco'. 我只想搜索一小部分人提到“ taco”的情况,而不是搜索提到“ taco”一词的每个用户。 For example, my bot only looks for tweets from Bob and Sue that mention 'taco', but not tweets from Fred. 例如,我的机器人仅查找Bob和Sue提到“ taco”的推文,而不查找Fred的推文。 I'm not finding what I need. 我找不到我需要的东西。

From the Tweepy documentation, you should probably use the method API.user_timeline to read a specific user tweets. Tweepy文档中,您可能应该使用API.user_timeline方法读取特定的用户tweet。

API.user_timeline([user_id/screen_name]) API.user_timeline([user_id / screen_name])

Returns the 20 most recent statuses posted from the authenticating user or the user specified . 返回从身份验证用户或指定用户发布的20个最新状态。

Parameters : 参数
user_id – Specifies the ID of the user user_id –指定用户的ID
screen_name – Specifies the screen name of the user screen_name –指定用户的屏幕名称

You could accomplish this through the user timeline API endpoint; 您可以通过用户时间轴 API端点完成此操作 however, depending on how many terms and users you want to track, you'd have to worry about rate limits (the user timeline endpoint rate limit is pretty high, 300/app auth/15 mins, 150/user auth/15 mins = 450/15 mins), and also the fact that you'd have to call the endpoint manually at some time interval. 但是,根据要跟踪的字词和用户数量,您必须担心速率限制(用户时间轴端点速率限制非常高,300 / app auth / 15分钟,150 / user auth / 15分钟= 450/15分钟),以及您必须在某个时间间隔手动调用端点的事实。

Another way to do this is by using the streaming API endpoint, specifically the user stream . 做到这一点的另一种方法是使用流API端点,特别是用户流 Follow everyone you want to reply to, and then create some rules for specific phrases. 跟随您要回复的所有人,然后为特定短语创建一些规则。 As followed users post tweets, they should stream to your user stream endpoint. 用户发布推文后,他们应该流到您的用户流终结点。 You'd just have to have a listener running, with some logic for tracked users/phrases. 您只需要运行一个侦听器,并为跟踪的用户/短语提供一些逻辑即可。

track = {
    'taco': ['Bob', 'Sue'],
    'salsa': ['John', 'Steve'],
    'guacamole': ['Mary', 'Fred']
}

You'd subclass Tweepy's StreamListener : 您可以将Tweepy的StreamListener子类化:

class TacoListener(tweepy.StreamListener):
    def on_status(self, status):
        # Note, I rolled my own Twitter API wrapper, and just had a glance at the Tweepy docs, so some of this syntax might be incorrect, change it as required
        # I think Tweepy has syntax like: status.text; I'll just refer to the fields as they appear in the Twitter JSON payload
        for k, v in track.items():
            if k in status.get('text') and status.get('screen_name') in v:
                tweet = ""
                for name in v:
                    tweet += "@" + name
                tweet += " are talking about " + k + "!  Yum."
                api.update_status(status=tweet)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM