souljoy commited on
Commit
cf22d38
1 Parent(s): 63137a5

Update app.py

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