Building the #VetsWhoCode Retweet Bot
Tags: python, showdev, wecoded, beginners
Published: 2020-01-02 20:07:59

## Why build a retweet bot?
As a newbie to coding, I was also introduced to Twitter, via the [#100DaysOfCode](https://twitter.com/search?q=%23100DaysOfCode&src=typed_query&f=live) tag. From there, I found [#WomenWhoCode](https://twitter.com/search?q=%23WomenWhoCode&src=typed_query&f=live), [#GirlsWhoCode](https://twitter.com/search?q=%23GirlsWhoCode&src=typed_query&f=live), and much more recently [#HelpMeCode](https://twitter.com/HelpmecodeBot) 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](https://twitter.com/search?q=%23VetsWhoCode&src=typed_query&f=live) 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. 
{% twitter 1209991491185238017 %}

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`
```j
Flask==1.0.2
tweepy~=3.8.0
```

`server.py`
```python
from os import environ
from flask import Flask

app =  Flask(__name__)
app.run(host='0.0.0.0', port=environ.get('PORT'))
```

`setup.py`
```python

```

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`. 

{% twitter 1213907287951249410 %}

---

## What Next?
- Get the bot to run 24/7/365 with minimal downtime
- Build tests
- Decide on blocking words or not
{% twitter 1212081750307983361 %}

## The Bot
A link to [the bot on Twitter](https://twitter.com/VetsWhoCodeBot)
{% github http://github.com/VickiLanger/VetsWhoCode-Retweet-Bot no-readme %}