This project is a Twitter bot build that tells the local weather based on local tweets. The bot pulls Sacramento, CA area tweets containing the word 'weather' from Twitter. Some light text analysis is done to subset out tweets that refer to common weather forecast terms and the current date. Then, a message containing the forecasts (including temperature highs and lows) is generated and posted to the bot's twitter account here.
This project's main script is written in R and uses the following packages:
rtweetwraps the Twitter API in useful and easy to use R functionsdplyrfor tidyverse style data manipulation, e.g.select(),filter(),mutate(), and%>%(piping)tidytextfor text analysis using tidy data principlesstringifor string manipulationpakfor loading the most up to date package ofrtweetfrom a ropensci's github
The python script is used to get the access credentials to a third party Twitter acount, in this case, the bot's account. More about the python script can be found in the Hurdles section. This script uses the modules:
tweepyto handle 3-legged OAuth authorization
The following were the main challenges I ran into during this project:
- At first, my app was in a Project. This meant I could not access the Twitter API v1 required by
rtweet. Thanks to this post I realized the issue and addressed it. - I ran into trouble when trying to authorize the app for the bot account. After trying a few different iterations of a 3-legged OAuth flow to get access to the bot
account's access tokens, I found this python suggestion by
beaugunderson that uses the
tweepypackage. The GetTwitterOauthCredentials.py is the updated version I made to get access credentials for my weather bot.- After the Python solution was implemented, I also found the
create_token()function in thertweetpackage. This would also be good solution, but I opted to use the Python script. Oscar Baruffa has a good description of this implementation here.
- After the Python solution was implemented, I also found the
Prerequisites:
- Twitter Developer account
- Twitter account you'd like your bot to post to (doesn't need to the same account as the Twitter Developer account)
- A way to run a Python 3 script on your machine
- An R script that accesses the Twitter API and does whatever you want your bot to do
- Create a Standalone app using the Twitter Developer Portal with your Twitter Developer account. Note that if you don't create a Standalone app and instead make an app that lives inside a project, you will need to apply for 'Elevated' status for the project. This is because the R package
rtweetuses Twitter API v1 and projects can only us Twitter API v2 unless they are granted 'Elevated' status. - Within the app you just created, edit the authentication settings:
- Set App permissions correctly for your app depending whether R script reads only; reads and writes; or reads, writes, and accesses direct messages
- Set the Callback URI to
http://127.0.0.1 - Set the Website URL to whatever you'd like; I set mine to the bot's Twitter page
- Fork this repository.
- Add your app's API token and secret to your repository as Repository Secrets.
- On your local machine, pull the forked repository.
- Log into the bot's Twitter account via your browser.
- Run the GetTwitterOauthCredentials.py with your app's consumer API token and secret to get the access token and secret for your bot's Twitter account.
- Add the API access token and secret for your bot's Twitter account to your repository as Repository Secrets.
- Lastly, edit your yaml file.
- Edit the
cronelement to set a time based trigger, e.g. have the job run every day at noon UTC.
on:
schedule:
-cron: 0 12 * * *- Additionally, setting
ontoworkflow_dispatchallows you to manually run the workflow from the Actions page of your repository. For more triggers, check the docs. - Edit the
envlist to ensure the workflow uses the correct names of your Repository Secrets. - Edit the
install.packages()call to install thepakpackage and any other packages required for your R script.
- name: Install CRAN packages
run: Rscript -e "install.packages(c('pak', 'YOUR', 'OTHER', 'CRAN', 'PACKAGES', 'HERE'), repos = 'https://cloud.r-project.org/', dependecies = TRUE)"Note that the pak package is required to load the latest stable version of rtweet regardless of what other packages you need from CRAN.