Learn how you can become a Python programmer in just 12 weeks.

    We respect your privacy. Unsubscribe at anytime.

    Python Twitter Bot to Follow Followers – 3 Easy Steps

    What will we cover in this tutorial?

    • To build a Bot to Follow Followers in Twitter using Python
    • Link to how you can get your access tokens and consumer keys to get access to the Twitter API (needed)
    • How to access the Twitter API
    • Finally, full code example of a Python Twitter Bot to follow the followers your account does not follow already.

    Step 1: Setup up environment

    In order to get a connection to twitter you need to have access tokens and consumer keys. If you don’t already have that, or you do not know what it is, then I recommend you follow this tutorial.

    You also need the tweepy library. You can install it by typing the following command in the command line or see here for more details.

    pip install tweepy
    

    Then you are ready to connect to the Twitter API.

    Step 2: Connecting to Twitter API

    The first thing your code should do is to connect to the Twitter API and return the tweepy api to your program.

    import tweepy
    def get_twitter_api():
        # personal details
        consumer_key = "__USE YOUR KEY HERE__"
        consumer_secret = "__USE YOUR KEY HERE__"
        access_token = "__USE YOUR KEY HERE__"
        access_token_secret = "__USE YOUR KEY HERE__"
        # authentication of consumer key and secret
        auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
        # authentication of access token and secret
        auth.set_access_token(access_token, access_token_secret)
        api = tweepy.API(auth, wait_on_rate_limit=True)
        return api
    

    This code will authenticate and return the tweepy api.

    Step 3: List followers and friends to follow back

    Confused by the headline? Me, too. But here is where the magic happens.

    The code simply explained.

    • Retrieves all the users that follow you (followers)
    • Retrieves those you follow (friends)
    • Loops through followers and check if you follow them.
      • If not, follow them back
    def process():
        api = get_twitter_api()
        followers = api.followers_ids(api.me().id)
        print("Followers", len(followers))
        friends = api.friends_ids(api.me().id)
        print("You follow:", len(friends))
        for follower in followers:
            if follower not in friends:
                api.create_friendship(follower)
    

    Full code example here

    You can see the full code here.

    import tweepy
    
    def get_twitter_api():
        # personal details
        consumer_key = "__USE YOUR KEY HERE__"
        consumer_secret = "__USE YOUR KEY HERE__"
        access_token = "__USE YOUR KEY HERE__"
        access_token_secret = "__USE YOUR KEY HERE__"
        # authentication of consumer key and secret
        auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
        # authentication of access token and secret
        auth.set_access_token(access_token, access_token_secret)
        api = tweepy.API(auth, wait_on_rate_limit=True)
        return api
    
    def process():
        api = get_twitter_api()
        followers = api.followers_ids(api.me().id)
        print("Followers", len(followers))
        friends = api.friends_ids(api.me().id)
        print("You follow:", len(friends))
        for follower in followers:
            if follower not in friends:
                api.create_friendship(follower)
    
    if __name__ == "__main__":
        process()
    

    Next steps

    • Deploy it to an cron job so it runs every hour.
    • You can use PythonAnywhere (not sponsored by them)

    Python for Finance: Unlock Financial Freedom and Build Your Dream Life

    Discover the key to financial freedom and secure your dream life with Python for Finance!

    Say goodbye to financial anxiety and embrace a future filled with confidence and success. If you’re tired of struggling to pay bills and longing for a life of leisure, it’s time to take action.

    Imagine breaking free from that dead-end job and opening doors to endless opportunities. With Python for Finance, you can acquire the invaluable skill of financial analysis that will revolutionize your life.

    Make informed investment decisions, unlock the secrets of business financial performance, and maximize your money like never before. Gain the knowledge sought after by companies worldwide and become an indispensable asset in today’s competitive market.

    Don’t let your dreams slip away. Master Python for Finance and pave your way to a profitable and fulfilling career. Start building the future you deserve today!

    Python for Finance a 21 hours course that teaches investing with Python.

    Learn pandas, NumPy, Matplotlib for Financial Analysis & learn how to Automate Value Investing.

    “Excellent course for anyone trying to learn coding and investing.” – Lorenzo B.

    Leave a Comment