#!/usr/bin/env python
# coding: utf-8
import requests
import json
def get_token(app_id = "*************",app_secret = "******************"):
"""获取应用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):
"""发送普通消息"""
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):
"""发送图片消息"""
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):
"""发送富文本消息"""
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):
"""发送卡片消息"""
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):
"""发送文件,目前没有找到方法,推荐用七牛代替"""
网友评论