A minimum example for how to run a Python Twitter bot to tweet automatically.
- Get a Twitter account
- Go to Twitter Dev Dashboard
- Create an App and enter the App name (bot name)
- Save the
API Key
andAPI Secret Key
to somewhere safe - Click
App Settings
- Click
Keys and Tokens
tab - Click the
Generate
button inAccess Token and Secret
section - Save the
Access token
andAccess token secret
to somewhere safe
- Clone the twitter-bot-example
$ git clone [email protected]:shc261392/twitter-bot-example.git
$ cd twitter-bot-example
- Create your
.env
file by
$ cp .env.example .env
- Fill the
.env
file with your saved keys. For example, if yourAPI key
is111111
, yourAPI secret key
is222222
, yourAccess token
is333333
, yourAccess token secret
is444444
, it should look like this:
CONSUMER_KEY=111111
CONSUMER_SECRET=222222
ACCESS_TOKEN=333333
ACCESS_TOKEN_SECRET=444444
- Create and activate a virtual environment
$ python3 -m venv venv
$ source venv/bin/activate
- Install dependencies and install the package in editable mode
(venv)$ python3 -m pip install -e .
- You can now try to tweet with the bot
Tweet (text only)
(venv)$ simple_tweet -t 'This is the content of tweet'
Tweet (text and image)
(venv)$ simple_tweet -t 'This is a tweet with image' -m https://d1e00ek4ebabms.cloudfront.net/production/846fa298-c016-465f-b73e-0591df79ec98.png
Random tweet chosen from data/tweet_contents.json
(venv)$ random_tweet
Random tweet chosen from data/tweet_contents.json
, scheduled to tweet on all times listed in data/schedule.txt
(venv)$ scheduled_random_tweet