DEV Community

loading...

Building the #VetsWhoCode Retweet Bot

vickilanger profile image Vicki (she/her) Updated on ・3 min read

Why build a retweet bot?

As a newbie to coding, I was also introduced to Twitter, via the #100DaysOfCode tag. From there, I found #WomenWhoCode, #GirlsWhoCode, and much more recently #HelpMeCode all retweet your posts. This really helped me meet new people, learn, and grow from the coding community around me. These hashtags are a great way to promote and support those who use them. With this in mind, I realized I didn't really know of any other veterans in the coding community. So, I started playing with the idea of building a bot to help. I started using #VetsWhoCode a few months back, but that wasn't going to be enough to build a community. So, the planning began.

Requirements

My Stack & Pre-Requisites

Before doing any project you have to know things. You don't have to know everything, but it helps to know the basics.

First off, I needed to know what language I planned to use. As I have solely been learning Python, this was the obvious choice.

From there I needed to know where I was going to save my repo. I chose GitHub over GitLab, based solely on my own familiarity with GitHub.

I also knew I would need Heroku, to do something. I'm still not super sure what it's doing, other than hosting, but it's there. It also caused me a lot of frustration because I had no understanding of how it works or what to do in order to host my bot.

What I Learned

Twitter Stuff Learned

Apply for the dev with the account that will run the bot. This may be obvious to some, but I originally applied with my personal account. The keys you receive with the dev account are only for the account you applied with.

Python Stuff Learned

Using the Python module Tweepy was new to me. It made the script for the bot very concise and short.

I had also never used environment variables. The variables were very frustrating to me. This tweet sums up my thoughts.
Liquid error: internal

I have since learned more about environments and the use of environ

Once I had the script done and pushed to GitHub, I thought I was pretty much done. I was sadly mistaken.

Heroku Stuff Learned

I learned more, dealing with Heroku than I did writing the actual bot.

Every Heroku thing needs a Procfile, which contains something about web and worker.

In order to work, Heroku must identify your language(s). To do this, there are certain files your repo must have in order to be viewed as a [insert your language(s)].

For Python, I required a requirements.txt, server.py, and setup.py. For reference, their contents are below.

requirements.txt

Flask==1.0.2
tweepy~=3.8.0
Enter fullscreen mode Exit fullscreen mode

server.py

from os import environ
from flask import Flask

app =  Flask(__name__)
app.run(host='0.0.0.0', port=environ.get('PORT'))
Enter fullscreen mode Exit fullscreen mode

setup.py


Enter fullscreen mode Exit fullscreen mode

From here, everything was loaded and now the bot should work, right? No. Very wrong. I knew the last thing to the bot was to make sure it kept running. To be honest, it still isn't running constantly and I don't know why. In order to try, I set up the Heroku scheduler add-on to run every 10 minutes. It asked for a command and some googling and guessing led me to $ Heroku run retweet.py. It works, I think. Then again, the tweets aren't always getting retweeted.


Edit: Turns out, Heroku scheduler desired python retweet.py not heroku run retweet.py.

Liquid error: internal


What Next?

  • Get the bot to run 24/7/365 with minimal downtime
  • Build tests
  • Decide on blocking words or not

The Bot

A link to the bot on Twitter

GitHub logo VickiLanger / VetsWhoCode-Retweet-Bot

retweet bot to promote veterans with #VetsWhoCode

Discussion

pic
Editor guide