This repository contains a bot designed to automatically post content to Threads. The bot operates using a modular structure with asynchronous capabilities, making it efficient and extensible.
- Automatic Post Scheduling: Ensures minimum intervals between posts to avoid spam.
- Dynamic Post Generation: Generates batches of posts based on a role description.
- Customizable Post Frequency: Specify the frequency of posts in seconds via command-line options.
- Progress Tracking: Displays progress until the next post using a CLI progress bar.
- Error Handling: Handles file and configuration errors gracefully.
- Asynchronous Architecture: Utilizes asyncio for efficient and scalable operations.
- Python 3.7 or higher
- Dependencies specified in
requirements.txt
.env
file for environment-specific configuration
- Install dependencies:
pip install -r requirements.txt
- Create openssl certs for https
# Generate a private key
openssl genpkey -algorithm RSA -out threads.key -pkeyopt rsa_keygen_bits:2048
# Generate a certificate signing request (CSR)
openssl req -new -key threads.key -out threads.csr
# Generate a self-signed certificate valid for 1 year
openssl x509 -req -days 365 -in threads.csr -signkey threads.key -out threads.crt
- Create a Threads app in Meta
- Create a
.env
file for storing sensitive or environment-specific data.
Example:
THREADS_APP_ID=123123123
THREADS_API_SECRET=SomethingSecret
THREADS_SSL_CERT_FILEPATH="threads.crt"
THREADS_SSL_KEY_FILEPATH="threads.key"
# You need this page to only capture the threads post callback and copy paste this into the script if asked for it
THREADS_REDIRECT_URI="https://your-callback-url/callback"
OPENAI_API_KEY="sk-supersecretopenai-key-which-will-be-used-for-requests"
Run the bot using the following command:
python bot.py <bot_name> [OPTIONS]
<bot_name>
: The unique name of the bot (required).
-r, --role-txt-path
: Path to a file containing a role description for generating posts (optional).-c, --creds-file-path
: Path to a credentials file for authentication (optional).-f, --post-frequency
: Post frequency in seconds (optional, defaults to 7200 seconds).
python bot.py "MyThreadsBot" -r "role_description.txt" -c "creds.json" -f 3600
The entry point of the bot. Handles command-line arguments, reads optional files, and initializes the main asynchronous function.
The core loop of the bot:
- Checks for existing posts.
- Generates new posts if none are available.
- Schedules and publishes posts while ensuring proper intervals.
Handles the API communication for posting content to Threads.
Manages post generation, saving, and retrieval.
Handles loading and saving the last posted time for ensuring intervals between posts.
Sets up structured logging for monitoring and debugging.
Sets up model behaviour and role
Logging is configured in logging_setup.py
to ensure all activities are recorded. Modify the logging level and output format as needed.
The default interval between posts is set in the script:
MINIMUM_TIMESPAN_BETWEEN_POSTS = 60 * 60 * 2 # 2 hours in seconds
You can override this using the -f
or --post-frequency
command-line option.
To contribute or modify the bot:
-
Create a new branch:
git checkout -b feature/new-feature
-
Make changes and test locally:
python bot.py <bot_name> [OPTIONS]
-
Submit a pull request to the
main
branch.
This project is licensed under the specific License. See the LICENSE
file for details.