Meeken 的杂物间

14 object(s)
 

写个飞书消息推送类

import requests
import json


class Feishu:
    def __init__(self):
        self.app_id = 'cli_9fd1bdc4cfee500e'
        self.app_secret = '你的 Secret'

    def get_token(self):
        payload = {
            'app_id': self.app_id,
            'app_secret': self.app_secret,
        }
        r = requests.post(
            'https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal/', data=payload)
        return r.json()['tenant_access_token'] if r.ok else ''

    def send_msg(self, title='hello', content='world'):
        token = self.get_token()
        headers = {
            'Authorization': 'Bearer %s' % token,
            'Content-Type': 'application/json'
        }
        msg = {
            "chat_id": "oc_6fdbd9480850cdf0cdacd6bce1fa9705",
            "msg_type": "post",
            "content": {
                "post": {
                    "zh_cn": {
                        "title": title,
                        "content": [
                            [
                                {
                                    "tag": "text",
                                    "un_escape": True,
                                    "text": content
                                }
                            ]
                        ]
                    }
                }
            }
        }

        print(json.dumps(msg))

        res = requests.post(
            'https://open.feishu.cn/open-apis/message/v4/send/', headers=headers, json=msg)
        print(res.json())
        return True


feishu = Feishu()
feishu.send_msg(title='你好', content='world')

写好了。