A Telegram bot that logs messages and forwards them to a SiYuan Inbox.
- Save messages to SiYuan Inbox via API
- Generate AI summaries for longer messages
- Handle URL forwarding with auto-summarization
- System statistics with fastfetch
/help- Show help message/s [message]- Save a message to SiYuan/stats- Get system statistics
For messages longer than 128 characters, the bot uses OpenAI to generate a concise summary. This summary is used as the title for the Siyuan note and added to the note content for context.
The title format is: YYYY-MM-DD Summary Text
When a URL is posted in the chat, the bot will:
- Scrape the web page content
- Strip HTML tags and extract the text
- Send the content to OpenAI for summarization
- Create a Siyuan inbox note with the summary and original URL
This allows you to quickly capture and summarize web content without copying and pasting.
Telegram:
Siyuan Result:
- Python 3.7+
- python-telegram-bot
- openai
- requests
- beautifulsoup4
- Clone the repository
- Install dependencies:
pip install -r requirements.txt - Configure the
.envfile - Run the bot:
python main.py
Copy the sample.env file to .env and edit the values:
cp sample.env .envThen edit the .env file with your specific configuration:
TELEGRAM_BOT_TOKEN: Your Telegram bot token from BotFatherALLOWED_USERIDS: Comma-separated list of Telegram user IDs allowed to use the botALLOWED_CHATIDS: Comma-separated list of Telegram chat IDs where the bot can operateSIYUAN_TOKEN: Your SiYuan API token
DEBUG: Set to "True" to enable debug logging (default: False)OPENAI_TOKEN: OpenAI API token for summary generationOPENAI_MODEL: OpenAI model to use (default: gpt-3.5-turbo)
You can customize these user-facing messages by editing these variables in the .env file:
TXT_GENERAL_HELP: Message shown when user sends text without a commandTXT_MISSING_CONTENT: Message shown when /s command is used without contentTXT_SEND_FAILED: Message shown when sending to SiYuan failsTXT_SEND_SUCCESS: Message shown when sending to SiYuan succeedsTXT_SEND_SUCCESS_WITH_TITLE: Message shown when sending to SiYuan succeeds with a title (use {title} placeholder)
Note: The help text (TXT_HELP_TEXT) is defined in the code and cannot be customized via .env file due to Python-dotenv limitations with multiline values.
For security reasons, the bot should run as a non-root user. Here's how to create a limited user:
-
Create a new user with a limited shell:
sudo useradd -r -s /usr/sbin/nologin telegrambot
-
Create a directory for the bot and set permissions:
sudo mkdir -p /opt/solrem_bot sudo chown telegrambot:telegrambot /opt/solrem_bot
-
Copy the bot files to the new directory:
sudo cp -r * /opt/solrem_bot/ sudo chown -R telegrambot:telegrambot /opt/solrem_bot
Create a systemd service file to run the bot as a service:
-
Create a file in
/etc/systemd/system/telegram-rem-bot.service:[Unit] Description=Telegram Bot REM After=network.target [Service] Type=simple User=telegrambot Group=telegrambot WorkingDirectory=/opt/solrem_bot/ ExecStart=/usr/bin/python3 /opt/solrem_bot/main.py Restart=on-failure StandardOutput=journal StandardError=journal Environment=PYTHONUNBUFFERED=1 [Install] WantedBy=multi-user.target -
Enable and start the service:
sudo systemctl daemon-reload sudo systemctl enable telegram-rem-bot.service sudo systemctl start telegram-rem-bot.service -
Check the service status and logs:
sudo systemctl status telegram-rem-bot.service sudo journalctl -u telegram-rem-bot.service -f