This is a simple template you can use to build a twitter bot that tweets fake lyrics from your favorite musician using web scraping, Markov Chaining, Python and an AWS Lambda Function. I used it to create @dg_unreleased. This is adapted from Dylan Castillo's wonderful tutorial which you should check out here.
To build and use the bot, you'll need a few things:
- Register for a twitter developer account
- Get genius api keys
- Generate genius access token
- Have Docker installed
- Have Python 3.8 or higher installed
- Create a twitter app
- Make sure to give it Read and Write permissions.
- Set up an AWS account
- Create a Lambda Function for your bot
- Create a Lambda Layer to use additional libraries in your Lambda Function
-you can do 8 and 9 later DEFINITELY read thru Dylan's tutorial on his site it does an excellent breakdown of the steps for deployment
To make your own bot follow these steps:
- Clone this repository on your local machine
- Create a virtual environment in your project's root directory:
python3 -m venv venv && source venv/bin/activate
- Install the required libraries using pip:
pip install -r requirements.txt
- Create a file called
.env
in the root directory of your project. Put your twitter App keys there:
ACCESS_TOKEN=<YOUR_ACCESS_TOKEN_HERE>
ACCESS_TOKEN_SECRET=<YOUR_ACCESS_TOKEN_SECRET_HERE>
CONSUMER_KEY=<YOUR_CONSUMER_KEY_HERE>
CONSUMER_SECRET=<YOUR_CONSUMER_SECRET_HERE>
- edit
lyric_scrape.py
adding your genius access token where it says YOUR-TOKEN-HERE - modify
lyric_scrape.py
to get your fave musicians lyrics by changing the artist name and song count on the last line in the file - run
lyric_scrape.py
to produce a txt file with all the scraped lyrics. You will want to get a lot of songs so you have diversity in the lyrics. - once you have the lyrics copy and paste them into the src/lyrics.txt file replacing the lyrics already there.
- Test your changes locally by running
python entrypoint.py
from the root directory of your project
Once you are happy with your bot:
time to look at Dylan's walkthru deployment!
- Add any additional packages you used to
requirements.txt
- Run
sh createlambdalayer.sh
from the root directory of your project. It'll generate a zip file with your libraries calledlayer.zip
this will take a while! - Create a Lambda Layer by uploading the generated
layer.zip
- Run
sh buildpackage.sh
from the root directory of your project. It'll make a zip file with the code for your Lambda Function calledlambda_function.zip
- Create a lambda function by uploading
lambda_function.zip
- Add your twitter App keys as environment variables in the Lambda Function
- Add a scheduled trigger to your Lambda Function using EventBridge
The createlambdalayer.sh
script comes from this repository.