import requestsimport jsonimport hmacimport hashlibimport base64import time# ========== 你的飞书配置 ==========WEBHOOK_URL = "https://open.feishu.cn/open-apis/bot/v2/hook/4e256d5e-508a-406d-xxxx"SECRET = "ZQ-------" # 签名密钥# ========== 生成飞书签名 ==========def gen_sign(secret): timestamp = int(time.time()) string_to_sign = '{}\n{}'.format(timestamp, secret) hmac_code = hmac.new( string_to_sign.encode(), digestmod=hashlib.sha256 ).digest() sign = base64.b64encode(hmac_code).decode() return timestamp, sign# ========== 发送飞书消息 ==========def send_feishu_msg(text): timestamp, sign = gen_sign(SECRET) payload = { "timestamp": timestamp, "sign": sign, "msg_type": "text", "content": { "text": text } } headers = {"Content-Type": "application/json"} resp = requests.post(WEBHOOK_URL, data=json.dumps(payload), headers=headers) print("返回结果:", resp.json()) return resp.json()# ========== 测试发送 ==========if __name__ == '__main__': send_feishu_msg("✅ Python 飞书 Webhook 发送成功!\n带签名安全版")