This is a simple template you can use to build a twitter bot using Python and an AWS Lambda Function. I used it to create @dereksiversbot. Learn how to make your own here.
Why build a bot this way?
- It's quick and easy
- You have full control over the bot's actions
- It only uses services from AWS free tier (but see limitations first)
To build and use the bot, you'll need to:
- Register for a twitter developer account
- 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
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>
- Make changes in the logic of the bot by modyifing
src/lambda_function.py
- Test your changes locally by running
python entrypoint.py
from the root directory of your project
Once you are happy with your bot:
- 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
- Update your Lambda Layer using
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
- Upload
lambda_function.zip
to your Lambda Function - Add your twitter App keys as environment variables in the Lambda Function
- Add a scheduled trigger to your Lambda Function using EventBridge
Read this before using the bot:
- This is free unless you go crazy with it or use custom events for triggering the Lambda Function. Check the AWS Free Tier if you have any questions. Use it at your own risk!
- Current logic is very simple. The bot will post a random tweet (excluding its last 3 tweets). If you want something more complex, you'll need to add it on your own.
The createlambdalayer.sh
script comes from this repository.