python-飞书机器人发送消息

首先建群,建机器人,不多说,自行操作,获取webhook,通过webhook发送信息,原api地址: https://open.feishu.cn/document/ukTMukTMukTM/ucTM5YjL3ETO24yNxkjN

python-飞书机器人发送消息_第1张图片

1、发送普通消息,代码如下

import requests
import json

def get_data():
    data = {
        "msg_type": "text",
        "content": {
            "text": "你好"
        }
    }
    return json.dumps(data,ensure_ascii=True).encode("utf-8")
    
def req(data):
	#webhook
    url = "https://open.feishu.cn/open-apis/bot/v2/hook/111-22-33-44"
    header = {
        "Content-type": "application/json",
        "charset":"utf-8"
    }
    requests.post(url,data=data,headers=header)

if __name__ == '__main__':
    req(get_data())

效果图
在这里插入图片描述
2、发送富文本

import requests
import json

def get_data():
    data = {
        "msg_type": "post",
        "content": {
            "post": {
                "zh_cn": {
                    "title": "项目更新通知",
                    "content": [
                        [{
                            "tag": "text",
                            "text": "项目有更新: "
                        },
                        {
                            "tag": "a",
                            "text": "请查看",
                            "href": "https://www.baidu.com/"
                        },
                        {
                            "tag": "at",
                            "user_id": "123456"
                        }
                        ]
                    ]
                }
            }
        }
    }
    return json.dumps(data,ensure_ascii=True).encode("utf-8")


def req(data):
    url = "https://open.feishu.cn/open-apis/bot/v2/hook/11-11-22-33"
    header = {
        "Content-type": "application/json",
        "charset":"utf-8"
    }
    requests.post(url,data=data,headers=header)

if __name__ == '__main__':
    req(get_data())

效果图
python-飞书机器人发送消息_第2张图片

你可能感兴趣的:(学习,总结,python)