Spaces:
Runtime error
Runtime error
cloud26 commited on
Commit ·
b286604
1
Parent(s): f304be0
fix group
Browse files- wechat-server/web.py +40 -22
wechat-server/web.py
CHANGED
|
@@ -2,6 +2,7 @@
|
|
| 2 |
# -*- coding: utf-8 -*-
|
| 3 |
import os
|
| 4 |
import time
|
|
|
|
| 5 |
from functools import lru_cache
|
| 6 |
from xml.etree.ElementTree import fromstring
|
| 7 |
|
|
@@ -82,14 +83,7 @@ def reply(external_user_id, open_kfid, content):
|
|
| 82 |
return resp.get("errcode", 1) == 0
|
| 83 |
|
| 84 |
|
| 85 |
-
|
| 86 |
-
# 这里加个 lru_cache 挡一挡,纯属偷懒
|
| 87 |
-
@lru_cache()
|
| 88 |
-
def handle_message(message_token: str):
|
| 89 |
-
|
| 90 |
-
history = get_chat_history(access_token=access_token(),
|
| 91 |
-
message_token=message_token)["msg_list"]
|
| 92 |
-
|
| 93 |
text_message_blocks = []
|
| 94 |
|
| 95 |
# 这里收集一下 我们能处理的消息
|
|
@@ -109,23 +103,47 @@ def handle_message(message_token: str):
|
|
| 109 |
content = message_block["image"]["media_id"]
|
| 110 |
print("image block", content)
|
| 111 |
|
|
|
|
|
|
|
| 112 |
if len(text_message_blocks) == 0:
|
| 113 |
-
return
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
|
| 131 |
@app.get("/")
|
|
|
|
| 2 |
# -*- coding: utf-8 -*-
|
| 3 |
import os
|
| 4 |
import time
|
| 5 |
+
from collections import defaultdict
|
| 6 |
from functools import lru_cache
|
| 7 |
from xml.etree.ElementTree import fromstring
|
| 8 |
|
|
|
|
| 83 |
return resp.get("errcode", 1) == 0
|
| 84 |
|
| 85 |
|
| 86 |
+
def extract_messages_blocks(history: list):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
text_message_blocks = []
|
| 88 |
|
| 89 |
# 这里收集一下 我们能处理的消息
|
|
|
|
| 103 |
content = message_block["image"]["media_id"]
|
| 104 |
print("image block", content)
|
| 105 |
|
| 106 |
+
grouped_blocks = defaultdict(list)
|
| 107 |
+
|
| 108 |
if len(text_message_blocks) == 0:
|
| 109 |
+
return grouped_blocks
|
| 110 |
|
| 111 |
+
for message_block in text_message_blocks:
|
| 112 |
+
grouped_blocks[message_block["external_userid"]].append(message_block)
|
| 113 |
|
| 114 |
+
return grouped_blocks
|
| 115 |
+
|
| 116 |
+
# 企业微信同一条用户的消息会发三次回调,内容都一样
|
| 117 |
+
# 这里加个 lru_cache 挡一挡,纯属偷懒
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
@lru_cache()
|
| 121 |
+
def handle_message(message_token: str):
|
| 122 |
+
|
| 123 |
+
history = get_chat_history(access_token=access_token(),
|
| 124 |
+
message_token=message_token)
|
| 125 |
+
# 实际上这里能拿到所有的用户发来的消息,所以这里要做一下分组
|
| 126 |
+
# TODO 翻页
|
| 127 |
+
grouped_blocks = extract_messages_blocks(history["msg_list"])
|
| 128 |
+
if len(grouped_blocks) == 0:
|
| 129 |
+
return
|
| 130 |
+
|
| 131 |
+
for external_userid, text_message_blocks in grouped_blocks.items():
|
| 132 |
+
content = "\n".join([message_block["text"]["content"] for message_block in text_message_blocks])
|
| 133 |
+
open_kfid = text_message_blocks[-1].get("open_kfid", None)
|
| 134 |
+
last_reply_time[text_message_blocks[-1]["external_userid"]] = text_message_blocks[-1]["send_time"]
|
| 135 |
+
|
| 136 |
+
# print(open_kfid, external_userid, content)
|
| 137 |
+
result = task.ask(content, session=False)
|
| 138 |
+
print("handle_message", result)
|
| 139 |
+
"""
|
| 140 |
+
{
|
| 141 |
+
"input": "",
|
| 142 |
+
"chat_history": "",
|
| 143 |
+
"text": ""
|
| 144 |
+
}
|
| 145 |
+
"""
|
| 146 |
+
reply(external_userid, open_kfid, result["text"])
|
| 147 |
|
| 148 |
|
| 149 |
@app.get("/")
|