Telegram 消息自动化处理与转发模块
本项目是一个基于 Python 的、高度可配置的自动化解决方案。其核心功能是实时监听指定的 Telegram 频道或群组的新消息并进行转发,同时提供一系列管理与查询功能。
安装
pip install tg-msg-forwarder
配置
在您的服务器任意位置(例如 /etc/tg-forwarder/ 或用户主目录)创建一个 config.json 文件。
安全提示: config.json 文件包含敏感信息。请务必将其权限设置为只有运行服务的用户才能读取,例如:chmod 600 /path/to/your/config.json。
参考以下模板填入您的配置信息:
{
"telegram": {
"api_id": 1234567,
"api_hash": "your_api_hash_here",
"session_name": "forwarder_session"
},
"channels": [
{
"name": "GoogleAI",
"actions": [
{
"type": "feishu",
"details": {
"webhook_url": "https://open.feishu.cn/open-apis/bot/v2/hook/your_webhook"
},
"template": "来自 {channel_name} 的新动态!\n\n发送人: {sender_name}\n\n{text}"
},
{
"type": "sqlite",
"details": {
"db_path": "/var/data/telegram_archive.db"
}
}
],
"filters": {
"include_keywords": ["important", "release"],
"exclude_keywords": ["spam", "ad"]
}
},
{
"name": -1001234567890,
"actions": [
{
"type": "dingtalk",
"details": {
"webhook_url": "https://oapi.dingtalk.com/robot/send?access_token=your_token",
"secret": "your_dingtalk_secret"
}
}
]
}
]
}
使用方法
首次运行 (重要)
在您第一次运行本工具,或者更换了 session_name 后,您必须先在命令行前台手动运行一次程序以完成登录:
tg-msg-forwarder --config /path/to/your/config.json --list-channels
程序会提示您输入 手机号 (Phone Number)、登录验证码 (Login Code) 和您的 两步验证密码 (2FA Password)。成功登录后,程序会在配置文件所在目录生成一个 .session 文件。
看到频道列表成功输出后,证明登录已完成。之后您就可以使用 Ctrl+C 退出,然后通过 systemd 或其他方式在后台无交互地运行服务了。
1. 启动转发服务
这是工具的核心功能。运行后,程序将持续监听新消息并执行转发。
tg-msg-forwarder --config /path/to/your/config.json
2. 查询与管理功能
以下命令用于执行一次性操作,执行后程序会自动退出。
追溯处理历史消息
一次性处理所有已配置频道在过去一段时间内的历史消息。
tg-msg-forwarder --config /path/to/your/config.json --process-history <时长>
获取频道与群组列表
获取您账户下所有频道和群组的列表,用于查找私有频道的数字 ID。
tg-msg-forwarder --config /path/to/your/config.json --list-channels
获取好友列表
列出您账户中的所有联系人(好友)。
tg-msg-forwarder --config /path/to/your/config.json --list-contacts
查看频道/群组的详细信息
获取指定频道或群组的详细信息。
tg-msg-forwarder --config /path/to/your/config.json --channel-info <ID 或 Username>
部署为后台服务 (systemd)
创建服务文件 /etc/systemd/system/tg-msg-forwarder.service:
[Unit]
Description=Telegram Message Forwarder Service
After=network.target
[Service]
Type=simple
User=your_username
ExecStart=/usr/local/bin/tg-msg-forwarder --config /home/your_username/config.json
Restart=on-failure
RestartSec=10
[Install]
WantedBy=multi-user.target
管理服务:
sudo systemctl daemon-reload
sudo systemctl start tg-msg-forwarder
sudo systemctl enable tg-msg-forwarder
Metadata
Release files for tg-msg-forwarder 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| tg_msg_forwarder-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Release files / tg_msg_forwarder-0.1.0-py3-none-any.whl
| Download URL | tg_msg_forwarder-0.1.0-py3-none-any.whl |
|---|---|
| Size | 11.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
75cc1c1fbdc8f8782dc68be063cd6a770cbdcc80020e370a98c59703433d5c73
|
|
BLAKE2b-256 checksum How to use checksums |
02a734312b6460f3c8b14ea9339de1a7ea8dc5766d0a6eebf89b2e3eecce71b7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.2
|
Security for tg-msg-forwarder 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.