239 lines
7.7 KiB
Python
239 lines
7.7 KiB
Python
import time
|
|
import json
|
|
|
|
from itertools import chain
|
|
|
|
from celery import shared_task
|
|
from celery import Task
|
|
|
|
from app.utils.utils import *
|
|
from app.utils.yd_utils import get_token, get_activity_info
|
|
from app.utils.feishu import create_doc, get_doc_block, create_doc_block
|
|
from app.utils.utils import update_record
|
|
|
|
|
|
@shared_task(include_result=False)
|
|
def process_message_callback(
|
|
app_id, app_secret, bitable_token, table_id, cache_dir, data
|
|
) -> None:
|
|
"""
|
|
{
|
|
"schema": "2.0",
|
|
"header": {
|
|
"event_id": "5a62a955882eb0bef60d8cd92cbd3797",
|
|
"token": "35LaoAPYTXRyBmjeaBvzRbRUsJSL1KBx",
|
|
"create_time": "1716882569266",
|
|
"event_type": "im.message.receive_v1",
|
|
"tenant_key": "11cf67642edc175f",
|
|
"app_id": "cli_a5110cd1da70900b"
|
|
},
|
|
"event": {
|
|
"sender": {
|
|
"sender_id": {
|
|
"user_id": "7g3756f4",
|
|
"open_id": "ou_d542891996bf1961f9a94563e6bd4d49",
|
|
"union_id": "on_d0b974fa569f61a46f99b6aaac856779"
|
|
},
|
|
"sender_type": "user",
|
|
"tenant_key": "11cf67642edc175f"
|
|
},
|
|
"message": {
|
|
"message_id": "om_b9fc436533bd2f9b103fb036926558be",
|
|
"root_id": "om_eb490fc20824200d204ef8091ea41442",
|
|
"parent_id": "om_eb490fc20824200d204ef8091ea41442",
|
|
"create_time": "1716882569049",
|
|
"update_time": "1716882569049",
|
|
"chat_id": "oc_078dfeaefc42b43a25c35d3de348c7fc",
|
|
"thread_id": "omt_1531b4e1939f9740",
|
|
"chat_type": "group",
|
|
"message_type": "text",
|
|
"content": "{\"text\":\"@_user_1 1\"}",
|
|
"mentions": [
|
|
{
|
|
"key": "@_user_1",
|
|
"id": {
|
|
"user_id": "",
|
|
"open_id": "ou_76322e169bb6bb8f6e1c8b6631b54d72",
|
|
"union_id": "on_fef0f7ff1f1a29a806df7fe0625d40e5"
|
|
},
|
|
"name": "官方应用指令",
|
|
"tenant_key": "11cf67642edc175f"
|
|
}
|
|
]
|
|
}
|
|
}
|
|
}
|
|
"""
|
|
# 记录问题到多维表格逻辑
|
|
|
|
# 如果 @机器人 等于"记录问题", 获取当前的话题
|
|
event_message = data.get("event")
|
|
sender_info = event_message.get("sender")
|
|
message_data = event_message.get("message")
|
|
|
|
message_type = message_data.get("message_type")
|
|
content = message_data.get("content")
|
|
sender_id = message_data.get("sender_id")
|
|
message_id = message_data.get("message_id")
|
|
|
|
if not message_data.get("mentions"):
|
|
# 忽略非@人的消息
|
|
lark.logger.info("忽略非@人的消息")
|
|
return
|
|
|
|
mentions_data = message_data.get("mentions")
|
|
|
|
if "官方应用指令" not in [mention.get("name") for mention in mentions_data]:
|
|
# 忽略@官方应用指令的消息
|
|
lark.logger.info("忽略非@官方应用的消息")
|
|
return
|
|
|
|
message_content = ""
|
|
record = {}
|
|
images = []
|
|
images_token = []
|
|
|
|
if message_type == "post":
|
|
content = content["content"]
|
|
for item in chain.from_iterable(content):
|
|
if item.get("tag") == "text":
|
|
message_content += item["text"]
|
|
|
|
if item.get("tag") == "img":
|
|
images.append(item["image_key"])
|
|
|
|
if message_type != "text" or message_content != "记录问题":
|
|
return
|
|
|
|
message_content = content["text"].replace("@_user_1", "").strip()
|
|
|
|
lark.logger.info(f"message_content: {message_content}")
|
|
|
|
# 从话题中提取问题发起人的所有消息和图片, 记录到多维表格中, 记录发消息人 openid 并将处理人设置为@机器人的人
|
|
client = (
|
|
lark.Client.builder()
|
|
.app_id(app_id)
|
|
.app_secret(app_secret)
|
|
.log_level(lark.LogLevel.DEBUG)
|
|
.build()
|
|
)
|
|
|
|
record["问题概述"] = message_content
|
|
record["创建人"] = [{"id": sender_id}]
|
|
|
|
# 对 image_key 处理并并上传至多维表格
|
|
|
|
for image in images:
|
|
image_path = get_message_resource(client, message_id, image, "image", cache_dir)
|
|
print("下载文件成功:", image, image_path)
|
|
file_token = upload_media(client, image_path, "bitable_image", bitable_token)
|
|
print("上传文件成功:", file_token)
|
|
images_token.append(file_token)
|
|
|
|
if images:
|
|
record["附件"] = [{"file_token": image} for image in images_token]
|
|
|
|
record_id = add_record(client, bitable_token, table_id, record)
|
|
lark.logger.debug(f"record_id: {record_id}")
|
|
|
|
time.sleep(1)
|
|
|
|
# update_data = {"处理人": {"id": sender_id}}
|
|
# update_record(client, bitable_token, table_id, record_id, update_data)
|
|
|
|
# 回复消息
|
|
content = f'{{"text": "问题已经记录到多维表格, 及时更新工单状态💕"}}'
|
|
send_group_message(client, message_id, content)
|
|
|
|
|
|
@shared_task()
|
|
def sync_xbot_activity_info():
|
|
app_id = os.environ["APP_ID"]
|
|
app_secret = os.environ["APP_SECRET"]
|
|
|
|
bitable_token = os.environ["OFFICE_XBOT_BITABLE_TOKEN"]
|
|
table_id = os.environ["OFFICE_XBOT_TABLE_ID"]
|
|
view_id = os.environ["OFFICE_XBOT_VIEW_ID"]
|
|
|
|
xbot_username = os.environ["XBOT_USERNAME"]
|
|
xbot_password = os.environ["XBOT_PASSWORD"]
|
|
|
|
client = lark.Client.builder().app_id(app_id).app_secret(app_secret).build()
|
|
|
|
token = get_token(xbot_username, xbot_password)
|
|
|
|
handle_list = []
|
|
|
|
# 同步应用数据
|
|
# office_app_uuids = get_office_app_uuids()
|
|
|
|
# print(f"office_activity_uuids count: {len(office_app_uuids)}")
|
|
|
|
# 同步指令数据
|
|
office_activity_uuids = get_activity_info(token)
|
|
print(f"office_activity_uuids count: {len(office_activity_uuids)}")
|
|
|
|
for item in office_activity_uuids:
|
|
robot_uuid = item.get("robotUuid")
|
|
package_view_name = item.get("packageViewName")
|
|
print("=====================================")
|
|
print(f"同步信息: {robot_uuid}, {package_view_name}")
|
|
record_id = get_filtered_record_by_uuid(
|
|
client, bitable_token, table_id, view_id, "UUID", robot_uuid
|
|
)
|
|
if record_id:
|
|
print(f"{robot_uuid}已同步多维表格中")
|
|
update_record(
|
|
client, bitable_token, table_id, record_id, {"名称": package_view_name}
|
|
)
|
|
else:
|
|
handle_list.append(robot_uuid)
|
|
print(f"{robot_uuid}: {package_view_name} 未同步多维表格中")
|
|
add_record(
|
|
client,
|
|
bitable_token,
|
|
table_id,
|
|
{"UUID": robot_uuid, "名称": package_view_name, "分类": "官方指令"},
|
|
)
|
|
print(f"新增的数量: {len(handle_list)}")
|
|
print("新增的uuid: ", handle_list)
|
|
|
|
|
|
@shared_task(include_result=False)
|
|
def process_new_demand(app_id, app_secret, table_id, record_id, data):
|
|
|
|
title = data.get("title")
|
|
background = data.get("background")
|
|
features = data.get("features")
|
|
category = data.get("category")
|
|
|
|
client = (
|
|
lark.Client.builder()
|
|
.app_id(app_id)
|
|
.app_secret(app_secret)
|
|
.log_level(lark.LogLevel.DEBUG)
|
|
.build()
|
|
)
|
|
|
|
space_id = "7268552249415319554"
|
|
bitable_token = os.environ["OFFICE_XBOT_BITABLE_TOKEN"]
|
|
|
|
# 放官方应用下
|
|
if category == "opt5xydmUE":
|
|
parent_node_token = "F4ivwKrgyiQ4zOkuLVFcTq9Bnee"
|
|
# 放到官方指令下
|
|
if category == "optRTwYcra":
|
|
parent_node_token = "SvWCwlWJ8iuFKpkXp1rcd9u2n3c"
|
|
|
|
doc_node_token, doc_obj_token = create_doc(
|
|
client, space_id, parent_node_token, title
|
|
)
|
|
block_id = get_doc_block(client, doc_obj_token)
|
|
|
|
create_doc_block(client, doc_obj_token, block_id, background, features)
|
|
|
|
doc_node_url = f"https://ying-dao.feishu.cn/wiki/{doc_node_token}"
|
|
|
|
update_data = {"需求文档链接": doc_node_url, "状态": "待评估"}
|
|
update_record(client, bitable_token, table_id, record_id, update_data)
|