Metadata-Version: 2.4
Name: tg-msg-forwarder
Version: 0.1.0
Summary: 一个高度可配置的工具，用于实时监听Telegram消息并转发到钉钉、飞书、Slack或数据库。
Author-email: Your Name <you@example.com>
License: MIT License
        
        Copyright (c) 2025 Your Name
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/yourusername/tg-msg-forwarder
Project-URL: Bug Tracker, https://github.com/yourusername/tg-msg-forwarder/issues
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Topic :: Communications :: Chat
Classifier: Topic :: System :: Logging
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: telethon
Requires-Dist: requests
Requires-Dist: sqlalchemy
Requires-Dist: PyMySQL
Requires-Dist: pysocks
Dynamic: license-file

# Telegram 消息自动化处理与转发模块

本项目是一个基于 Python 的、高度可配置的自动化解决方案。其核心功能是实时监听指定的 Telegram 频道或群组的新消息并进行转发，同时提供一系列管理与查询功能。

## 安装

```bash
pip install tg-msg-forwarder
```

## 配置
在您的服务器任意位置（例如 /etc/tg-forwarder/ 或用户主目录）创建一个 config.json 文件。

**安全提示**: `config.json` 文件包含敏感信息。请务必将其权限设置为只有运行服务的用户才能读取，例如：`chmod 600 /path/to/your/config.json`。

参考以下模板填入您的配置信息：

```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` 后，您必须先在命令行前台手动运行一次程序以完成登录：

```bash
tg-msg-forwarder --config /path/to/your/config.json --list-channels
```

程序会提示您输入 **手机号 (Phone Number)**、**登录验证码 (Login Code)** 和您的 **两步验证密码 (2FA Password)**。成功登录后，程序会在配置文件所在目录生成一个 `.session` 文件。

看到频道列表成功输出后，证明登录已完成。之后您就可以使用 `Ctrl+C` 退出，然后通过 `systemd` 或其他方式在后台无交互地运行服务了。

### 1. 启动转发服务

这是工具的核心功能。运行后，程序将持续监听新消息并执行转发。

```bash
tg-msg-forwarder --config /path/to/your/config.json
```

### 2. 查询与管理功能

以下命令用于执行一次性操作，执行后程序会自动退出。

#### 追溯处理历史消息

一次性处理所有已配置频道在过去一段时间内的历史消息。

```bash
tg-msg-forwarder --config /path/to/your/config.json --process-history <时长>
```

#### 获取频道与群组列表

获取您账户下所有频道和群组的列表，用于查找私有频道的数字 ID。

```bash
tg-msg-forwarder --config /path/to/your/config.json --list-channels
```

#### 获取好友列表

列出您账户中的所有联系人（好友）。

```bash
tg-msg-forwarder --config /path/to/your/config.json --list-contacts
```

#### 查看频道/群组的详细信息

获取指定频道或群组的详细信息。

```bash
tg-msg-forwarder --config /path/to/your/config.json --channel-info <ID 或 Username>
```

## 部署为后台服务 (systemd)

创建服务文件 `/etc/systemd/system/tg-msg-forwarder.service`：

```ini
[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
```

管理服务：

```bash
sudo systemctl daemon-reload
sudo systemctl start tg-msg-forwarder
sudo systemctl enable tg-msg-forwarder
```
