|
@@ -12,6 +12,8 @@ Create Date: 2021/6/19
|
|
|
import re
|
|
|
import time
|
|
|
import sys
|
|
|
+import uuid
|
|
|
+
|
|
|
from fastapi import FastAPI, Response, Request, BackgroundTasks, Body
|
|
|
from WXBizMsgCrypt3 import WXBizMsgCrypt
|
|
|
from xml.etree.ElementTree import fromstring
|
|
@@ -19,7 +21,7 @@ import uvicorn
|
|
|
import requests
|
|
|
import json
|
|
|
|
|
|
-from commom import get_logger
|
|
|
+from commom import get_logger, request_id_context
|
|
|
|
|
|
logger = get_logger()
|
|
|
|
|
@@ -151,13 +153,15 @@ def qiwei_post(username: str, answer: str,agentid:str):
|
|
|
|
|
|
|
|
|
|
|
|
-def post_consumer_api(user_query, decrypt_data):
|
|
|
+def post_consumer_api(user_query, decrypt_data, request_id):
|
|
|
data = {
|
|
|
"user_query": user_query,
|
|
|
- "decrypt_data": decrypt_data
|
|
|
+ "decrypt_data": decrypt_data,
|
|
|
+ "request_id": request_id,
|
|
|
}
|
|
|
url = "https://101.126.81.2:18088/consumer"
|
|
|
try:
|
|
|
+ request_id_context.set(request_id)
|
|
|
response = requests.post(url, json=data, verify=False)
|
|
|
response.raise_for_status()
|
|
|
logger.info(f"post_consumer_api 请求成功: {response.json()}")
|
|
@@ -175,8 +179,8 @@ request: Request
|
|
|
body = json.loads(body)
|
|
|
user_query = body["user_query"]
|
|
|
decrypt_data = body["decrypt_data"]
|
|
|
-
|
|
|
-
|
|
|
+ request_id = body["request_id"]
|
|
|
+ request_id_context.set(request_id)
|
|
|
logger.info(f"consumer 请求:{user_query}")
|
|
|
username = decrypt_data.get('FromUserName', '')
|
|
|
agentid = decrypt_data.get('AgentID', '')
|
|
@@ -224,13 +228,15 @@ async def verify(msg_signature: str, timestamp: str, nonce: str, echostr: str):
|
|
|
async def recv(msg_signature: str, timestamp: str, nonce: str, request: Request, background_tasks: BackgroundTasks):
|
|
|
|
|
|
body = await request.body()
|
|
|
+ request_id = str(uuid.uuid4())
|
|
|
+ request_id_context.set(request_id)
|
|
|
ret, sMsg = wxcpt.DecryptMsg(body.decode('utf-8'), msg_signature, timestamp, nonce)
|
|
|
decrypt_data = {}
|
|
|
for node in list(fromstring(sMsg.decode('utf-8'))):
|
|
|
decrypt_data[node.tag] = node.text
|
|
|
user_query = decrypt_data.get('Content', '')
|
|
|
logger.info(f"start: {user_query}")
|
|
|
- background_tasks.add_task(post_consumer_api, user_query, decrypt_data)
|
|
|
+ background_tasks.add_task(post_consumer_api, user_query, decrypt_data, request_id)
|
|
|
|
|
|
|
|
|
|