This commit is contained in:
quantulr
2024-01-25 11:56:36 +08:00
parent 4b4ee17bc7
commit 6b67995704
4 changed files with 50 additions and 11 deletions

Binary file not shown.

View File

@ -0,0 +1,2 @@
class FeishuAuthException(Exception):
pass

View File

@ -4,22 +4,37 @@ from io import BytesIO
import requests
from custom_exception import FeishuAuthException
from event_map_mapping import mapping
from redis import asyncio as aioredis
headers = {"Authorization": "Bearer t-g1041oeFF3LTS5MA3HQYUHYFOK246EAEPFJWPRWP"}
app_id = "cli_a525f3d78e3e500c"
app_secret = "JVhnbKfXifddjHVwcTqbEfn1rDQBYqDD"
headers = {"Authorization": ""}
def get_access_token(api_id, secret):
url = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal"
data = {
"app_id": api_id,
"app_secret": secret
}
resp = requests.post(url, data=data).content.decode('utf-8')
return json.loads(resp)['tenant_access_token']
# 获取事件的图片
def get_image_url(event):
analysis_key = mapping.get(event['eventType'])['analyze_key']
print(analysis_key)
if analysis_key is None:
return []
return None
images = []
for el in event['data'][analysis_key]:
images.append(el['imageUrl'])
if len(images) == 0:
return None
return images
@ -39,6 +54,10 @@ def upload_image(url):
]
image = requests.request("POST", "https://open.feishu.cn/open-apis/im/v1/images", data=payload,
files=files, headers=headers).content.decode('utf-8')
data = json.loads(image)
code = data['code']
if code == 99991663:
raise FeishuAuthException
image_key = json.loads(image)['data']['image_key']
return image_key
@ -49,10 +68,16 @@ def push_to_feishu(data):
# push_url = "http://192.168.20.115:8000/ipaasuat/engine_company/anycross/trigger/callback/MmRhZTE4OTRiYjVkZDQ5YWNmOGRmZDI0NjQ1MTBlODUw/1.0.0"
data = json.dumps(data)
r = requests.post(push_url, data=data, headers=headers).content.decode('utf-8')
data = json.loads(r)
code = data['code']
if code == 99991663:
raise FeishuAuthException
print(r)
async def read_event():
access_token = get_access_token(app_id, app_secret)
headers['Authorization'] = f"Bearer {access_token}"
redis_client = await aioredis.from_url("redis://localhost")
while True:
data = await redis_client.brpop("event")
@ -64,8 +89,10 @@ async def read_event():
print(event_type)
# TODO: 上传图片到飞书链接
images = get_image_url(event)
feishu_image_url = upload_image(images[0])
print(feishu_image_url)
feishu_image_key = None
if images is not None:
feishu_image_key = upload_image(images[0])
print(feishu_image_key)
# TODO: 根据ip_address获取对应userid
#
@ -85,14 +112,15 @@ async def read_event():
"elements": [
{
"tag": "markdown",
"content": "这里是卡片文本支持使用markdown标签设置文本格式。例如\n*斜体* 、**粗体**、~~删除线~~、[文字链接](https://www.feishu.cn)、<at id=all></at>、<font color='red'> 彩色文本 </font>"
"content": "这里是卡片文本支持使用markdown标签设置文本格式。例如\n*斜体* 、**粗体**、~~删除线~~、[文字链接]("
"https://www.feishu.cn)、<at id=all></at>、<font color='red'> 彩色文本 </font>"
},
{
"alt": {
"content": "",
"tag": "plain_text"
},
"img_key": feishu_image_url,
"img_key": feishu_image_key,
"tag": "img",
"mode": "fit_horizontal",
"compact_width": False
@ -123,13 +151,17 @@ async def read_event():
}
push_to_feishu(push_data)
print(event_type["type"], ip_address)
except FeishuAuthException:
print("飞书登录失败")
# TODO: 重新获取 access token
access_token = get_access_token(app_id, app_secret)
headers['Authorization'] = f"Bearer {access_token}"
await redis_client.lpush("event", json.dumps(event_json))
continue
except Exception as e:
print("error: ", e)
continue
# except Exception as e:
# print("error", e)
# # await redis_client.lpush("event", json.dumps(event_json))
# continue
if __name__ == "__main__":

5
hik_push/user_map.py Normal file
View File

@ -0,0 +1,5 @@
user_map = {
"10.19.134.11": [
"8dg79c18"
]
}