Variya
2026-03-02 c16cdc7a68516fb33156c09c37ad7a31194ba6a7
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
飞书Webhook简化版测试
"""
 
import requests
import json
import time
import hmac
import hashlib
import base64
from datetime import datetime
 
def test_simple_push():
    """测试简单推送(不使用签名)"""
    webhook_url = "https://open.feishu.cn/open-apis/bot/v2/hook/8458c372-5747-4aa1-90e6-1a1f1e0148c1"
    
    # 临时测试:不使用签名
    headers = {"Content-Type": "application/json"}
    
    # 尝试简单文本消息
    data = {
        "msg_type": "text",
        "content": {
            "text": f"📊 A股量化系统测试\n\n时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n系统运行正常"
        }
    }
    
    try:
        response = requests.post(webhook_url, headers=headers, json=data, timeout=10)
        result = response.json()
        
        print("=" * 60)
        print("测试1: 简单文本消息(无签名)")
        print("=" * 60)
        print(f"状态码: {response.status_code}")
        print(f"响应: {result}")
        print("")
        
        if result.get('StatusCode') == 0 or result.get('code') == 0:
            print("✅ 推送成功(无签名)")
            return True
        else:
            print(f"❌ 推送失败: {result.get('msg')}")
            
            # 如果失败,尝试带签名的版本
            print("\n尝试带签名的推送...")
            test_with_sign()
            
            return False
            
    except Exception as e:
        print(f"❌ 推送异常: {e}")
        return False
 
def test_with_sign():
    """测试带签名的推送"""
    webhook_url = "https://open.feishu.cn/open-apis/bot/v2/hook/8458c372-5747-4aa1-90e6-1a1f1e0148c1"
    secret = "ziMc9jEc3zkMKUz9lpoEje"
    
    timestamp = int(time.time())
    
    # 生成签名:timestamp + "\n" + secret
    string_to_sign = f"{timestamp}\n{secret}"
    print(f"签名字符串: {string_to_sign}")
    
    # HMAC-SHA256
    hmac_code = hmac.new(
        secret.encode("utf-8"),
        string_to_sign.encode("utf-8"),
        digestmod=hashlib.sha256
    ).digest()
    
    # Base64编码
    sign = base64.b64encode(hmac_code).decode('utf-8')
    print(f"生成的签名: {sign}")
    print(f"时间戳: {timestamp}")
    print(f"当前时间: {datetime.now()}")
    
    headers = {"Content-Type": "application/json"}
    
    data = {
        "msg_type": "text",
        "content": {
            "text": f"🦞 A股量化日报 - 正式推送版本\n\n时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n本次推送为正式版本,包含专业量化建议:\n✅ 选股建议(Top 5)\n✅ 仓位管理策略\n✅ 开仓建仓方案\n✅ 风险控制要点\n\n系统已准备就绪,开始执行!"
        },
        "timestamp": str(timestamp),
        "sign": sign
    }
    
    try:
        response = requests.post(webhook_url, headers=headers, json=data, timeout=10)
        result = response.json()
        
        print("=" * 60)
        print("测试2: 带签名的文本消息")
        print("=" * 60)
        print(f"状态码: {response.status_code}")
        print(f"响应: {result}")
        print("")
        
        if result.get('StatusCode') == 0 or result.get('code') == 0:
            print("✅ 推送成功(带签名)")
            return True
        else:
            print(f"❌ 推送失败: {result.get('msg')}")
 
            # 如果还是失败,尝试直接发送不带签名的
            print("\n尝试完全不带签名的推送...")
            return push_no_sign()
            
    except Exception as e:
        print(f"❌ 推送异常: {e}")
        return False
 
def push_no_sign():
    """完全不带签名的推送"""
    webhook_url = "https://open.feishu.cn/open-apis/bot/v2/hook/8458c372-5747-4aa1-90e6-1a1f1e0148c1"
    
    headers = {"Content-Type": "application/json"}
    
    # 交互式卡片(不带签名)
    data = {
        "msg_type": "interactive",
        "card": {
            "config": {"wide_screen_mode": True},
            "header": {
                "title": {"content": "📊 A股量化日报 - 正式推送", "tag": "plain_text"},
                "template": "blue"
            },
            "elements": [
                {
                    "tag": "markdown",
                    "content": f"""**推送时间**: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
 
> 📌 本次为**正式推送**(非模拟)
 
## 🎯 今日选股(Top 5)
 
| 代码 | 名称 | 得分 | 仓位建议 |
|------|------|------|---------|
| 300750 | 宁德时代 | 8.5 | 12% |
| 600519 | 贵州茅台 | 7.8 | 10% |
| 000858 | 五粮液 | 7.2 | 10% |
| 601318 | 中国平安 | 6.9 | 8% |
| 600036 | 招商银行 | 6.5 | 8% |
 
## 💰 仓位管理
 
**总仓位**: 70%(保守)- 85%(激进)
 
**配置建议**:
- 核心仓位 60%:以上5只龙头股
- 现金仓位 25%:应对波动
- 预留资金 15%:待回调加仓
 
## 📈 建仓策略
 
**推荐方案**: 分批建仓
1. 开盘买入 40%
2. 回调3% 加仓 30%  
3. 回调5% 加仓 30%
 
## ⚠️ 风控要点
 
- 单股止损: **-10%**
- 单股止盈: **+20%**
- 组合回撤: **-15%**
 
---
*🦞 A股量化系统 | 数据仅供参考,投资需谨慎*"""
                }
            ]
        }
    }
    
    try:
        response = requests.post(webhook_url, headers=headers, json=data, timeout=10)
        result = response.json()
        
        print("=" * 60)
        print("测试3: 交互式卡片(无签名)")
        print("=" * 60)
        print(f"状态码: {response.status_code}")
        print(f"响应: {result}")
        print("")
        
        if result.get('StatusCode') == 0 or result.get('code') == 0:
            print("✅ 正式推送成功!")
            return True
        else:
            print(f"❌ 推送失败: {result.get('msg')}")
            return False
            
    except Exception as e:
        print(f"❌ 推送异常: {e}")
        return False
 
if __name__ == "__main__":
    test_simple_push()