公眾號接入chatGPT的詳細(xì)教程 附Python源碼
前置準(zhǔn)備
- 一個域名
- 一臺服務(wù)器
- 一個公眾號
域名配置
在你的域名服務(wù)商新建二級域名并綁定服務(wù)器主機(jī)IP
服務(wù)器配置
上傳下面的python文件到你的服務(wù)器,并修改代碼段中相應(yīng)位置代碼(token、api-key、port)
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 = "" # 自定義字母和數(shù)字組合即可,后續(xù)需要填入公眾號后臺 my_gpt_key = "" # 這里填寫你在OpenAI后臺創(chuàng)建的API-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服務(wù)助手</a>正在維護(hù)中,暫時無法預(yù)估維護(hù)持續(xù)時間,請明天再來嘗試吧。" @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)#開放xxxx端口
使用寶塔是比較快捷的配置方式,安裝寶塔面板后,進(jìn)入軟件商店,安裝下面兩個插件
打開 python 項(xiàng)目管理器 ,簡單配置下我們要啟動的項(xiàng)目
啟動后映射項(xiàng)目域名,頂級域和二級域都可以,比如我這里填入的是 chatgpt.coder-jason.cn
公眾號配置
進(jìn)入公眾號后臺,找到設(shè)置與開發(fā),進(jìn)入基本配置,由于我這里已經(jīng)配置好了,這里僅演示下怎么添加啟用
點(diǎn)擊添加配置
token 值就是在上述代碼段中填入的值,自定義字母和數(shù)字組合即可
點(diǎn)擊提交后,如果服務(wù)器中的項(xiàng)目啟動無誤,則會提示 token校驗(yàn)成功
接下來就可以回到公眾號和 chatGPT 愉快的交流啦~
到此這篇關(guān)于公眾號接入 chatGPT教程 附Python源碼的文章就介紹到這了,更多相關(guān)公眾號接入 chatGPT內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Pytorch數(shù)據(jù)類型Tensor張量操作的實(shí)現(xiàn)
本文主要介紹了Pytorch數(shù)據(jù)類型Tensor張量操作的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07python筆記(1) 關(guān)于我們應(yīng)不應(yīng)該繼續(xù)學(xué)習(xí)python
關(guān)于Python,如果你要學(xué)習(xí),建議大家查看一下網(wǎng)站:因?yàn)楸救艘彩莿倓倹Q定收集點(diǎn)零碎時間來學(xué)習(xí)下它,推薦可能并不是最好的2012-10-10python腳本實(shí)現(xiàn)mp4中的音頻提取并保存在原目錄
這篇文章主要介紹了python腳本實(shí)現(xiàn)mp4中的音頻提取并保存在原目錄,本文給大家通過實(shí)例代碼介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02淺析python實(shí)現(xiàn)scrapy定時執(zhí)行爬蟲
這篇文章主要介紹了淺析python實(shí)現(xiàn)scrapy定時執(zhí)行爬蟲的相關(guān)資料,需要的朋友可以參考下2018-03-03python 實(shí)現(xiàn)IP子網(wǎng)計算
這篇文章主要介紹了python 實(shí)現(xiàn)IP子網(wǎng)計算的方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2021-02-02Python-apply(lambda x: )的使用及說明
這篇文章主要介紹了Python-apply(lambda x: )的使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02使用BeeWare實(shí)現(xiàn)iOS調(diào)用Python方式
這篇文章主要介紹了使用BeeWare實(shí)現(xiàn)iOS調(diào)用Python方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12