This project synchronizes posts between a Telegram channel and a WordPress blog. It uses a SQLite database to store the posts and the python-telegram-bot library to interact with the Telegram API.
We strongly believe that we cannot trust media controlled by billionaires. If we use them, we should also keep a copy on our own website and database. That's why we created this Python program—to help you save everything you publish on your Telegram channel, both on your local machine and your WordPress site.
- Windows, Linux, or WSL (Windows Subsystem for Linux) on Windows is highly recommended
- Python 3.x
- pip (Python package installer)
- Telegram Channel
- Telegram Bot
- WordPress Site
-
Clone the repository:
git clone https://github.com/SmarterTechAustralia/microblog.git cd microblog- It is recommended to run it in a virtual environment. For example:
python3 -m venv .- Enter your virtual environment in Linux:
source bin/activate- Or if you are using Windows:
.\Scripts\activate
Note: Replace
python3withpythonif you are on Windows. -
Install the required Python packages:
pip install -r requirements.txt
-
Create a keys.yaml file in the project root directory with the following structure:
- in linux try
sudo nano keys.yaml
telegram: bot_token: "YOUR_TELEGRAM_BOT_TOKEN" channel_id: "YOUR_TELEGRAM_CHANNEL_ID" wordpress: urlen: "YOUR_EN_WORDPRESS_URL" urlfa: "YOUR_FA_WORDPRESS_URL" username: "YOUR_WORDPRESS_USERNAME" password: "YOUR_WORDPRESS_API_KEY"
- in linux try
- note: you can use "none" for
urlfaandurlento skip
- Update your keys.yaml with Telegram Bot and WordPress credentials:
-
Telegram Bot:
- You should have or create a Telegram bot and add it to your channel as an admin. Follow these steps:
-
Create a Telegram Bot:
- Open the Telegram app and search for the
@BotFatherbot. - Start a chat with
@BotFatherand send the command/newbot. - Follow the instructions to choose a name and username for your bot.
- After completing the setup,
@BotFatherwill provide you with a bot token. Save this token as you will need it later.
- Open the Telegram app and search for the
-
Get the Channel ID:
- Add your bot to the desired Telegram channel.
- Make the bot an admin of the channel.
- To get the channel ID, you can use the following method:
- Forward a message from your channel to
@userinfoboton Telegram. @userinfobotwill reply with information about the forwarded message, including the channel ID (it will be a negative number).
- Forward a message from your channel to
-
Configure the Bot:
- Ensure your bot has the necessary permissions to read messages and post in the channel.
-
Add the Bot Token and Channel ID to keys.yaml:
- Use the bot token and channel ID in the keys.yaml file as shown in the installation section.
-
- You should have or create a Telegram bot and add it to your channel as an admin. Follow these steps:
-
WordPress Site:
- You should have a WordPress site with an admin username and an active API key:
-
Set Up a WordPress Site:
- Ensure you have a WordPress site up and running.
- You should have an admin username and password for your WordPress site.
-
Generate an API Key:
- Log in to your WordPress admin dashboard.
- Navigate to
Users>Profile. - Scroll down to the
Application Passwordssection. - Enter a name for the application (e.g., "Microblog Sync") and click
Add New Application Password. - Copy the generated API key and save it securely as you will need it later.
- Use this API key in the keys.yaml file you have created previously.
-
- You should have a WordPress site with an admin username and an active API key:
-
-
Initialize the database:
python sync.py --init-db
-
Run the sync process:
python sync.py
Note: Ensure your virtual environment is activated before running the sync process.
To run the app automatically using a cron job, follow these steps:
-
Open the crontab file for editing:
crontab -e
-
Add a new cron job to run the sync script at your desired interval. For example, to run the script every hour, add the following line:
0 * * * * /usr/bin/python3 /home/kevin/projects/microblog/sync.py
-
Save and close the crontab file. The cron job will now run the script automatically at the specified interval.
To run the app automatically using Task Scheduler, follow these steps:
-
Open Task Scheduler from the Start menu.
-
Click on "Create Basic Task" in the Actions pane.
-
Follow the wizard to set up the task:
- Name: Enter a name for the task (e.g., "Microblog Sync").
- Trigger: Choose how often you want the task to run (e.g., daily, weekly).
- Action: Select "Start a program" and browse to the Python executable (e.g.,
C:\Python39\python.exe). - Add arguments: Enter the path to the sync script (e.g.,
C:\Users\Kevin\projects\microblog\sync.py).
-
Finish the wizard and the task will be created. The Task Scheduler will now run the script automatically at the specified interval.
For any questions or assistance, feel free to reach out. I will respond as soon as I can. If you find this project helpful, you can support me by buying me a coffee
This project is licensed under the MIT License.
