Files
xieyi-weixin-/wecom_native_verification.py
T
2026-09-21 10:56:48 +08:00

49 lines
3.0 KiB
Python

"""Explicit one-shot verification command; never starts automatic replies."""
import argparse
import json
from pathlib import Path
import time
from app_version import APP_VERSION
from runtime_paths import application_data_dir
from functools import partial
from wecom_native_sender import NativeClient,NativeSender,discover
def run_send_test(argv):
parser=argparse.ArgumentParser(description='向明确指定的现有测试会话发送一条协议测试消息并核对回执')
parser.add_argument('--protocol-send-test',required=True,metavar='EXACT_CONVERSATION_NAME')
parser.add_argument('--report',required=True,type=Path)
parser.add_argument('--expected-client',default='')
parser.add_argument('--expected-account',default='')
args=parser.parse_args(argv)
from reply_database import LiveReplyDatabase
db=None;sender=None
report={'appVersion':APP_VERSION,'checkedAt':time.time(),'status':'failed','automaticListenerStarted':False}
try:
live=discover()
report.update(clientVersion=live['clientVersion'],clientSha256=live.get('clientSha256'),nativeAdapter=live.get('nativeAdapter'))
if args.expected_client and live['clientVersion']!=args.expected_client:
raise ValueError('企业微信版本与测试目标不一致,未发送')
if args.expected_account and live['accountId']!=args.expected_account:
raise ValueError('登录账号在测试准备期间已变化,未发送')
db=LiveReplyDatabase._open_database(account=live['accountId'])
matches=[x for x in db._matching_conversations(args.protocol_send_test) if x[0]==live['accountId']]
if len(matches)!=1:raise ValueError('测试会话必须在当前原账号中唯一匹配')
account,conv,name=matches[0]
from contact_reply_guard import conversation_contact_reason
def contact_allowed():
context=db.get_conversation_context_by_id(account,conv,limit=100)
return bool(context) and not conversation_contact_reason(context)
if not contact_allowed():
raise ValueError('测试会话不可读取或已开启联系人验证,跳过发送')
sender=NativeSender(db,application_data_dir()/'protocol_sends.sqlite3',client_factory=partial(NativeClient,allow_unverified=True))
# A repeated invocation of this build/contact returns the stored receipt.
request_id=f'manual-verification:{APP_VERSION}:{live["clientVersion"]}:{account}:{conv}'
receipt=sender.send(request_id,live['pid'],account,conv,'协议通道测试,请忽略',guard=contact_allowed)
report.update(status='confirmed',target=name,receipt=receipt,requestId=request_id)
except Exception as exc:report['error']=str(exc)
finally:
if sender:sender.close()
if db:db.close()
args.report.parent.mkdir(parents=True,exist_ok=True)
args.report.write_text(json.dumps(report,ensure_ascii=False,indent=2),encoding='utf-8')
return 0 if report['status']=='confirmed' else 2