Dec. 18, 2020, 2:59 a.m.
Twitter bots! Itâs all the buzz â like 2 years ago đ Anyway, now theyâre really easy to make. Letâs do it!
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 đ
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âŚ
OK so now we have a bot that can tweet. Letâs make one that searches đ and likes too đ
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
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.
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!!
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 đ