import time import hmac import hashlib import base64 import requests import json # 飞书Webhook URL webhook_url = 'https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx' # 飞书Webhook密钥 secret = 'xxxxxxxxxxxxxxxxxxxxxx' def gen_sign(timestamp, secret): # 拼接timestamp和secret string_to_sign = '{}\n{}'.format(timestamp, secret) hmac_code = hmac.new(string_to_sign.encode("utf-8"), digestmod=hashlib.sha256).digest() # 对结果进行base64处理 sign = base64.b64encode(hmac_code).decode('utf-8') return sign def send_message_to_feishu_webhook(webhook_url, secret, message): # 获取当前时间戳 timestamp = int(time.time()) # 计算签名 sign = gen_sign(timestamp, secret) # 设置请求头 headers = { 'Content-Type': 'application/json' } # 设置请求体 payload = { 'timestamp': str(timestamp), 'sign': sign, 'msg_type': 'text', 'content': { 'text': message } } # 发送POST请求 response = requests.post(webhook_url, headers=headers, data=json.dumps(payload)) # 输出响应内容 print(response.json()) # 要发送的消息内容 message = 'Hello, Feishu! This is a test message.' # 发送消息到飞书Webhook send_message_to_feishu_webhook(webhook_url, secret, message)