Update app.py
Browse files
app.py
CHANGED
|
@@ -6,11 +6,6 @@ import requests
|
|
| 6 |
import json
|
| 7 |
import openai
|
| 8 |
import time
|
| 9 |
-
from fastapi import FastAPI, Request, Response
|
| 10 |
-
from fastapi.responses import PlainTextResponse
|
| 11 |
-
from hashlib import sha1
|
| 12 |
-
from time import time
|
| 13 |
-
from xml.etree.ElementTree import Element, tostring
|
| 14 |
|
| 15 |
|
| 16 |
class Text(BaseModel):
|
|
@@ -199,71 +194,3 @@ def create_image_ep(content: Text = None):
|
|
| 199 |
)
|
| 200 |
return JSONResponse(content=result.json())
|
| 201 |
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
def check_signature(token, signature, timestamp, nonce):
|
| 211 |
-
tmp_list = [token, timestamp, nonce]
|
| 212 |
-
tmp_list.sort()
|
| 213 |
-
tmp_str = ''.join(tmp_list)
|
| 214 |
-
tmp_str = sha1(tmp_str.encode('utf-8')).hexdigest()
|
| 215 |
-
|
| 216 |
-
return tmp_str == signature
|
| 217 |
-
|
| 218 |
-
def chat_gpt_response(prompt):
|
| 219 |
-
# Replace with your GPT-3.5 implementation
|
| 220 |
-
return "你好呀,小哥哥"
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
@app.get('/wechat')
|
| 224 |
-
def verify_server_address(signature: str, timestamp: str, nonce: str, echostr: str):
|
| 225 |
-
token = 'zsj'
|
| 226 |
-
if check_signature(token, signature, timestamp, nonce):
|
| 227 |
-
return PlainTextResponse(echostr)
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
@app.post('/wechat')
|
| 231 |
-
def process_message(request: Request):
|
| 232 |
-
xml_data = await request.body()
|
| 233 |
-
xml_tree = ElementTree.fromstring(xml_data)
|
| 234 |
-
|
| 235 |
-
msg_type = xml_tree.find('MsgType').text
|
| 236 |
-
if msg_type == 'text':
|
| 237 |
-
content = xml_tree.find('Content').text
|
| 238 |
-
user_open_id = xml_tree.find('FromUserName').text
|
| 239 |
-
public_account_id = xml_tree.find('ToUserName').text
|
| 240 |
-
|
| 241 |
-
reply_content = chat_gpt_response(content)
|
| 242 |
-
|
| 243 |
-
reply = Element('xml')
|
| 244 |
-
to_user_name = Element('ToUserName')
|
| 245 |
-
to_user_name.text = user_open_id
|
| 246 |
-
reply.append(to_user_name)
|
| 247 |
-
|
| 248 |
-
from_user_name = Element('FromUserName')
|
| 249 |
-
from_user_name.text = public_account_id
|
| 250 |
-
reply.append(from_user_name)
|
| 251 |
-
|
| 252 |
-
create_time = Element('CreateTime')
|
| 253 |
-
create_time.text = str(int(time()))
|
| 254 |
-
reply.append(create_time)
|
| 255 |
-
|
| 256 |
-
msg_type = Element('MsgType')
|
| 257 |
-
msg_type.text = 'text'
|
| 258 |
-
reply.append(msg_type)
|
| 259 |
-
|
| 260 |
-
content = Element('Content')
|
| 261 |
-
content.text = reply_content
|
| 262 |
-
reply.append(content)
|
| 263 |
-
|
| 264 |
-
response_xml = tostring(reply, encoding='utf-8')
|
| 265 |
-
return Response(content=response_xml, media_type='application/xml')
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
|
|
|
| 6 |
import json
|
| 7 |
import openai
|
| 8 |
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
|
| 11 |
class Text(BaseModel):
|
|
|
|
| 194 |
)
|
| 195 |
return JSONResponse(content=result.json())
|
| 196 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|