Python通过飞书机器人推送消息至飞书群中

首先需要在飞书开发者页面定制飞书消息卡片,然后将卡片的json复制出来即可。

import json
import requests
from requests_toolbelt import MultipartEncoder


class Send_feishu_msg:
    # 应用凭证,获取方式查看上面的步骤
    app_id = "cli_xxxxxxxxxxxxxxx"
    app_secret = "nEkQ8A2x13_xxxxxxxxxxx"
    # 机器人webhook
    chatGPT_url = 'https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxxxxxxxx'
    headers = {
        "Content-Type": "application/json; charset=utf-8",
    }
    json_data = {
        "config": {
            "wide_screen_mode": True
        },
        "header": {
            "template": "green",
            "title": {
                "tag": "plain_text",
                "content": "📢📢📢xxxxxxxxxxx📢📢📢"
            }
        },
        "elements": [
            {
                "tag": "markdown",
                "content": "📌**xxxxxxxxxxx**"
            },
            {
                "alt": {
                    "content": "",
                    "tag": "plain_text"
                },
                "img_key": "${Picture_1}",
                "tag": "img",
                "mode": "fit_horizontal",
                "compact_width": False
            },
            {
                "tag": "hr"
            },
            {
                "tag": "markdown",
                "content": "📌**xxxxxxxxxxx**"
            },
            {
                "tag": "img",
                "img_key": "${Picture_2}",
                "alt": {
                    "tag": "plain_text",
                    "content": ""
                },
                "mode": "fit_horizontal",
                "preview": True,
                "compact_width": False
            },
            {
                "tag": "action",
                "actions": [
                    {
                        "tag": "button",
                        "text": {
                            "tag": "plain_text",
                            "content": "点击查看更多详情"
                        },
                        "type": "primary",
                        "multi_url": {
                            "url": "https://lotuscars.feishu.cn/sheets/xxxxxxxxx",
                            "pc_url": "",
                            "android_url": "",
                            "ios_url": ""
                        }
                    }
                ]
            },
            {
                "tag": "div",
                "text": {
                    "content": "<at id=all></at>",
                    "tag": "lark_md"
                }
            }
        ]
    }

    def __init__(self):
        url = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/xxxxxxxx"
        payload_data = {
            "app_id": self.app_id,
            "app_secret": self.app_secret
        }
        response = requests.post(url=url, data=json.dumps(payload_data), headers=self.headers).json()
        self.token = response['tenant_access_token']

    def uploadImage(self, picture_path):
        get_image_key_url = "https://open.feishu.cn/open-apis/im/v1/xxxxxx"
        form = {'image_type': 'message',
                'image': (open(picture_path, 'rb'))}  # 需要替换具体的path
        multi_form = MultipartEncoder(form)
        image_key_headers = {
            'Authorization': 'Bearer ' + self.token,
        }
        image_key_headers['Content-Type'] = multi_form.content_type
        response = requests.request("POST", get_image_key_url, headers=image_key_headers, data=multi_form)
        content = json.loads(response.content)
        img_key = str(content['data']['image_key'])
        print(response.headers['X-Tt-Logid'])  # for debug or oncall
        print(f'The img_key is {img_key}')  # Print Response
        return img_key

    def send_msg_talk(self, picture1_img_key, picture2_img_key):
        self.json_data['elements'][1]['img_key'] = picture1_img_key
        self.json_data['elements'][4]['img_key'] = picture2_img_key
        msg_card = json.dumps(self.json_data)
        body = json.dumps({"msg_type": "interactive", "card": msg_card})
        response = requests.post(url=self.chatGPT_url, data=body, headers=self.headers)
        print(response)

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值