Categories
Software

Make a Twitter Bot πŸ€– Easily

Twitter bots! It’s all the buzz — like 2 years ago πŸ˜… Anyway, now they’re really easy to make. Let’s do it!

Ready, Set, Go!

First, get python!

Be sure to check the install pip checkbox during installation – that’s how you get python libraries! We’re gonna need at least one, tweepy. To get it, run this from the command prompt (win +r, “cmd”, enter) or bash:

pip install tweepy

Next apply for Twitter Developer Access!

That’s required, as it’s how you’ll get your login keys. You’ll need (4) four!

After creating a “project” (clicking next a lot) go to your projects and grab your “consumer_key” and “consumer_secret” as we’ll call them for simplicity

twitter uses slightly different names, but the order is the same

Next, get your “access_token” and your “access_secret

make sure to put this somewhere safe for later unless you want to have to regenerate!

Do not share these! It’s basically a twitter login. Create a file (like twitterBot.py) with the .py extension and open it in your favorite editor! Ours is vscode πŸ€·β€β™€οΈ

Start your bot script by importing libraries (copying and pasting text into that file) like so:

import tweepy as tw

Next you will need define your login “keys” (leave the quotes!) Add:

consumer_key= 'yourkeyhere'
consumer_secret= 'yourkeyhere'
access_token= 'yourkeyhere'
access_token_secret= 'yourkeyhere'

And then pass that information on to the Twitter API…

auth = tw.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tw.API(auth, wait_on_rate_limit=True)

Got all that added? Save, and test it by opening cmd (or bash on linux 🐧), navigate to where your python script is stored:

cd C:/Users/YOU/Desktop/twitterBot.py

And run it!

python twitterBoy.py

If you didn’t get a big scary error… rejoice! πŸ‘ You have a python bot. Doesn’t do anything yet but hey that’s progress 🎊

Send your first tweet

Let’s make it do something. Add the following:

api.update_status("I'm posting this from python @anicyber thx bro")

Run it just like before… did it error? If not…

Go check your Twitter! It worked!! πŸŽ‰

OK so now we have a bot that can tweet. Let’s make one that searches πŸ”Ž and likes too πŸ’—

Searching Twitter for Tweets

Searching is as easy as posting! Pick some hashtags that you wanna search, and a date from which you want to search from (note: can’t go too far back!)

Also go ahead and remove that last “update_status” line if you don’t wanna try and tweet again (it won’t let you tweet duplicates πŸ˜‰), so your script should now look like this:

import tweepy as tw

consumer_key= 'yourkeyhere'
consumer_secret= 'yourkeyhere'
access_token= 'yourkeyhere'
access_token_secret= 'yourkeyhere'

auth = tw.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tw.API(auth, wait_on_rate_limit=True)

search_words = "#funny"
date_since = "2020-07-10"

If you wanna do multiple just use OR like:

search_words = "#funny OR #memes"
date_since = "2020-07-10"

Then let’s fetch a list of tweets with those #hashtags

tweets = tw.Cursor(api.search,
              q=search_words,
              lang="en",
              since=date_since).items(5)
tweets

Run it and you should see:

<tweepy.cursor.ItemIterator at 0x7facc336f211>

Without nerding out too much πŸ€“ … This is basically a bunch of tweets in “object form”. To properly handle one at a time you can do a loop:

tweets = tw.Cursor(api.search,
              q=search_words,
              lang="en",
              since=date_since).items(5)

for tweet in tweets:
    print(tweet.text)

Run it and you should see something like:

2/2 blah blah
1/2 Obama's cool
RT @anicyber wow  [#horses]
RT @rabbirt stfu
RT @anicyber @rabbirt y'all r bretty cool

Taking out the “trash”

Retweets are like a repost with credit the OP. I prefer not to include those. To filter those our add the following right after: search_words = “#technology OR cyborg”

search_words = search_words + " -filter:retweets"

Run that, and you should get a curated list of tweets!

For now, we just want to work with one at a time, so we’re gonna change the .items(5) to a 1, like so:

tweets = tw.Cursor(api.search,
              q=search_words,
              lang="en",
              since=date_since).items(1)

If you’re confused, your script should look like this:

import tweepy as tw

consumer_key= 'yourkeyhere'
consumer_secret= 'yourkeyhere'
access_token= 'yourkeyhere'
access_token_secret= 'yourkeyhere'

auth = tw.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tw.API(auth, wait_on_rate_limit=True)

search_words = "#funny"
search_words = search_words + " -filter:retweets"
date_since = "2020-07-10"

tweets = tw.Cursor(api.search,
              q=search_words,
              lang="en",
              since=date_since).items(1)

for tweet in tweets:
    print(tweet.text)

Time to interact with ’em. Or it. The tweet.

Liking the tweet

Let’s start by liking it!

The actual liking part is… one line.

api.create_favorite(tweet.id)

To get a nice ‘lil status message to tell you what was liked, change the for loop to:

for tweet in tweets:
    api.create_favorite(tweet.id)
    print("liked: " + tweet.text)

Run it and feel proud of how supportive you’ve just been. Wanna reply too? Fine, but that’s the last one we’re doing in this article!!

Reply AND Like πŸ’‘

Again, really just a one liner…

api.update_status("Lol nice",in_reply_to_status_id = tweet.id, auto_populate_reply_metadata=True)

Add this before or after the like command to do both. Your whole script could look like:

import tweepy as tw

consumer_key= 'yourkeyhere'
consumer_secret= 'yourkeyhere'
access_token= 'yourkeyhere'
access_token_secret= 'yourkeyhere'

auth = tw.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tw.API(auth, wait_on_rate_limit=True)

search_words = "#funny"
search_words = search_words + " -filter:retweets"
date_since = "2020-07-10"

tweets = tw.Cursor(api.search,
              q=search_words,
              lang="en",
              since=date_since).items(1)

for tweet in tweets:
    api.create_favorite(tweet.id)
    api.update_status("Lol nice",in_reply_to_status_id = tweet.id, auto_populate_reply_metadata=True)
    print("liked AND replied to: " + tweet.text)

Run that and you should see this!

liked AND replied to: IS THAT HARASSMENT?...
#Overwatch #ow #edits #Pankake101 #gaming  #twitch #YouTuber #fail #fails #YouTube #games… https://t.co/ZhPSJ6sVpI    

Well, hopefully not that in particular. Anyway, yay πŸŽ‰ Wanna expand? Add some sleep timers, maybe do some NLP?

Start here:

If you liked this then check back for more 😁