Wednesday Frog Twitter Bot
by Ryan Bogutz
I knew from the start that I wanted to make a project that was silly and meme-related but still allowed me to learn a lot in the process. I've thought that Twitter bots seem cool and would be a fair challenge for me as a first-time hacker.
I followed a tutorial which created a bot that uploads pre-determined tweets to twitter, but I wanted to take things a step further by generating tweets based on some sort of formula. I decided to make the Wednesday Frog Bot because it fits all of the above criteria.
What it does
The bot tweets every one to six hours about the proximity between the current time and the next Wednesday (in EST). That's it. It may not seem like much, but the task proved seemed to be much more daunting than I previously thought.
How I built it
I programmed using Python in Atom on my Windows 10 PC. I used Github for version control and to allow connectivity to Heroku, a cloud-based app hosting service. I also created a Twitter account (@wedsfrogbot) and used Twitter's API to in conjunction with Python's tweepy and pip to bring the bot to life.
Challenges I ran into
My biggest challenge was working alone. I was originally going to create a different project with a friend, but he dropped out due to feeling stressed about homework. I thought about dropping out, too, but I did not want to give up so easliy. When I thought of the idea for this project, I knew I had to do it.
Notably, I had never before programmed a full project in Python before, but have been wanting to do so for quite a while. I had to get used to the wonky syntax of Python (like the fact that it does not use semicolons or brackets) as well as learn how to use some of its less common packages like datetime.
I had a very difficult time installing Python correctly and getting the PATH variables correct. I finally got it to work after realizing I need to use py in command line and not python! Argh!
It also took me a reaaaaaaaaaallyyyyyy long time to figure out how to calculate the number of hours between the time of a tweet and the following Wednesday, like 3+ hours. I had to learn all about the datetime package. Below is the much sought-after code which I finally figured out in order to calculate the time difference:
dayToWeds = [2, 1, 0, 6, 5, 4, 3] #holds days to weds for each day of week currentTime = datetime.datetime.now() #time at tweet currentDay = currentTime.today().weekday() #current day of week nextWeds = currentTime + datetime.timedelta(days=dayToWeds[currentDay], hours=0-currentTime.time().hour, minutes=0-currentTime.time().minute, seconds=0-currentTime.time().second) #date of next wednesday td = (nextWeds-currentTime) #difference between now and wednesday hoursBetween = round(td.days*24 + td.seconds/3600) + 5
I also had to figure out how to host the app and to get it to run from the cloud, for which I used Heroku. It took a lot of difficulty and trial-and-error to get Heroku to host the app properly and get it to run; I did not realize I needed to add additional files (in this case requirements.txt and Procfile to tell the Heroku builder about how the app runs and such).
Heroku dashboard for the project
I am also very new to Git and had to overcome a steep learning curve to keep track of my changes properly.
I also received many errors from Twitter's API, from authentication failures to duplicate tweets, I had to do a lot of tweaking to make sure that my code worked properly in conjunction with Twitter's specifications.
Accomplishments that I'm proud of
I consider this whole project an accomplishment. I am most proud that I pushed past my fear of participating in the hackathon and working alone. I encountered many obstacles and did many new things, most notably learning a new programming language and using a cloud hosting service for the first time. I'm proud that this app works and that I learned so much in the process.
What I learned
I learned so much about APIs, hosting apps on the cloud, Git, GitHub, Python, and programming as a whole. I learned that I can use my coding skills to create fun projects as opposed to the more serious ones I create in class. I also learned that I can pick up coding skills more quickly than I had previously thought, especially in the context of Python and Git. Both aforementioned things seemed very daunting to me just a day ago, but now I feel immeasurably more comfortable. Furthermore, if you would have told me a day ago that I would learn to use the cloud to host and run an app, and also learn how to take advantage of Twitter's API, I would not have believed you. But I did all of these things and more! It's 2 a.m. as I write this and I feel more proud than tired, believe it or not.
What's next for Wednesday Frog Bot
Not much is left to be done. If I encounter any bugs in the program or roadblocks with Twitter's sometimes frustrating API or Heroku's hosting, I will try to implement fixes. Perhaps I will add more functionality to the bot in the nearby future, but for now I am proud of what it does.