Skip to content

Commit

Permalink
添加飞书发送图片和文件
Browse files Browse the repository at this point in the history
  • Loading branch information
TommyMerlin committed May 10, 2024
1 parent 77fdd27 commit 88b69b9
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 5 deletions.
88 changes: 85 additions & 3 deletions ANotify/Nfeishu.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
import requests
from enum import Enum
import json


class ReceiverType(Enum):
OPEN_ID = 'open_id'
CHAT_ID = 'chat_id'
Expand Down Expand Up @@ -53,6 +53,87 @@ def send_msg(self, receive_id_type: ReceiverType ,receive_id, msg):
res = requests.post(url, params=params, headers=headers, json=body)
return res.json()

def send_img(self, receive_id_type: ReceiverType ,receive_id, img_path):
url = 'https://open.feishu.cn/open-apis/im/v1/messages'

headers = {
'Authorization': 'Bearer ' + self.access_token
}

params = {
"receive_id_type": receive_id_type.value
}

body = {
"receive_id": receive_id,
"msg_type": "image",
"content": json.dumps({
"image_key": self.upload_image(img_path)
})
}

res = requests.post(url, params=params, headers=headers, json=body)
return res.json()

def send_file(self, receive_id_type: ReceiverType ,receive_id, file_path):
url = 'https://open.feishu.cn/open-apis/im/v1/messages'

headers = {
'Authorization': 'Bearer ' + self.access_token
}

params = {
"receive_id_type": receive_id_type.value
}

body = {
"receive_id": receive_id,
"msg_type": "file",
"content": json.dumps({
"file_key": self.upload_file(file_path)
})
}

res = requests.post(url, params=params, headers=headers, json=body)
return res.json()

def upload_image(self, file_path):
url = "https://open.feishu.cn/open-apis/im/v1/images"

file_name = os.path.basename(file_path)

payload = {'image_type': 'message'}
files = [
('image', (file_name, open(file_path, 'rb'), 'application/json'))
]
headers = {
'Authorization': 'Bearer ' + self.access_token
}

response = requests.request("POST", url, headers=headers, data=payload, files=files).json()
if response['msg'] == 'success':
return response['data']['image_key']

def upload_file(self, file_path):
url = "https://open.feishu.cn/open-apis/im/v1/files"
file_name = os.path.basename(file_path)

payload = {
'file_type': 'stream',
'file_name': file_name
}

files = [
('file', (file_name, open(file_path, 'rb'), 'application/json'))
]

headers = {
'Authorization': 'Bearer ' + self.access_token
}

response = requests.request("POST", url, headers=headers, data=payload, files=files).json()
if response['msg'] == 'success':
return response['data']['file_key']

class FeishuWebhookNotify:
def __init__(self, webhook_url):
Expand Down Expand Up @@ -81,10 +162,11 @@ def send_msg(self, msg):
CHAT_ID = ''

feishu = FeishuNotify(appid=APPID, appsecret=APPSECRET)
# print(feishu.send_file(ReceiverType.OPEN_ID, OPEN_ID, "E:/Desktop/gpt/Cursor_Demo/test.png"))
# print(feishu.send_msg(ReceiverType.OPEN_ID, OPEN_ID, "Hello World!"))
# print(feishu.send_msg(ReceiverType.UINION_ID, UNION_ID, "Hello World!"))
# print(feishu.send_msg(ReceiverType.USER_ID, USER_ID, "Hello World!"))
# print(feishu.send_msg(ReceiverType.CHAT_ID, CHAT_ID, "Hello World!"))

feishu_webhook = FeishuWebhookNotify(webhook_url="https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxxxxxx")
feishu_webhook.send_msg("Hello World!")
# feishu_webhook = FeishuWebhookNotify(webhook_url="https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxxxxxx")
# feishu_webhook.send_msg("Hello World!")
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ feishu.send_msg(Nfeishu.ReceiverType.OPEN_ID, OPEN_ID, "Hello World!")
feishu.send_msg(Nfeishu.ReceiverType.UINION_ID, UNION_ID, "Hello World!")
feishu.send_msg(Nfeishu.ReceiverType.USER_ID, USER_ID, "Hello World!")
feishu.send_msg(Nfeishu.ReceiverType.CHAT_ID, CHAT_ID, "Hello World!")
# 发送图片
feishu.send_img(Nfeishu.ReceiverType.OPEN_ID, OPEN_ID, "test.png")
# 发送文件
feishu.send_file(Nfeishu.ReceiverType.OPEN_ID, OPEN_ID, "test.txt")

# Webhook
feishu_webhook = Nfeishu.FeishuWebhookNotify("https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxx")
Expand Down Expand Up @@ -131,4 +135,4 @@ SENDER = ''

email_notify = Nemail.EmailNotify(MAIL_HOST, MAIL_USER, MAIL_PASS, SENDER)
email_notify.send_email("测试标题", "测试正文", attachment_filename=None, receiver='123@example.com')
```
```
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='anotify',
version='0.0.7',
version='0.0.8',
packages=find_packages(),
install_requires=[
'requests>=2.15.1',
Expand Down

0 comments on commit 88b69b9

Please sign in to comment.