How To Reply to Tweets with a Python Twitter Bot using Tweepy

How To Reply to Tweets with a Python Twitter Bot using Tweepy

Why do people use Twitter? Attention. Give them what they want with this simple Python Twitter Bot. We’ll be using Python and the excellent Tweepy library to search for tweets by key word and post a reply. If this is new to you, please see our introductory tutorial, How To Write A Twitter Bot with Python and tweepy.

First, visit, and create a new application.

Now, in your development directory, create a file,, and add the following code:

Replace the ‘x’ fields with your keys and tokens from your newly created Twitter application. Keep the quotation marks, though.

Lastly, create a file,, in the same directory as, and add the following code:

Let’s walk through this.

First, as you will recall from our previous tutorial, we import tweepy. Then, with this line we import our keys dictionary from our module:

We next set our constants for authorization and connect with the API.

Using the search method from the API, we run a query for the string “Hello World!”, and store it in the variable, twt:

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.

Unfortunately, our search query collects these because it features the words “hello” and “world”.  So let’s create a list of specific strings we want to reply to:

Searching for specific strings has the added benefit of disregarding Tweets filled with profanity, which, if our goal is to be professional, we don’t want to reply to.

Now we 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:

From our status object, s, we assign the user’s screen name, user.screen_name, to the variable sn. Next we 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 respond.

About the Author

How To Reply to Tweets with a Python Twitter Bot using Tweepy was posted by on . Jared is one half of the creative force behind Dototot. In addition to writing scripts and tutorials, he draws and animates both the digital and the analog.

Discuss this article in the Comments below, or and it with your friends. Learn more about our creative process by subscribing to the New Moon Newsletter .

Recommended for you
Join the discussion
  • Thanks for the tutorial! Congratulations, this was my Day 1 tutorial for my 180 Days Coding Challenge!

  • Cool! Good luck!

  • DarrenMc

    Thanks for the Tutorial, after some messing about I appear to have it all working…. almost!

    When i run the script from cmd: python

    It runs and pauses for about 5 seconds, then returns to standard command prompt without any errors.

    So I was just wondering exactly this works?

    1 – will it search for the terms only whilst the script runs (so in the 2 second window)

    2 – Does it perform a general search on Twitter or only work on tweets where the bot is mentioned?

    3 – If (1) is true then how to get it to repeat the search?

    or do I have something very wrong?

    Thank you!

  • DarrenMc

    Also one other thing, I am not sure if I found a typo or whether this is what has broken mine somehow.

    However when I tried to run the script the first time I got this error:

    NameError: name ‘twt’ is not defined

    On line 15 of your post above it says:

    twts =”Hello World!”)

    but on the loop it has

    for s in twt:

    with TWT not TWTS I updated line 15 and knocked the S off, which then allowed me to run the script successfully, or appeared to?

  • Russ

    When i try to run the script from cmd using python, i get the message “python: can’t open file ‘’ [Errno2] No such file or directory. Do i have to put my script file somewhere particular? Any help is appreciated, tnx.

  • mansour


    did you solve it out ? I’m having the same issue

  • derpadoo

    it’s a typo in the article. twt should be twts

  • Dangerbeard

    I’ve got this bot up and running, but from what I can tell, it’s only replying to tweets that *start with* my keywords, as opposed to finding that string anywhere in a tweet. Any ideas?

  • Rinzl3r

    I’m getting the same issue, is it because there needs to be another file that activates and repeats this script?

  • Patrick Bobble Off Baranowski

    You need to use CD to where the script is saved by default with python2.7 idel saves in c:/python27/scripts

  • Karlheinz

    I’m getting this error no matter what I do, any idea?

    Failed to send request: data must be a byte string

  • Amy Ireland

    Hey there, I’ve got the same problem as DarrenMc below — the script is running fine, but the bot doesn’t actually seem to be doing anything. I’ve followed it with another account (and reciprocated the follow from the bot account), then deliberately tweeted the designated search term and it still doesn’t react. Anyone figured out how to fix this?

  • Amy Ireland

    Anyone ever figure this out? Struggling with the same problem … everything is working codewise, but the bot is inert.

  • brown linda

    I have never used tweepy ,I used Tweetattackspro ,I know Twitter series Softwares exclusive 30% off Code revealed Copy:”whitehatboxcode30″

  • Anthong Brown

    YOU CAN USE A TWITTER BOT twitter automatic tool, auto-follow, unfollow, follow back, tweet, retweet, reply, favorite, delete tweet, unretweet, unfavorite,

  • Henry Cuandi

    Thank you for the guide. Really useful.
    Result Togel
    Result Togel

Subscribe to our New Moon Newsletter

How do you make money doing what you love? Sign up for monthly emails exploring the necessary evils of business as we develop a creative media studio using free and open source software.

The email entered is invalid