1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| import time from flask import Flask,make_response,request import openai from flask import Flask, request from flask_caching import Cache import xml.etree.cElementTree as ET import hashlib import requests import re import os
cnt = 0 my_wx_token = "" my_gpt_key = "" my_switch_chatgpt = True
app = Flask(__name__) env_dist = os.environ cache = Cache(app, config={'CACHE_TYPE': 'simple', "CACHE_DEFAULT_TIMEOUT": 30})
@app.route('/',methods=['GET','POST']) def wechat(): if request.method == 'GET': signature = request.args.get("signature", "")
timestamp= request.args.get("timestamp", "")
nonce= request.args.get("nonce", "")
echostr= request.args.get("echostr", "") print(signature, timestamp, nonce, echostr)
token=my_wx_token
data =[token, timestamp, nonce]
data.sort()
temp = ''.join(data)
sha1 = hashlib.sha1(temp.encode('utf-8'))
hashcode=sha1.hexdigest() print(hashcode)
if hashcode == signature: print("wechat commit check OK") return echostr else: print("GET error input msg") return "error-return\r\n" else: xmlData = ET.fromstring(request.stream.read()) msg_type = xmlData.find('MsgType').text if msg_type == 'text': ToUserName = xmlData.find('ToUserName').text FromUserName = xmlData.find('FromUserName').text CreateTime = xmlData.find('CreateTime').text print(ToUserName) print(FromUserName) print(CreateTime) global cnt cnt += 1 print('-------> ' + str(cnt)) return generate_response_xml(FromUserName, ToUserName, xmlData.find('Content').text) def text_reply(FromUserName, ToUserName, output_content): reply = ''' <xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> </xml> ''' response = make_response(reply % (FromUserName, ToUserName, str(int(time.time())), output_content)) response.content_type = 'application/xml' return response
def generate_response_xml(FromUserName, ToUserName, input_content): output_content = generate_response(input_content) return text_reply(FromUserName, ToUserName, output_content)
outofsevice_txt = "抱歉,<a href=\"https://mp.weixin.qq.com/s/0LN37YiERJgMyvIDpzRcAQ\">攻城狮杰森的ChatGPT服务助手</a>正在维护中,暂时无法预估维护持续时间,请明天再来尝试吧。"
@cache.memoize(timeout=60) def generate_response(prompt): if not my_switch_chatgpt: return outofsevice_txt openai.api_key = my_gpt_key response = openai.Completion.create( model="text-davinci-003", prompt=prompt, temperature=0, max_tokens=1024, top_p=1, frequency_penalty=0.0, presence_penalty=0.0, ) message = response.choices[0].text print(message) ans = message.strip() return ans if __name__ == '__main__': app.run(host='0.0.0.0', port=xxxx, debug=True)
|