Write a Twitter Bot with Python and Tweepy To Tweet a Message to a List of Users

Write a Twitter Bot with Python and Tweepy To Tweet a Message to a List of Users

Sometimes you want to reach out to people who aren’t following you on Twitter. You could do this by sending one tweet at a time. Or you could automate the process and do it in bulk. In this tutorial, you will write a Twitter Bot using the excellent Tweepy library to send a message to a list of users.

First, start a new application at http://dev.twitter.com

After that, create a file, keys.py, and add the following:

Enter your keys and tokens from Twitter. Then create a file, nudge.py, and add the following:

Next, create a file, handles.txt, and add the list of Twitter user names you want to message:

Run this script from the command line by passing three arguments to the Python interpreter. The first argument is the file itself, nudge.py. The second argument is our list of user names, handles.txt. And our third argument is the message we want to send to everyone on the list.

Go see for yourself. Then try it again with a different message.

Let’s walk through it. First is our imports: tweepy , of course, but we need  sys  for the arguments we are passing, time  for the nap we want our for loop to take in-between iterations, and randint to generate the length of said nap. Finally, we import our keys from our keys.py module:

The next few lines are straightforward, we create CONSTANTS with our keys and connect to the Twitter API.

Next we read our handles.txt file:

As you will recall, our file, handles.txt, was the second argument in our Python interpreter command. We reference it here with sys.srgv[1] (1 because Python begins its count at 0, and our first argument was nudge.py), and then assign it to the variable handles .  We next create a new variable, f , that we use to open handles . The  "r"  parameter tells the open function that we want to read the file. In the following line, we read the lines in  f  and store them in a new variable, h . Finally, we close our file.

Now the fun part.

Using a for loop, we iterate over all of the user names stored in h . We want to strip off unnecessary newline characters and white space from each user name string. We do that using the  rstrip()  method. Then we reassign the variable back to itself. Next we concatenate our message by adding our user name string to a blank space, " " , and adding that to our message, which is sys.argv[2] . Next, we send our message to Twitter with api.update_status(m) . And lastly, we rest. We generate a random integer between 1 and 60 and pass that to  time.sleep() to pause the program for that many seconds. We do this so our robot will appear less robotic to anyone watching.

Now you’re ready to sing your praises to the world with your new Twitter Bot.

About the Author

Write a Twitter Bot with Python and Tweepy To Tweet a Message to a List of Users 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
  • This is incredibly useful!!! Thanks a lot!
    I’m a newbie at this and I’m planning to implement this bot as a starter project.
    Do you think it’s possible to get the list of users from a twitter List through the API instead of a txt file? It would be much more useful and easy to manage.

  • S.Kumar

    Nice Post, But somehow my account was suspended. twitter doesn’t allow mentioning user.

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
×