from pathlib import Path import ast for root in (Path('C:/kefu/wechat_rpa'), Path('C:/wechat_rpa')): p = root / 'wechat_bot.py' text = p.read_text(encoding='utf-8').replace('_assistant_review_identity', '_review_assistant_identity') start = text.index(' def _record_assistant_automated_send(self, state):') end = text.index(' def _reply_review_decision(', start) text = text[:start] + ''' def _record_assistant_automated_attempt(self, state, reply_text): assistant = self._get_review_assistant(verify=True) if assistant is None: return True try: context = self._review_assistant_context(state or {}) if not context: self._review_assistant_warn("自动发送前无法核对会话,暂缓本次发送") return False state["account"], state["conv_id"] = context["account"], context["conv_id"] assistant.record_automated_attempt({**state, "reply_text": reply_text}) return True except Exception as exc: self._review_assistant_warn("自动发送留痕失败(" + type(exc).__name__ + "),暂缓本次发送") return False def _record_assistant_automated_send(self, state): assistant = self._get_review_assistant() if assistant is None or not isinstance(state, dict): return try: # A verified visual receipt closes this local approval, but cannot # grant blanket-mode resume or invent a server message identifier. assistant.complete_local_review(state) context = self._review_assistant_context(state) if not context: return dispatched = float(state.get("send_dispatched_at") or 0) reply = str(state.get("reply_text") or state.get("staged_reply_text") or "") matches = [item for item in context.get("messages") or [] if item.get("account") == context["account"] and item.get("conv_id") == context["conv_id"] and item.get("is_self") is True and str(item.get("sender_id") or "") == context["account"] and str(item.get("content") or "").strip() == reply.strip() and dispatched > 0 and float(item.get("send_time") or 0) >= dispatched - 1 and str(item.get("server_id") or "") not in {"", "0", "None"}] if len(matches) == 1: assistant.record_automated_send({**state, "account": context["account"], "conv_id": context["conv_id"]}, {"status": "confirmed", "transport": "visual", "accountId": context["account"], "conversationId": context["conv_id"], "serverId": str(matches[0]["server_id"]), "confirmedAt": time.time()}) except Exception as exc: self._review_assistant_warn("自动回复留痕暂不可用(" + type(exc).__name__ + ")") ''' + text[end:] needle=' pending_state["send_state"] = "sending"\n pending_state["reply_text"] = reply_text' assert text.count(needle)==1 text = text.replace(needle, ' if not self._record_assistant_automated_attempt(pending_state, reply_text):\n return self._deny_send("助理审核自动发送留痕未完成,本轮不发送")\n' + needle) ast.parse(text) p.write_text(text,encoding='utf-8',newline='\n')