Variya
2026-03-11 f95cdc7e5df20d8a14be0322baaf4c7e17b70312
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/env python3
"""
飞书推送模块(支持签名验证)
"""
 
import requests
import json
import time
import hmac
import hashlib
import base64
from datetime import datetime
 
 
class FeishuPusher:
    """飞书推送器(支持签名验证)"""
 
    def __init__(self, webhook_url=None, secret=None):
        """
        Args:
            webhook_url: 飞书机器人webhook地址
            secret: 签名密钥(如果启用了签名验证)
        """
        self.webhook_url = webhook_url
        self.secret = secret
 
    def _gen_sign(self, timestamp):
        """
        生成签名(如果配置了secret)
 
        Args:
            timestamp: 时间戳
 
        Returns:
            签名字符串
        """
        if not self.secret:
            return None
 
        # 拼接timestamp和secret
        string_to_sign = f"{timestamp}\n{self.secret}"
 
        # 使用HMAC-SHA256计算签名
        hmac_code = hmac.new(
            self.secret.encode("utf-8"),
            string_to_sign.encode("utf-8"),
            digestmod=hashlib.sha256
        ).digest()
 
        # Base64编码
        sign = base64.b64encode(hmac_code).decode('utf-8')
        return sign
 
    def send_text(self, text):
        """
        发送文本消息
 
        Args:
            text: 文本内容
        """
        if not self.webhook_url:
            print("⚠️ 未配置webhook,跳过推送")
            return False
 
        headers = {"Content-Type": "application/json"}
 
        # 构建消息体
        data = {
            "msg_type": "text",
            "content": {"text": text}
        }
 
        # 如果配置了secret,添加签名
        if self.secret:
            timestamp = int(time.time())
            sign = self._gen_sign(timestamp)
            data["timestamp"] = str(timestamp)
            data["sign"] = sign
 
        try:
            response = requests.post(
                self.webhook_url,
                headers=headers,
                json=data,
                timeout=10
            )
 
            if response.status_code == 200:
                result = response.json()
                if result.get('StatusCode') == 0 or result.get('code') == 0:
                    print("✓ 飞书推送成功")
                    return True
                else:
                    print(f"✗ 飞书推送失败: {result.get('msg', '未知错误')}")
                    return False
            else:
                print(f"✗ 飞书推送失败: HTTP {response.status_code}")
                return False
 
        except Exception as e:
            print(f"✗ 飞书推送异常: {e}")
            return False
 
    def send_markdown(self, title, content):
        """
        发送Markdown格式消息
 
        Args:
            title: 标题
            content: Markdown内容
        """
        if not self.webhook_url:
            print("⚠️ 未配置webhook,跳过推送")
            return False
 
        headers = {"Content-Type": "application/json"}
 
        # 构建消息体
        data = {
            "msg_type": "interactive",
            "card": {
                "config": {"wide_screen_mode": True},
                "header": {
                    "title": {"content": title, "tag": "plain_text"},
                    "template": "turquoise"
                },
                "elements": [
                    {
                        "tag": "markdown",
                        "content": content
                    }
                ]
            }
        }
 
        # 如果配置了secret,添加签名
        if self.secret:
            timestamp = int(time.time())
            sign = self._gen_sign(timestamp)
            data["timestamp"] = str(timestamp)
            data["sign"] = sign
 
        try:
            response = requests.post(
                self.webhook_url,
                headers=headers,
                json=data,
                timeout=10
            )
 
            if response.status_code == 200:
                result = response.json()
                if result.get('StatusCode') == 0 or result.get('code') == 0:
                    print("✓ 飞书推送成功")
                    return True
                else:
                    print(f"✗ 飞书推送失败: {result.get('msg', '未知错误')}")
                    return False
            else:
                print(f"✗ 飞书推送失败: HTTP {response.status_code}")
                return False
 
        except Exception as e:
            print(f"✗ 飞书推送异常: {e}")
            return False
 
    def send_report(self, report_path):
        """
        发送完整报告
 
        Args:
            report_path: 报告文件路径
        """
        try:
            with open(report_path, 'r', encoding='utf-8') as f:
                report_content = f.read()
        except Exception as e:
            print(f"✗ 读取报告失败: {e}")
            return False
 
        # 提取标题和内容
        lines = report_content.split('\n')
        title = lines[0].replace('#', '').strip()
        content = '\n'.join(lines[1:])
 
        # 发送
        return self.send_markdown(title, content)
 
 
def main():
    """测试飞书推送"""
    import os
    import sys
 
    # 从环境变量或配置文件加载
    webhook_url = os.environ.get('FEISHU_WEBHOOK_URL')
    secret = os.environ.get('FEISHU_SECRET')
 
    # 尝试从配置文件加载
    if not webhook_url:
        config_file = 'config/feishu_config.json'
        if os.path.exists(config_file):
            with open(config_file) as f:
                config = json.load(f)
                webhook_url = config.get('webhook_url')
                secret = config.get('secret')
 
    if not webhook_url:
        print("⚠️ 请配置webhook_url")
        print("方式1: 设置环境变量 FEISHU_WEBHOOK_URL 和 FEISHU_SECRET")
        print("方式2: 编辑配置文件 config/feishu_config.json")
        return
 
    pusher = FeishuPusher(webhook_url, secret)
 
    # 测试推送
    print("=" * 60)
    print("测试飞书推送")
    print("=" * 60)
 
    success = pusher.send_text(
        "📊 A股量化系统 - 测试消息\n\n"
        "系统已配置成功!\n"
        f"推送时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n"
        "签名验证: " + ("已启用" if secret else "未启用")
    )
 
    if success:
        print("\n✅ 测试成功!")
    else:
        print("\n❌ 测试失败!")
        if not secret:
            print("\n💡 提示: 当前Webhook可能需要签名验证")
            print("请在飞书群设置中查看机器人的签名密钥(secret)")
            print("并更新配置文件: config/feishu_config.json")
 
 
if __name__ == "__main__":
    main()