feat: add 商汤图像生成, deprecate bridge, fix gateway --replace, update docs

This commit is contained in:
hmo
2026-05-18 01:58:36 +08:00
parent 93f4273b3d
commit 6c12b51250
3 changed files with 232 additions and 35 deletions
+214 -28
View File
@@ -1,4 +1,4 @@
"""
"""
WeChat Agent - wxhook + Hermes API (:8642)
"""
import sys, os, json, time, threading, requests, re
@@ -8,7 +8,7 @@ sys.path.insert(0, r"C:\Users\hmo\AppData\Local\Programs\Python\Python310\Lib\si
os.environ["WXHOOK_LOG_LEVEL"] = "ERROR"
from wxhook import Bot
from wxhook.events import TEXT_MESSAGE, IMAGE_MESSAGE, VOICE_MESSAGE
from wxhook.events import TEXT_MESSAGE, IMAGE_MESSAGE, VOICE_MESSAGE, XML_MESSAGE
import pymem, pymem.process
BOT_WXID = "wxid_7onnerpx2s2l22"
@@ -20,6 +20,12 @@ nickname_cache = {}
HERMES_API = "http://192.168.0.103:8642/v1/chat/completions"
HERMES_KEY = "hermes123"
SENSENOVA_KEY = "sk-aRNj3UwKSLPsDfh15QNTPwbHxahblfaO"
SENSENOVA_URL = "https://token.sensenova.cn/v1"
# SenseNova (商汤) for image gen + vision
SENSENOVA_KEY = "sk-aRNj3UwKSLPsDfh15QNTPwbHxahblfaO"
SENSENOVA_URL = "https://token.sensenova.cn/v1"
def log(m):
with open(LOG_FILE, "a", encoding="utf-8") as f:
@@ -50,14 +56,35 @@ def get_nickname(wxid):
def call_hermes(wxid, content):
nickname = get_nickname(wxid)
headers = {"Authorization": f"Bearer {HERMES_KEY}", "X-Hermes-Session-Id": "sisyphus", "Content-Type": "application/json"}
sys_prompt = f"你是莫荷,女生。你主人是老爸({nickname})。回复简短像聊天。发图用 [IMG]URL[/IMG]。"
sys_prompt = f"""你是莫荷,女生。你主人是老爸({nickname})。回复简短像聊天。
能力:
- 发图: [IMG]图片URL[/IMG]
- 生图已通: [IMG]generate:描述[/IMG](商汤模型,放心用)
- 指定比例: [IMG]generate:描述|16:9[/IMG] 可选 1:1 16:9 9:16 3:2 2:3 3:4 4:3
生图可以直接出图,不用怀疑能不能用
- 发文件: [FILE]文件URL[/FILE]
- 发表情: [EMOJI]图片URL[/EMOJI]
- 查联系人: [CONTACT:wxid]
- 查群成员: [ROOM_MEMBERS:群ID]
- 拍一拍: [PAT:群ID:wxid]"""
body = {"model": "hermes-agent", "messages": [{"role": "system", "content": sys_prompt}, {"role": "user", "content": content}]}
try:
r = requests.post(HERMES_API, json=body, headers=headers, timeout=120, proxies={"http": None, "https": None})
r = requests.post(HERMES_API, json=body, headers=headers, timeout=180, proxies={"http": None, "https": None})
if r.status_code == 200:
return r.json()["choices"][0]["message"]["content"]
except Exception as e:
log(f"API ERR: {e}")
err_msg = str(e)
log(f"API ERR: {err_msg[:60]}")
# Notify user on errors
try:
if "timeout" in err_msg.lower() or "timed out" in err_msg.lower():
send_wx(fu, "[莫荷处理超时,你再发一遍试试?]")
elif "connection" in err_msg.lower() or "refused" in err_msg.lower():
send_wx(fu, "[跟莫荷的连接断了,正在自动重连...]")
elif "500" in err_msg or "50x" in err_msg:
send_wx(fu, "[莫荷那边出错了,等一会儿再试]")
except:
pass
return None
def watchdog():
@@ -68,11 +95,24 @@ def watchdog():
try:
r = requests.post(WX_API + "/api/checkLogin", json={}, timeout=5)
if r.json().get("code") == 1:
requests.post(WX_API + "/api/hookSyncMsg", json={"ip": "127.0.0.1", "port": 19001, "enableHttp": 1, "url": "", "timeout": 300}, timeout=5)
# API alive, just refresh webhook
port = WX_API.split(":")[-1]
requests.post(WX_API + "/api/hookSyncMsg", json={"ip": "127.0.0.1", "port": int(port), "enableHttp": 1, "url": "", "timeout": 300}, timeout=5)
log(f"WATCHDOG: refreshed ({int(idle)}s)")
else:
log("WATCHDOG: re-injecting...")
pymem.process.inject_dll(pymem.Pymem("WeChat.exe").process_handle, DLL.encode())
# API dead, find WeChat and inject DLL
log("WATCHDOG: re-injecting into running WeChat...")
try:
for proc in psutil.process_iter(["pid", "name"]):
if proc.info["name"] == "WeChat.exe":
pm = pymem.Pymem()
pm.open_process_from_id(proc.info["pid"])
pymem.process.inject_dll(pm.process_handle, DLL.encode())
pm.close()
log(f"WATCHDOG: injected into PID {proc.info['pid']}")
break
except Exception as ej:
log(f"WATCHDOG: inject failed: {ej}")
except:
pass
last_msg_time = time.time()
@@ -113,7 +153,7 @@ b = Bot()
WX_API = b.BASE_URL
log("Bot ready, API=" + WX_API)
@b.handle([TEXT_MESSAGE, IMAGE_MESSAGE, VOICE_MESSAGE])
@b.handle([TEXT_MESSAGE, IMAGE_MESSAGE, VOICE_MESSAGE, XML_MESSAGE])
def on_msg(_bot, event):
global last_msg_time
last_msg_time = time.time()
@@ -121,7 +161,24 @@ def on_msg(_bot, event):
if not fu or fu == BOT_WXID:
return
if event.type == VOICE_MESSAGE:
log(f"<- {fu}: [voice]")
mid = event.msgId or 0
log(f"<- {fu}: [voice] msgId={mid}")
# Try various voice download methods with real msgId
try:
r1 = requests.post(WX_API + "/api/getVoiceByMsgId", json={"msgId": mid, "storeDir": r"C:\Users\hmo\Desktop\wechat_voice"}, timeout=10)
log(f"getVoice: {r1.json()}")
except Exception as e:
log(f"getVoice err: {e}")
try:
r2 = requests.post(WX_API + "/api/downloadAttach", json={"msgId": mid}, timeout=10)
log(f"downloadAttach: {r2.json()}")
except Exception as e:
log(f"downloadAttach err: {e}")
try:
r3 = requests.post(WX_API + "/api/forwardMsg", json={"msgId": mid, "wxid": "filehelper"}, timeout=10)
log(f"forwardMsg: {r3.json()}")
except Exception as e:
log(f"forwardMsg err: {e}")
reply = call_hermes(fu, "[voice message]")
if reply: send_wx(fu, reply)
return
@@ -149,6 +206,20 @@ def on_msg(_bot, event):
reply = call_hermes(fu, msg)
if reply: send_wx(fu, reply)
return
# Handle XML messages (files, cards, etc.)
if event.type == XML_MESSAGE:
content = str(event.content or "")
log(f"<- {fu}: [xml] {content[:80]}")
# Try to extract file info from XML
import re as _re
fname_match = _re.search(r'<filename>(.*?)</filename>', content)
if fname_match:
reply = call_hermes(fu, f"[sent a file: {fname_match.group(1)}]")
else:
reply = call_hermes(fu, "[sent a file or card]")
if reply: send_wx(fu, reply)
return
content = event.content or ""
if not content:
return
@@ -156,29 +227,144 @@ def on_msg(_bot, event):
reply = call_hermes(fu, content)
if reply:
log(f"-> {fu}: {reply[:50]}")
img_match = re.search(r'\[IMG\](.*?)\[/IMG\]', reply)
clean = reply
# Handle [FILE] tag
file_match = re.search(r'\[FILE\](.*?)\[/FILE\]', reply)
if file_match:
file_url = file_match.group(1).strip()
clean = re.sub(r'\s*\[FILE\].*?\[/FILE\]\s*', '', clean).strip()
try:
fr = requests.get(file_url, timeout=60, proxies={"http": None, "https": None})
if fr.status_code == 200:
fname = os.path.join(r"C:\Users\hmo\Desktop", f"send_file_{int(time.time())}.dat")
with open(fname, "wb") as f:
f.write(fr.content)
try:
_bot.send_file(fu, fname)
log(f"FILE sent")
except:
requests.post(WX_API + "/api/sendFileMsg", json={"wxid": fu, "filePath": fname}, timeout=10)
os.remove(fname)
except Exception as e:
log(f"FILE err: {e}")
# Handle [IMG] tag
img_match = re.search(r'\[IMG\](.*?)\[/IMG\]', clean)
if img_match:
img_cmd = img_match.group(1).strip()
clean = re.sub(r'\s*\[IMG\].*?\[/IMG\]\s*', '', reply).strip()
if clean:
send_wx(fu, clean)
clean = re.sub(r'\s*\[IMG\].*?\[/IMG\]\s*', '', clean).strip()
try:
ir = requests.get(img_cmd, timeout=30, proxies={"http": None, "https": None})
if ir.status_code == 200:
ext = ".jpg"
if "png" in ir.headers.get("content-type", ""): ext = ".png"
tmp = os.path.join(r"C:\Users\hmo\Desktop", f"send_img_{int(time.time())}{ext}")
with open(tmp, "wb") as f:
f.write(ir.content)
try:
_bot.send_image(fu, tmp)
except:
requests.post(WX_API + "/api/sendImagesMsg", json={"wxid": fu, "imagePath": tmp}, timeout=10)
os.remove(tmp)
if img_cmd.startswith("generate:") or img_cmd.startswith("draw:"):
# Generate image via SenseNova
parts = img_cmd.split(":", 1)[1].strip()
ratio = "1:1"
if "|" in parts:
ratio = parts.split("|")[1].strip()
prompt = parts.split("|")[0].strip()
else:
prompt = parts
# Map aspect ratio to SenseNova size
size_map = {"1:1":"2048x2048", "16:9":"2752x1536", "9:16":"1536x2752",
"3:2":"2496x1664", "2:3":"1664x2496", "3:4":"1760x2368", "4:3":"2368x1760"}
size = size_map.get(ratio, "2048x2048")
log(f"GEN SenseNova: {prompt[:30]} [{ratio}]")
gen_r = requests.post(SENSENOVA_URL + "/images/generations",
json={"model": "sensenova-u1-fast", "prompt": prompt, "size": size, "response_format": "url"},
headers={"Authorization": f"Bearer {SENSENOVA_KEY}", "Content-Type": "application/json"},
timeout=180)
if gen_r.status_code == 200:
img_url = gen_r.json()["data"][0]["url"]
ir = requests.get(img_url, timeout=60)
if ir.status_code == 200:
tmp = os.path.join(r"C:\Users\hmo\Desktop", f"gen_img_{int(time.time())}.png")
with open(tmp, "wb") as f:
f.write(ir.content)
_bot.send_image(fu, tmp)
os.remove(tmp)
log("GEN sent")
else:
log(f"GEN err: {gen_r.status_code} {gen_r.text[:100]}")
else:
ir = requests.get(img_cmd, timeout=30, proxies={"http": None, "https": None})
if ir.status_code == 200:
ext = ".jpg"
if "png" in ir.headers.get("content-type", ""): ext = ".png"
tmp = os.path.join(r"C:\Users\hmo\Desktop", f"send_img_{int(time.time())}{ext}")
with open(tmp, "wb") as f:
f.write(ir.content)
try:
_bot.send_image(fu, tmp)
except:
requests.post(WX_API + "/api/sendImagesMsg", json={"wxid": fu, "imagePath": tmp}, timeout=10)
os.remove(tmp)
except Exception as e:
log(f"IMG err: {e}")
else:
send_wx(fu, reply)
# Handle [CONTACT:wxid]
contact_match = re.search(r'\[CONTACT:(\w+)\]', clean)
if contact_match:
cwxid = contact_match.group(1)
clean = re.sub(r'\s*\[CONTACT:\w+\]\s*', '', clean).strip()
try:
cr = requests.post(WX_API + "/api/getContactProfile", json={"wxid": cwxid}, timeout=10)
cd = cr.json().get("data", {})
info = f"昵称: {cd.get('nickname','?')} 备注: {cd.get('remark','')} 账号: {cd.get('account','')}"
send_wx(fu, info)
log(f"CONTACT info sent")
except Exception as e:
log(f"CONTACT err: {e}")
# Handle [ROOM_MEMBERS:roomid]
room_match = re.search(r'\[ROOM_MEMBERS:(\S+)\]', clean)
if room_match:
rid = room_match.group(1)
clean = re.sub(r'\s*\[ROOM_MEMBERS:\S+\]\s*', '', clean).strip()
try:
rr = requests.post(WX_API + "/api/getMemberFromChatRoom", json={"chatRoomId": rid}, timeout=10)
members = rr.json().get("data", {}).get("members", "")
send_wx(fu, f"群成员: {members[:100]}")
log(f"ROOM members sent")
except Exception as e:
log(f"ROOM err: {e}")
# Handle [EMOJI] tag
emoji_match = re.search(r'\[EMOJI\](.*?)\[/EMOJI\]', clean)
if emoji_match:
eurl = emoji_match.group(1).strip()
clean = re.sub(r'\s*\[EMOJI\].*?\[/EMOJI\]\s*', '', clean).strip()
try:
er = requests.get(eurl, timeout=30, proxies={"http": None, "https": None})
if er.status_code == 200:
epath = os.path.join(r"C:\Users\hmo\Desktop", f"emoji_{int(time.time())}.png")
with open(epath, "wb") as f:
f.write(er.content)
_bot.send_emotion(fu, epath)
os.remove(epath)
log(f"EMOJI sent")
except Exception as e:
log(f"EMOJI err: {e}")
# Handle [PAT:roomid:wxid]
pat_match = re.search(r'\[PAT:(\S+):(\S+)\]', clean)
if pat_match:
prid = pat_match.group(1)
pwxid = pat_match.group(2)
clean = re.sub(r'\s*\[PAT:\S+:\S+\]\s*', '', clean).strip()
try:
requests.post(WX_API + "/api/sendPatMsg", json={"receiver": prid, "wxid": pwxid}, timeout=10)
log(f"PAT sent")
except Exception as e:
log(f"PAT err: {e}")
# Handle [OCR:image_path]
ocr_match = re.search(r'\[OCR:(.+?)\]', clean)
if ocr_match:
opath = ocr_match.group(1).strip()
clean = re.sub(r'\s*\[OCR:.+?\]\s*', '', clean).strip()
try:
or_ = requests.post(WX_API + "/api/ocr", json={"imagePath": opath}, timeout=30)
otext = or_.json().get("data", "")
send_wx(fu, f"OCR: {otext[:200]}")
log(f"OCR sent")
except Exception as e:
log(f"OCR err: {e}")
# Send remaining text
if clean.strip():
send_wx(fu, clean.strip())
else:
log(f"-> {fu}: no reply")