A Python Twitter bot that runs on AWS Lambda (or locally)
GPL-3.0 License
A Twitter bot written in Python that runs on AWS Lambda. The bot has a number of functions, called "modes", that can do the following actions:
Other features:
You will need to create a Twitter developer account and create a twitter application.
Example bot configuration:
name: my-bot
modes:
reply_to_friends:
function: reply
config:
tweets:
- 100% agree 💙
- I miss you
recipients:
- BoJackHorseman
like_tweets:
function: like
recipients:
- NASAPersevere
- archillect
- horse_ebooks
rate_limit:
type: per_recipient
time: 2d
retweet:
function: retweet
recipients:
- dril
- Coldwar_Steve
rate_limit:
type: per_recipient
time: 3d
reply_to_idiots:
function: reply
config:
tweets:
- Are your parents cousins?
- You are an embarrassment
recipients:
- DonaldJTrumpJr
Configuration of the modes, rate limiting and other settings is in a simple YAML file. It's easiest to just look at the example: terraform/bot-config.yaml.
To create the AWS Lambda function and all associated resources it's easiest to use the provided Terraform code in terraform.
Some environment variables are required for configuration of the bot:
BOT_CONFIG_FILE
: Path to the bot configuration fileSTATE_S3_BUCKET
: The name of the S3 bucket to find/store the bot stateSTATE_S3_KEY
: The name of the key in the S3 bucket to find the state fileAWS_REGION
: The AWS regionTWITTER_CONSUMER_KEY
, TWITTER_CONSUMER_SECRET
, TWITTER_ACCESS_TOKEN
, TWITTER_ACCESS_TOKEN_SECRET
: These come from your Twitter app in the Twitter Developer Portal
Install the required packages:
pip3 install -r requirements.txt
You can run it locally without AWS Lambda like this:
python3 main.py
You can also install it locally:
pip3 install .
Run this:
aws lambda invoke --function-name my-bot-name out --log-type Tail --query 'LogResult' --output text | base64 -d
You can run a test invocation from the AWS Lambda console tab Test
, the payload doesn't matter.
The Terraform code has a enable_schedule
variable to enable a schedule for running the AWS Lambda function.
To run all tests, from root of repo run:
python3 -m unittest discover
Or to run a specific tests:
python3 -m unittest tests.test_bot.TestBot