【问题标题】:Twitter bot that replies to all the tweets of my friend. Python回复我朋友所有推文的 Twitter 机器人。 Python
【发布时间】:2021-08-04 17:16:15
【问题描述】:

您好,我正在搜索如何创建一个 Twitter 机器人,它可以在 Python 中回复特定用户的所有推文。 我已经创建了一个开发者帐户,并且是 Python 的初学者。

【问题讨论】:

    标签: python twitter bots


    【解决方案1】:

    首先,访问https://dev.twitter.com,并创建一个新的应用程序。

    领导你的 venv 或 anaconda 并执行

    pip install tweepy
    

    现在,在你的开发目录中,创建一个文件,keys.py,并添加以下代码:

    #!/usr/bin/env python
    #keys.py
    #visit https://dev.twitter.com to create an application and get your keys
    keys = dict(
        consumer_key =          'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
        consumer_secret =       'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
        access_token =          'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
        access_token_secret =   'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    )
    

    用您新创建的 Twitter 应用程序中的密钥和令牌替换“x”字段

    在keys.py所在目录下创建一个文件replybot.py,添加如下代码:

    #!/usr/bin/env python
    import tweepy
    #from our keys module (keys.py), import the keys dictionary
    from keys import keys
    
    CONSUMER_KEY = keys['consumer_key']
    CONSUMER_SECRET = keys['consumer_secret']
    ACCESS_TOKEN = keys['access_token']
    ACCESS_TOKEN_SECRET = keys['access_token_secret']
    
    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
    api = tweepy.API(auth)
    
    twts = api.search(q="Hello World!")
    
    #list of specific strings we want to check for in Tweets
    t = ['Hello world!',
        'Hello World!',
        'Hello World!!!',
        'Hello world!!!',
        'Hello, world!',
        'Hello, World!']
    
    for s in twt:
        for i in t:
            if i == s.text:
                sn = s.user.screen_name
                m = "@%s Hello!" % (sn)
                s = api.update_status(m, s.id)
    

    检查您的 API 是否正常工作。 sleep 是你确保你没有被要求验证你是一个机器人 python .txt

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import tweepy, time, sys
    
    argfile = str(sys.argv[1])
    
    #enter the corresponding information from your Twitter application:
    CONSUMER_KEY = '1234abcd...'#keep the quotes, replace this with your consumer key
    CONSUMER_SECRET = '1234abcd...'#keep the quotes, replace this with your consumer secret key
    ACCESS_KEY = '1234abcd...'#keep the quotes, replace this with your access token
    ACCESS_SECRET = '1234abcd...'#keep the quotes, replace this with your access token secret
    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
    api = tweepy.API(auth)
    
    filename=open(argfile,'r')
    f=filename.readlines()
    filename.close()
    
    for line in f:
        api.update_status(line)
        time.sleep(900)#Tweet every 15 minutes
    

    回复特定推特用户

    toReply = "someonesTwitterName" #user to get most recent tweet
    api = tweepy.API(auth)
    
    #get the most recent tweet from the user
    tweets = api.user_timeline(screen_name = toReply, count=1)
    
    for tweet in tweets:
        api.update_status("@" + toReply + " This is what I'm replying with", in_reply_to_status_id = tweet.id)
    

    你可以编写任何你想要的逻辑

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-12
      • 1970-01-01
      • 2023-01-29
      • 1970-01-01
      • 1970-01-01
      • 2021-03-21
      • 2014-05-29
      相关资源
      最近更新 更多