How To Reply To Tweets Using Python Twitter Bot

Short Bite: – Today At Python Lovers I Am Going To Show You How You Can Reply To Tweets Using Python Twitter Bot.

I know that you must have done lots of stuff in Python, which are fascinating but trust me responding to tweets using Python is the most interesting thing you can do now. So, In this article, We’ll be using Python and the excellent Tweepy library to search for tweets by keyword and post a reply.

If You’re Beginner In Python, First Go Through Python Beginners Section.

So Let’s Start.

 

  • First of all visit https://dev.twitter.com/ and create a new application.
  • Now, open your development directory and create a new file there named “Keys.py” and paste the following code:-
    #!/usr/bin/env python
    #keys.py
    #visit http://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',
    )
  • Replace the ‘x’ fields with your keys and tokens generated from your new Twitter application. Keep the quotation marks, though.
    Reply To tweets using pythonReply To Tweets using python
  • Create a file “hello.py” in the same directory and add the following code:-
    #!/usr/bin/env python
    import tweepy
    #from our keys module (keys.py), import the keys dictionary
    from keys import keysCONSUMER_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)
  • As in the previous step we import tweepy. So now using this line we will import our keys dictionary from our keys.py module:-
    from keys import keys
  • After that next we will set our constants for authorization and to connect with the API.
    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)
  • Now using the search method from the API, we will run a query for the string “Hello World!”, and store it in the variable, twt:-
    twt = api.search(q="Hello World!")

    The great thing about Twitter is that you can write robots. The bad thing is that other people can write robots. There’s a particularly annoying variety that Tweets something along these lines:


    Hello! Please follow. It would mean the world to me.

  • Searching for specific strings has the benefit of disregarding Tweets which are filled with profanity, which, if our goal is to be professional, we don’t want to reply to.
  • Now we will run our nested for loops, iterating over our list of Tweets first, then over our list of strings, finally checking if our Tweet is equal to our string. If so, we reply:-
    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)
  • From our status object, s, we assign the user’s screen name, user.screen_name, to the variable sn. Next, we will create our message, m. We want the Tweet to begin with the users Twitter handle, so it doesn’t appear in our timeline and annoy our followers. Lastly, we post our status update.When the world says “Hello!”, you can now automatically reply to the tweet.

    Hope You Enjoyed This Article.

    You may also like: – Twitter App With Python Request Libraries