一个高度可配置的工具,用于实时监听Telegram消息并转发到钉钉、飞书、Slack或数据库。
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tg_msg_forwarder-0.1.0-py3-none-any.whl.
File metadata
- Download URL: tg_msg_forwarder-0.1.0-py3-none-any.whl
- Upload date:
- Size: 11.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
75cc1c1fbdc8f8782dc68be063cd6a770cbdcc80020e370a98c59703433d5c73
|
|
| MD5 |
3179dc7b793ec87947dca6b7aa769d8a
|
|
| BLAKE2b-256 |
02a734312b6460f3c8b14ea9339de1a7ea8dc5766d0a6eebf89b2e3eecce71b7
|