python發(fā)飛書機(jī)器人消息

#!/usr/bin/env python
# coding: utf-8
import requests
import json

def get_token(app_id = "*************",app_secret = "******************"):
    """獲取應(yīng)用token,需要用app_id和app_secret,主要是上傳圖片需要用到token"""
    url = r"https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal/"
    headers = {"Content-Type": "text/plain"}
    Body = {
    "app_id":app_id,
    "app_secret":app_secret
    }
    r = requests.post(url, headers=headers, json=Body)
    return json.loads(r.text)['tenant_access_token']

def upload_image(image_path):
    """上傳圖片"""
    with open(image_path, 'rb') as f:
        image = f.read()
    resp = requests.post(
        url='https://open.feishu.cn/open-apis/image/v4/put/',
        headers={'Authorization': "Bearer "+get_token()},
        files={
            "image": image
        },
        data={
            "image_type": "message"
        },
        stream=True)
    resp.raise_for_status()
    content = resp.json()
    if content.get("code") == 0:
        return content['data']['image_key']
    else:
        return Exception("Call Api Error, errorCode is %s" % content["code"])

def send_text(Text,bot):
    """發(fā)送普通消息"""
    url = f"https://open.feishu.cn/open-apis/bot/v2/hook/{bot}"
    headers = {"Content-Type": "text/plain"}
    data = {
        "msg_type": "text",
        "content": {
            "text": Text
        }
    } 
    r = requests.post(url, headers=headers, json=data)
    return r.text

def send_img(path,bot):
    """發(fā)送圖片消息"""
    url = f"https://open.feishu.cn/open-apis/bot/v2/hook/{bot}"
    headers = {"Content-Type": "text/plain"}
    data = {
        "msg_type":"image",
        "content":{
            "image_key": upload_image(path)
        }
    }  
    r = requests.post(url, headers=headers, json=data)
    return r.text

def send_markdown(text,bot):
    """發(fā)送富文本消息"""
    url = f"https://open.feishu.cn/open-apis/bot/v2/hook/{bot}"
    headers = {"Content-Type": "text/plain"}
    data = {
    "msg_type": "interactive",
    "card": {
    "config": {
        "wide_screen_mode": True
    },
    "header": {
        "title": {
            "tag": "plain_text",
            "content": "注意咯!!注意咯!?。?
        },
        "template":"red"
    },
    "elements": [{"tag": "div",
                      "text": {"content":text,
                      "tag": "lark_md"}}]}
    } 
    r = requests.post(url, headers=headers, json=data)
    return r.text

def send_card(Text,bot):
    """發(fā)送卡片消息"""
    url = f"https://open.feishu.cn/open-apis/bot/v2/hook/{bot}"
    headers = {"Content-Type": "text/plain"}
    data = {
    "msg_type": "interactive",
    "card": Text
    } 
    r = requests.post(url, headers=headers, json=data)
    return r.text

def send_file(file_path,bot):
    """發(fā)送文件,目前沒有找到方法,推薦用七牛代替"""
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容