
Contemplating a Twitter bot that tweets out “Rise and grind” everyday at 6am.
— squacks (@benmansell) January 15, 2021
And so it was. For literally no other reason than to be more insufferable online... and I guess some of the tweets are hype.
Use Case
Supplement my own account with more hustle—from multiple categories—with a growing list of content. This is as simple as it gets.
This type of project has been done many times before. So much, that somebody made a python library to access the Twitter API. What luck! Be sure to check out the docs while you're at it.
Tools
- Twitter account with developer access
- Code editor (VS code)
- Python3 + Pip3 (for installs)
- Tweepy library
- VPS or other cloud hosting platform (Linode, Digital Ocean)
Process
If you are a seasoned developer with a bit of Python under your belt, this is anywhere from a 2 hour to a weekend project. Personally, this took a couple weeks of casual time. The longest part being the developer application process and getting it on a cloud service.
1) Get Your Twitter API Authentication Credentials
You'll need four magic codes to use the Twitter API:
- Consumer key
- Consumer secret
- Access token
- Access secret
Get Developer Access
Head on over to the Twitter developer site. From there you can either login to your own twitter account or one you've made specifically for bot purposes. I opted to use my own.
Fill out the application which includes things like intent, what you will use it for, and notably, if you're interacting with gov't accounts (gotta wonder why that exists). You'll get an email confirmation and then you're good to go.
Create The 'App'
Twitter allows authentication to apps (not accounts), so we need to create an 'app', then get the credentials related to that specific app.
Create Authentication Credentials for App
Head over to your app, to the Keys and tokens tab, then generate. Make a simple 4 line python file like so with your unique (and like 50 character long) strings:
credentials.py
CONSUMER_KEY = 'generated_consumer_key'
CONSUMER_SECRET = 'generated_consumer_secret'
ACCESS_KEY = 'generated_accss_token'
ACCESS_SECRET = 'generated_access_secret'
2) Get Coding
Working With Tweepy
As usual, read the docs. For my case, it's just one API call.
Things that will throw errors with Tweepy right off the bat (and cause our script to exit):
- Sending tweets less than 15 seconds apart
- Sending the same tweet in succession. More on this later.
- Exceeding the character limit
The Content
First off, I'm using a JSON file for this. It's wholly unnecessary and a python dictionary would've been easier and preferable. I thought somewhere down the line a more universal file would be great, so, I went with it.
Anyhow, the stuff I want tweeted out:
hustle_tweets.json
{
"hstl": [
"Rise and grind.",
"Hustle beats talent, when talent doesn't hustle.",
"Hustle in silence and let your success make the noise.",
"Reap what you repeat 💯 💯 💯",
"Dream with your eyes wide open",
"Tomorrow is NOW",
"Be creative, steal creative"
],
"twt": [
"Don't let your memes be dreams.",
"BITCOIN, bitch!",
"HUSTLEBOT v1.1 DEMANDS ORPHAN HEARTS"
],
"hype": [
"Vibin 'n thrivin",
"\n \n1) Cop\n \n 2) Flip\n \n3) Invest",
"Stay fresh in da chase",
"Hype is only real when you share it"
]
}
Why these three separate 'dictionaries'? Random might select the same string twice in a row and exit the script (this is a Tweepy thing). To solve this, we pick a random string, move to the next dict, pick a random string, and so on. Perfect.
One could also pick a random couple of characters, random emoji or even timestamp the tweet to solve this.
Anyhow, here's the code:
hustlebot.py
#!/usr/bin/python3
import random
import tweepy
import datetime
from time import sleep
import json
# run command for local testing
# python3 hustlebot.py
# Import our Twitter credentials from credentials.py
from credentials import *
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
# open hustle_tweets JSON file
myjsonfile = open('hustle_tweets.json', 'r')
jsondata = myjsonfile.read()
# 420 PARSE IT
obj = json.loads(jsondata)
# Set our index to 0
idx = 0
# get a list of all tweet types from JSON file
tCat = list(obj.keys()) #['hstl', 'twt', 'hype']
# Lüp
while (True):
# 6 am to 8am, but Zulu time
if (datetime.datetime.now().hour >= 14 and datetime.datetime.now().hour <= 16):
thiselem = tCat[idx]
idx = (idx + 1) % len(tCat)
hustleTweet = random.choice(obj[thiselem])
# printout for checking
print('🤖 ' + hustleTweet)
# send out the tweet!
api.update_status('🤖 ' + hustleTweet)
# 1hr 45 min.
sleep(1.75 * 3600)
3) Deploy to Server
If you're inclined to run a script like this on your machine 24/7, feel free. Most people, however, will opt to host it externally on some sort of cloud deployment platform for uptime sake.
This will indeed cost you money to the tune of ~5 USD / mo., but the VPS doubles as a VPS... so you can do other cool things with it. I went with LTS Ubuntu.
4) Conclusion
Timing! For whatever reason, maybe something to do with Zulu time or the VPS itself, the bot occasionally tweets at 4pm.
Not the best time to 'rise and grind' non the West Coast. I'll eventually set up a cron job to remedy this instead of doing it with the script.
While this was a fun project and an excellent outlet for my love of HYPE & HUSTLE, there are MANY more practical uses of the Twitter API. Many revolve around growing a following, marketing and data collection.
