python调用飞书机器人

python调用飞书机器人

最简单的代码

import hashlib
import base64
import hmac
import time
import requests

# 飞书webhook地址:https://open.feishu.cn/open-apis/bot/v2/xxxxxxxxxxxxx     通过设置飞书机器人可得
# 签名:yyyyyyyyyyyyyyyy    通过设置飞书机器人可得

url = 'https://open.feishu.cn/open-apis/bot/v2/xxxxxxxxxxxxx'


def gen_sign():
    secret = 'yyyyyyyyyyyyyyyy'
    timestamp = int(time.time())
    # 拼接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 timestamp, sign


def request_feishu():
    timestamp, sign = gen_sign()
    print(timestamp, sign)
    data = {
        "timestamp": timestamp,
        "sign": sign,
        "msg_type": "text",
        "content": {"text": "嘻嘻"}
    }
    headers = {
        "Content-Type": "application/json",
    }
    result = requests.post(url, json=data, headers=headers)
    print(result.json())
    return


if __name__ == '__main__':
    request_feishu()

image

posted @ 2023-02-24 17:00  zong涵  阅读(334)  评论(0编辑  收藏  举报