import hmac import hashlib import base64 import time import requests from utils.logging import logging def gen_sign(timestamp, secret): # Splicing timestamp and secret string_to_sign = '{}\n{}'.format(timestamp, secret) hmac_code = hmac.new(string_to_sign.encode("utf-8"), digestmod=hashlib.sha256).digest() # Perform base64 processing on the result sign = base64.b64encode(hmac_code).decode('utf-8') return sign def send_feishu_msg(msg: str): url = "https://open.feishu.cn/open-apis/bot/v2/hook/729ea554-3523-4829-9cf3-8dfa90ecec5d" token = "HO0vM2bkeizmRdw5GqVrn" timestamp = str(int(time.time())) sign = gen_sign(timestamp, token) payload = { "timestamp": timestamp, "sign": sign, "msg_type": "text", "content": { "text": msg } } try: resp = requests.post(url, json=payload) logging.info("send feishu msg %s got %s", payload, resp.text) except Exception as e: logging.exception("send feishu msg %s failed: %s", payload, e)