Twitter Accounts Activity API Client Library for Python
- ๐ Performs CRC validation
- ๐ Registers webhooks
- ๐ Subscribes to current user's context
- ๐ง Gets activities of the user in real time.
Refer to Twitter's getting started guide on how to create an application. Then set up dev environment label for the application.
App
โก๏ธ Details
โก๏ธ Keys and Tokens
Add the credentials to the environment variables.
~$ export consumer_key=API_KEY
~$ export consumer_secret=API_SECRET_KEY
~$ export access_token=ACCESS_TOKEN
~$ export access_token_secret=ACCESS_TOKEN_SECRET
~$ export env_name=APP_ENV_NAME
Install & run ngrok.
~$ ./ngrok http 5000
# stream_events.py
>>> from twitivity import Event
>>> import json
>>> class StreamEvent(Event):
CALLBACK_URL: str = "https://yourdomain.com/listener"
def on_data(self, data: json) -> None:
# process data
>>> stream_events = StreamEvent()
>>> stream_events.listen()
To register the callback URL run the program above alongside the one below. This will register your webhook URL and subscribe to activities. This only has to be setup once.
# configure.py
from twitivity import Activity
>>> account_activity = Activity()
>>> account_activity.register_webhook("https://youdomain.com/listener")
>>> account_activity.subscribe()
The registration will return json response
{
'id': '1198870971131686912', # webhook id
'url': 'https://1f4396a1.ngrok.io/twitter/callback',
'valid': True,
'created_timestamp': '2019-11-25 07:48:08 +0000'
}
~$ pip install twitivity