Skip to main content

Create a Twitter Bot to do the work for you using Tweepy

 I googled how to create a Twitter bot and was brought to a cleanly laid out web app. It allowed you to create a bot that would like, follow, or retweet a tweet based on a keyword. The problem was that you could only create one bot for one function.
So I decided to code a bot myself with Python and the Tweepy library.

Setup

First, I downloaded Tweepy. You can do this using the pip package manager.
pip install tweepy
You can also clone the GitHub repository if you do not have pip installed.
git clone https://github.com/tweepy/tweepy.git
cd tweepy
python setup.py install
You’ll need to import Tweepy and Tkinter (for the GUI interface).
import tweepy
import Tkinter

Credentials

Next, we need to link our Twitter account to our Python script. Go to apps.twitter.com and sign in with your account. Create a Twitter application and generate a Consumer Key, Consumer Secret, Access Token, and Access Token Secret. Now you are ready to begin!
Under your import statements store your credentials within variables and then use the second block of code to authenticate your account with tweepy.
consumer_key = 'consumer key'
consumer_secret = 'consumer secrets'
access_token = 'access token'
access_token_secret = 'access token secret'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
In order to check if your program is working you could add:
user = api.me()
print (user.name)
This should return the name of your Twitter account in the console.

Building the Bot

This bot is meant to:
  1. Follow everyone following you.
  2. Favorite and Retweet a Tweet based on keywords.
  3. Reply to a user based on a keyword.
Step one is the easiest, you simply loop through your followers and then follow each one.
for follower in tweepy.Cursor(api.followers).items():
    follower.follow()
    print ("Followed everyone that is following " + user.name)
At this point in order to make sure your code is working you should log onto Twitter and watch as the people you’re following increase.
From this point onwards, besides setting up and packing the labels in the GUI, I am coding everything under the function mainFunction().
def mainFunction():
    #The code
You might be able to see where this is going. In order to favorite or retweet a tweet we can use a for loop and a try statement like this:
search = "Keyword"
numberOfTweets = "Number of tweets you wish to interact with"
for tweet in tweepy.Cursor(api.search, search).items(numberOfTweets):
    try:
        tweet.retweet()
        print('Retweeted the tweet')
    except tweepy.TweepError as e:
        print(e.reason)
    except StopIteration:
        break
In order to favorite a tweet you can simply replace the
tweet.retweet()
with
tweet.favorite()
In order to reply to a user based on a keyword, we need to store the users username and twitter ID.
tweetId = tweet.user.id
username = tweet.user.screen_name
We can then loop through the tweets and update our status (tweet) at each user.
phrase = "What you would like your response tweet to say"
for tweet in tweepy.Cursor(api.search, search).items(numberOfTweets):
            try:
                tweetId = tweet.user.id
                username = tweet.user.screen_name
                api.update_status("@" + username + " " + phrase, in_reply_to_status_id = tweetId)
                print ("Replied with " + phrase)
            
           except tweepy.TweepError as e:
                print(e.reason)
           except StopIteration:
                break
If you want to only utilize the script through the terminal and update the code every time you wish to run it then you have completed your bot.

Creating the GUI

We can create a GUI application that will take our inputs of the keyword you would like to search for and whether or not you would like to favorite a tweet.
We first need to initialize Tkinter and setup the layout.
To create our user interface, we are going to have seven labels for search, number of tweets, and reply. Plus the questions do you want to reply, favorite, retweet the tweet, and follow the user.
Remember the code below is outside and above our mainFunction().
root = Tk()
label1 = Label( root, text="Search")
E1 = Entry(root, bd =5)
label2 = Label( root, text="Number of Tweets")
E2 = Entry(root, bd =5)
label3 = Label( root, text="Response")
E3 = Entry(root, bd =5)
label4 = Label( root, text="Reply?")
E4 = Entry(root, bd =5)
label5 = Label( root, text="Retweet?")
E5 = Entry(root, bd =5)
label6 = Label( root, text="Favorite?")
E6 = Entry(root, bd =5)
label7 = Label( root, text="Follow?")
E7 = Entry(root, bd =5)
We also need to pack each label so that they show up and then call the root function in a loop so that it remains on the screen and doesn’t immediately close.
The following is what packing the first label looks like. I packed all of the labels below the mainFunction().
label1.pack()
E1.pack()
root.mainloop()
If you only ran your GUI code, it should look something like this:



However, inputing text into the labels or clicking the submit button will do nothing at this point. As the interface is not yet connected to the code.
In order to store the user input in the labels, we need to use the .get()function. I used individual functions for each label.
def getE1():
    return E1.get()
Then in my mainFunction(), I called the function getE1() and stored the input into a variable. For E1 it looks like this:
getE1()
search = getE1()
You must do this for every label. For the numberOfTweets label make sure to convert the input into an integer.
getE2()
numberOfTweets = getE2()
numberOfTweets = int(numberOfTweets)
For the last four labels (Reply, Favorite, Retweet and Follow), we need to check to see if the input from the user is “yes” or “no” in order to run that given function or not. This can be accomplished through if statements.
This would be the code for the reply function:
if reply == "yes":
    for tweet in tweepy.Cursor(api.search,     search).items(numberOfTweets):
            try:
                tweetId = tweet.user.id
                username = tweet.user.screen_name
                api.update_status("@" + username + " " + phrase, in_reply_to_status_id = tweetId)
                print ("Replied with " + phrase)
            
           except tweepy.TweepError as e:
                print(e.reason)
except StopIteration:
                break
For the favorite, retweet and follow functions simply replace the reply with “retweet”, “favorite” and “follow”. Then copy and paste the code you wrote above for each one underneath the if statement.
Now we just need to add the submit button and tell it to call the mainFunction() and execute the code for our Twitter Bot. Again, don’t forget to pack it!
submit = Button(root, text ="Submit", command = mainFunction)
That’s it! After you run your bot script, a GUI application should run and you will be able to reply, retweet, favorite and follow users.

Here is the full source code on Github.

Comments

Popular posts from this blog

Introduction

Hi.I am Ajay and i'm 18 years old and you might be wondering 'why am i wasting my time reading some crap written by an eighteen year old ?' but before you make up your mind let me just tell you one thing,you're AWESOME.Why? Because you were probably thinking what i said earlier cause you had something else to do right? Like watch that new movie you waited forever with your u friends or maybe you wanna text your crush how good she was on stage singing that beautiful song that instantly made your heart explode into a million pieces or even better going on a date with her to that ice cream shop by the lake she loves where the only thing you wanna do is to stare into those beautiful eyes for as long as possible or to look up what kind of sugarless cake to buy for your grandma for her 80th birthday next week from that excellent cake shop in town.Whatever it was you had in mind there was something you had to do right? Yes all of us have to do a THOUSAND things everyday and i...

HOW TO INSTANTLY MAKE YOUR LIFE MORE AWESOME THAN BEFORE

1.Smile Please...                  Ever wondered what a simple smile costs you?Nothing not even a single penny.Who doesn't love people that smile often even being with them  improves our mood a lot.Apart from the fact that a silly grin releases pleasure and anti-depressant hormones ,it acts as a greeting card to whomever approaches you.Even 'You-Know-who' looks less evil when he's smiling.  Like who forgets a date where he/she you've been with smiles and laughs often compared to one where she kept complaining about how horrible her exs were.Make no mistake i'm not telling you to laugh like a mad person all the time that would be weird and stupid,rather just smile a bit more than you've used to till now and see the world changing around you. 2.Work,Work,Workout.     Yes,you heard me right;push-ups,pull-ups,do some weights and for those senoritas reading this i'm pretty sure you can workout too. It ...

How i discovered myself amid the abyss of life

Hey guys ,It's Ajay here and i'm so sorry for not writing more often.So lemme start; Just like the title suggests ,i found what i am truly passionate about ,the map that leads to the treasure cave and more than anything showed me my purpose here on Earth.As a deist i always felt like my life was worthless ,like it had no purpose other than just living for the sake of it.I constantly asked myself what i was doing here ,do i have a role to play in the drama that is life itself?!? Many a times i used to feel so depressed and far out of bounds of any help. I USED TO FEEL MISPLACED . If it wasn't for my lovely friends and most importantly my caring family i would've fell deeper and deeper into that abyss .I searched and searched and searched .I tried out so many different things but none of em clicked with me and i stayed THE SAME . BUT ,I DIDN'T LOSE HOPE . I kept pushing myself even when i felt like a total waste of precious resources . I WASN'T READY TO GIVE ...