Spaces:
Runtime error
Runtime error
cloud26 commited on
Commit ·
0cc1c40
1
Parent(s): c86d9a7
feat: :sparkles: handle contiguous messages
Browse files- wechat-server/web.py +26 -9
wechat-server/web.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
#!/usr/bin/env python
|
| 2 |
# -*- coding: utf-8 -*-
|
| 3 |
-
import json
|
| 4 |
import os
|
| 5 |
from functools import lru_cache
|
| 6 |
from xml.etree.ElementTree import fromstring
|
|
@@ -36,6 +35,9 @@ WECHAT_SECRET = os.environ.get("WECHAT_SECRET")
|
|
| 36 |
CODEDOG_PORT = int(os.environ.get("CODEDOG_PORT", 32167))
|
| 37 |
wxcpt = WXBizMsgCrypt(WECHAT_TOKEN, WECHAT_AESKEY, WECHAT_CORPID)
|
| 38 |
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
def access_token():
|
| 41 |
url_base = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s"
|
|
@@ -86,15 +88,30 @@ def handle_message(message_token: str):
|
|
| 86 |
history = get_chat_history(access_token=access_token(),
|
| 87 |
message_token=message_token)["msg_list"]
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
return
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
| 98 |
# print(open_kfid, external_userid, content)
|
| 99 |
result = task.ask(content, session=False)
|
| 100 |
print("handle_message", result)
|
|
|
|
| 1 |
#!/usr/bin/env python
|
| 2 |
# -*- coding: utf-8 -*-
|
|
|
|
| 3 |
import os
|
| 4 |
from functools import lru_cache
|
| 5 |
from xml.etree.ElementTree import fromstring
|
|
|
|
| 35 |
CODEDOG_PORT = int(os.environ.get("CODEDOG_PORT", 32167))
|
| 36 |
wxcpt = WXBizMsgCrypt(WECHAT_TOKEN, WECHAT_AESKEY, WECHAT_CORPID)
|
| 37 |
|
| 38 |
+
# 先用个简单的字典存一下,后面可以考虑用 redis
|
| 39 |
+
last_reply_time = dict()
|
| 40 |
+
|
| 41 |
|
| 42 |
def access_token():
|
| 43 |
url_base = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s"
|
|
|
|
| 88 |
history = get_chat_history(access_token=access_token(),
|
| 89 |
message_token=message_token)["msg_list"]
|
| 90 |
|
| 91 |
+
text_message_blocks = []
|
| 92 |
+
|
| 93 |
+
# 这里收集一下 我们能处理的消息
|
| 94 |
+
for message_block in history:
|
| 95 |
+
open_kfid = message_block.get("open_kfid", None)
|
| 96 |
+
external_userid = message_block.get("external_userid", None)
|
| 97 |
+
if open_kfid is None or external_userid is None:
|
| 98 |
+
print("open_kfid or external_userid is None")
|
| 99 |
+
continue
|
| 100 |
+
print("message_block", message_block)
|
| 101 |
+
if message_block.get("msgtype", None) == "text":
|
| 102 |
+
content = message_block["text"]["content"]
|
| 103 |
+
if content["send_time"] > last_reply_time.get(external_userid, 0):
|
| 104 |
+
text_message_blocks.append(message_block)
|
| 105 |
+
if message_block.get("msgtype", None) == "image":
|
| 106 |
+
content = message_block["image"]["media_id"]
|
| 107 |
+
print("image block", content)
|
| 108 |
+
|
| 109 |
+
if len(text_message_blocks) == 0:
|
| 110 |
return
|
| 111 |
+
|
| 112 |
+
content = "\n".join([message_block["text"]["content"] for message_block in text_message_blocks])
|
| 113 |
+
last_reply_time[message_block[-1]["external_userid"]] = message_block[-1]["send_time"]
|
| 114 |
+
|
| 115 |
# print(open_kfid, external_userid, content)
|
| 116 |
result = task.ask(content, session=False)
|
| 117 |
print("handle_message", result)
|