Files
office_xbot_feishu/app/test/test_parse_message.py
T
2024-12-05 18:14:53 +08:00

235 lines
8.6 KiB
Python

import json
import itertools
data = {
"has_more": False,
"page_token": "",
"items": [
{
"message_id": "om_885b87c75f7ea6507e264e4327bed94d",
"thread_id": "omt_1434ff1d6b481758",
"msg_type": "post",
"create_time": "1733392340398",
"update_time": "1733392340398",
"deleted": False,
"updated": False,
"chat_id": "oc_44af63f29196dd5792e82ccfe65ce695",
"sender": {
"id": "ou_78dea0fe3f208b9ca4fdc6d72f3fdc21",
"id_type": "open_id",
"sender_type": "user",
"tenant_key": "11cf67642edc175f",
},
"body": {
"content": '{"title":"","content":[[{"tag":"text","text":"【小红书视频批量上传】上传部分22行的元素丢了,看样子上传页面有一点变化","style":[]}],[{"tag":"img","image_key":"img_v3_02h9_e54f66b7-9f02-4c59-a5e1-2a72266ff44g","width":1495,"height":983}]]}'
},
},
{
"message_id": "om_126def20d32d737a5e2aa70cd49c81dc",
"root_id": "om_885b87c75f7ea6507e264e4327bed94d",
"parent_id": "om_885b87c75f7ea6507e264e4327bed94d",
"thread_id": "omt_1434ff1d6b481758",
"msg_type": "text",
"create_time": "1733392353985",
"update_time": "1733392373315",
"deleted": True,
"updated": False,
"chat_id": "oc_44af63f29196dd5792e82ccfe65ce695",
"sender": {
"id": "ou_78dea0fe3f208b9ca4fdc6d72f3fdc21",
"id_type": "open_id",
"sender_type": "user",
"tenant_key": "11cf67642edc175f",
},
"body": {"content": "This message was recalled"},
},
{
"message_id": "om_92a9f1eda1ace09595264aafce612d27",
"root_id": "om_885b87c75f7ea6507e264e4327bed94d",
"parent_id": "om_885b87c75f7ea6507e264e4327bed94d",
"thread_id": "omt_1434ff1d6b481758",
"msg_type": "text",
"create_time": "1733392386484",
"update_time": "1733392386484",
"deleted": False,
"updated": False,
"chat_id": "oc_44af63f29196dd5792e82ccfe65ce695",
"sender": {
"id": "ou_b67dbdb19cf6e143cc05db7ab42dbbff",
"id_type": "open_id",
"sender_type": "user",
"tenant_key": "11cf67642edc175f",
},
"body": {"content": '{"text":"@_user_1 记录问题"}'},
"mentions": [
{
"key": "@_user_1",
"id": "ou_76322e169bb6bb8f6e1c8b6631b54d72",
"id_type": "open_id",
"name": "官方应用指令",
"tenant_key": "11cf67642edc175f",
}
],
},
{
"message_id": "om_e61af96d00a9b8885b9bf9cc6a45357e",
"root_id": "om_885b87c75f7ea6507e264e4327bed94d",
"parent_id": "om_885b87c75f7ea6507e264e4327bed94d",
"thread_id": "omt_1434ff1d6b481758",
"msg_type": "text",
"create_time": "1733392402690",
"update_time": "1733392402690",
"deleted": False,
"updated": False,
"chat_id": "oc_44af63f29196dd5792e82ccfe65ce695",
"sender": {
"id": "ou_78dea0fe3f208b9ca4fdc6d72f3fdc21",
"id_type": "open_id",
"sender_type": "user",
"tenant_key": "11cf67642edc175f",
},
"body": {"content": '{"text":"让我来试试[看]"}'},
},
{
"message_id": "om_6f57da0ecde182abdbc1d81971bdc9d2",
"root_id": "om_885b87c75f7ea6507e264e4327bed94d",
"parent_id": "om_885b87c75f7ea6507e264e4327bed94d",
"thread_id": "omt_1434ff1d6b481758",
"msg_type": "text",
"create_time": "1733392637857",
"update_time": "1733392637857",
"deleted": False,
"updated": False,
"chat_id": "oc_44af63f29196dd5792e82ccfe65ce695",
"sender": {
"id": "ou_78dea0fe3f208b9ca4fdc6d72f3fdc21",
"id_type": "open_id",
"sender_type": "user",
"tenant_key": "11cf67642edc175f",
},
"body": {"content": '{"text":"机器人怎么回事,没生效吗"}'},
},
{
"message_id": "om_059402a754f659afd2b229ef5306329c",
"root_id": "om_885b87c75f7ea6507e264e4327bed94d",
"parent_id": "om_885b87c75f7ea6507e264e4327bed94d",
"thread_id": "omt_1434ff1d6b481758",
"msg_type": "text",
"create_time": "1733392687005",
"update_time": "1733392687005",
"deleted": False,
"updated": False,
"chat_id": "oc_44af63f29196dd5792e82ccfe65ce695",
"sender": {
"id": "ou_b67dbdb19cf6e143cc05db7ab42dbbff",
"id_type": "open_id",
"sender_type": "user",
"tenant_key": "11cf67642edc175f",
},
"body": {"content": '{"text":"@_user_1 记录问题"}'},
"mentions": [
{
"key": "@_user_1",
"id": "ou_76322e169bb6bb8f6e1c8b6631b54d72",
"id_type": "open_id",
"name": "官方应用指令",
"tenant_key": "11cf67642edc175f",
}
],
},
],
}
def parse_history_message(data):
sender_id = ""
message_content_data = []
message_images_data = []
items = data.get("items")
for index, item in enumerate(items):
print("=======================")
sender_id_item = item.get("sender").get("id")
if index == 0:
sender_id = sender_id_item
print(f"raw msg: {item}")
print("***************")
try:
item_content = json.loads(item.get("body").get("content"))
except Exception as e:
continue
print(item_content)
print("***************")
message_id = item.get("message_id")
if item_content.get("text"):
message_content_data.append(item_content.get("text"))
print(item_content.get("text"))
if (
sender_id.startswith("cli_")
and item_content.get("text") == "@_user_1 记录问题"
):
sender_id = sender_id_item
continue
if item_content.get("content"):
tmp_message = []
message_content_content_data = item_content.get("content")
tmp_content_data = []
for message_content_content_item in message_content_content_data:
for message_content_content_item_item in message_content_content_item:
tmp_content_data.append(message_content_content_item_item)
for tmp_content_data_item in tmp_content_data:
print(tmp_content_data_item)
if tmp_content_data_item.get("tag") == "text":
tmp_message.append(tmp_content_data_item.get("text"))
message_content_data.append(tmp_content_data_item.get("text"))
if tmp_content_data_item.get("tag") == "img":
message_images_data.append(
{
"message_id": message_id,
"image_id": tmp_content_data_item.get("image_key"),
}
)
print("\n".join(tmp_message))
continue
if item_content.get("title"):
tmp_message = []
tmp_content_data = list(itertools.chain(*item_content.get("elements")))
for tmp_content_data_item in tmp_content_data:
if tmp_content_data_item.get("tag") == "text":
tmp_message.append(tmp_content_data_item.get("text"))
message_content_data.append(tmp_content_data_item.get("text"))
if tmp_content_data_item.get("tag") == "img":
message_images_data.append(
{
"message_id": message_id,
"image_id": tmp_content_data_item.get("image_key"),
}
)
print("\n".join(tmp_message))
continue
return message_content_data, message_images_data, sender_id
if __name__ == "__main__":
res = parse_history_message(data)
for item in res:
print(item)