This is a work-in-progress port of a Twitter webhook server to Python 3.x. Several TODOs are noted in the code, but this should be functional.
Starter app / scripts for consuming events via Account Activity API.
The current functionality when setup includes:
- When subscribed user receives a Direct Message that is 'Hello Bot', will reply with 'Hello World'
- When a Tweet posted from the subscribed account is liked, the user who liked it's Screen Name will be printed
- A Twitter app created on apps.twitter.com
- Python
- requires TwitterAPI Python library >= 2.4.8
- Heroku CLI (optional)
-
Create a Twitter app on apps.twitter.com
-
On the Permissions tab > Access section > enable Read, Write and Access direct messages.
-
On the Keys and Access Tokens tab > Your Access Token section > click Create my access token button.
-
On the Keys and Access Tokens tab, take note of the
consumer key
,consumer secret
,access token
andaccess token secret
.
-
Clone this repository:
git clone https://github.com/rickredsix/twitter-webhook-boilerplate-python.git
-
Create a virtual environment
virtualenv venv
-
Activate virtual environment:
Note that the included venv is Python 2.7 and this current code requires 3.6.x
source venv/bin/activate
-
Install python requirements
pip install -r requirements.txt
-
Define key variables locally using the keys and access tokens noted previously (this is only for local example scripts, replace the text after the =)
export CONSUMER_KEY={INSERT_CONSUMER_KEY} export CONSUMER_SECRET={INSERT_CONSUMER_SECRET} export ACCESS_TOKEN={INSERT_ACCESS_TOKEN} export ACCESS_TOKEN_SECRET={INSERT_ACCESS_TOKEN_SECRET} export ENVNAME={INSERT_TWITTER_DEV_ENV_NAME} export WEBHOOK_URL={WEBHOOK_URL_AFTER_DEPLOYMENT}
-
Deploy app. To deploy to Heroku see "Deploy to Heroku" instructions below.
Take note of your webhook URL. For example:
https://your.app.domain/webhook
-
Create webhook config.
python example-scripts/create-webhook.py
(Take note of returned
webhook_id
). -
Add user subscription.
python example-scripts/subscribe-account.py
Subscription will be created for user the context provided by the access tokens. By default the tokens on the app page are the account that created the app.
-
Init Heroku app.
heroku create
-
Run locally. (This won't do receive the events as you'll have to configure the webhook URL above as the Heroku URL)
heroku local
-
Configure environment variables. Set up required environmental variables, these will be the keys and access tokens again, plus the Twitter ID of the account that is subscribed. You can find this on the app page listed as Owner ID. See Heroku documentation on Configuration and Config Vars.
heroku config:set CONSUMER_KEY={INSERT_CONSUMER_KEY} heroku config:set CONSUMER_SECRET={INSERT_CONSUMER_SECRET} heroku config:set ACCESS_TOKEN={INSERT_ACCESS_TOKEN} heroku config:set ACCESS_TOKEN_SECRET={INSERT_ACCESS_TOKEN_SECRET} heroku config:set CURRENT_USER_ID={INSERT_USER_ID}
-
Deploy to Heroku.
git push heroku master