Open main menu
首页
专栏
课程
分类
归档
Chat
Sci-Hub
谷歌学术
Libgen
GitHub镜像
登录/注册
搜索
关闭
Previous
Previous
Next
Next
ChatGPT接入微信公众号(手把手教学)
sockstack
/
565
/
2023-11-23 12:02:39
<p><span style="color: red; font-size: 18px">ChatGPT 可用网址,仅供交流学习使用,如对您有所帮助,请收藏并推荐给需要的朋友。</span><br><a href="https://ckai.xyz/?sockstack§ion=detail" target="__blank">https://ckai.xyz</a><br><br></p> <article class="baidu_pl"><div id="article_content" class="article_content clearfix"> <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css"> <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-25cebea3f9.css"> <div id="content_views" class="markdown_views prism-atom-one-dark"> <svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path></svg><h1> <a id="_0"></a>前言</h1> <p>本篇文章参考国内服务器 3 分钟将 ChatGPT 接入微信公众号(超详细)配置,纠正了一些过时的信息。</p> <h1> <a id="_4"></a>准备</h1> <p>一个微信公众号<br> 一个能访问外网的梯子<br> 一个ChatGPT账号</p> <p>有了这些就可以配置了</p> <h1> <a id="_11"></a>注册免费服务器,并部署代码</h1> <p>前往Laf https://laf.run/ 注册一个账号,手机号注册即可。注意,别进到国外的了<br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/img_convert/649a42716bc42944e7f428ba20de121e.png" alt="file"><br> 点击新建项目<br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/10224bfb76ca4b2d82e17feb7ba7caf4.png" alt="file"><br> 名称随便什么都可以,根据自己的喜好<br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/de464d9f7538458589550a34828ca9bd.png" alt="file"><br> 创建好过后是这个样子,我们点击开发进入编程页面<br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/5bcd414b8eec4ef6953c11936acc116c.png" alt="file"><br> 进入后添加npm依赖<br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/296ad7bf963a4eca82b95f9298a20a0a.png" alt="file"><br> 搜索chatgpt,添加第一个,保存并重启<br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/f25fb33db0254c049291ef958516958d.png" alt="file"><br> 然后新建一个函数<br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/57ef63d9ad0a4b19b4f8020183ce02ee.png" alt="file"><br> 函数名字也随便取一个<br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/1a948843b3d1420aa5a268dc4609bdb2.png" alt="file"><br> 然后将下列代码粘进去</p> <pre><code class="prism language-ts"><span class="token comment">// 引入crypto和cloud模块</span> <span class="token keyword">import</span> <span class="token operator">*</span> <span class="token keyword">as</span> crypto <span class="token keyword">from</span> <span class="token string">'crypto'</span><span class="token punctuation">;</span> <span class="token keyword">import</span> cloud <span class="token keyword">from</span> <span class="token string">'@lafjs/cloud'</span><span class="token punctuation">;</span><span class="token keyword">const</span> <span class="token constant">OPENAI_ACCESS_TOKEN</span> <span class="token operator">=</span> <span class="token string">"这个token我们稍后讲,怎么拿"</span> <span class="token keyword">const</span> <span class="token constant">WAIT_MESSAGE</span> <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">处理中 ... \n\n请稍等3秒后发送【1】查看回复</span><span class="token template-punctuation string">`</span></span> <span class="token keyword">const</span> <span class="token constant">NO_MESSAGE</span> <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">暂无内容,请稍后回复【1】再试</span><span class="token template-punctuation string">`</span></span> <span class="token keyword">const</span> <span class="token constant">CLEAR_MESSAGE</span> <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">✅ 记忆已清除</span><span class="token template-punctuation string">`</span></span> <span class="token keyword">const</span> <span class="token constant">HELP_MESSAGE</span> <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">ChatGPT 指令使用指南| 关键字 | 功能 || 1 | 上一次问题的回复 || 11 | 清除上下文 || 111 | 获取更多帮助 |</span><span class="token template-punctuation string">`</span></span><span class="token comment">// 不支持的消息类型</span> <span class="token keyword">const</span> <span class="token constant">UNSUPPORTED_MESSAGE_TYPES</span> <span class="token operator">=</span> <span class="token punctuation">{<!-- --></span>image<span class="token operator">:</span> <span class="token string">'暂不支持图片消息'</span><span class="token punctuation">,</span>voice<span class="token operator">:</span> <span class="token string">'暂不支持语音消息'</span><span class="token punctuation">,</span>video<span class="token operator">:</span> <span class="token string">'暂不支持视频消息'</span><span class="token punctuation">,</span>music<span class="token operator">:</span> <span class="token string">'暂不支持音乐消息'</span><span class="token punctuation">,</span>news<span class="token operator">:</span> <span class="token string">'暂不支持图文消息'</span><span class="token punctuation">,</span> <span class="token punctuation">}</span><span class="token comment">// 定义休眠函数</span> <span class="token keyword">const</span> <span class="token function-variable function">sleep</span> <span class="token operator">=</span> ms <span class="token operator">=></span> <span class="token keyword">new</span> <span class="token class-name"><span class="token builtin">Promise</span></span><span class="token punctuation">(</span>resolve <span class="token operator">=></span> <span class="token function">setTimeout</span><span class="token punctuation">(</span>resolve<span class="token punctuation">,</span> ms<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 创建数据库连接并获取Message集合</span> <span class="token keyword">const</span> db <span class="token operator">=</span> cloud<span class="token punctuation">.</span><span class="token function">database</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">const</span> Message <span class="token operator">=</span> db<span class="token punctuation">.</span><span class="token function">collection</span><span class="token punctuation">(</span><span class="token string">'messages'</span><span class="token punctuation">)</span><span class="token comment">// 处理接收到的微信公众号消息</span> <span class="token keyword">export</span> <span class="token keyword">async</span> <span class="token keyword">function</span> <span class="token function">main</span><span class="token punctuation">(</span>event<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token keyword">const</span> <span class="token punctuation">{<!-- --></span> signature<span class="token punctuation">,</span> timestamp<span class="token punctuation">,</span> nonce<span class="token punctuation">,</span> echostr <span class="token punctuation">}</span> <span class="token operator">=</span> event<span class="token punctuation">.</span>query<span class="token punctuation">;</span><span class="token keyword">const</span> token <span class="token operator">=</span> <span class="token string">'123456'</span><span class="token punctuation">;</span><span class="token comment">// 验证消息是否合法,若不合法则返回错误信息</span><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token function">verifySignature</span><span class="token punctuation">(</span>signature<span class="token punctuation">,</span> timestamp<span class="token punctuation">,</span> nonce<span class="token punctuation">,</span> token<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token keyword">return</span> <span class="token string">'Invalid signature'</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment">// 如果是首次验证,则返回 echostr 给微信服务器</span><span class="token keyword">if</span> <span class="token punctuation">(</span>echostr<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token keyword">return</span> echostr<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment">// 处理文本回复消息</span> <span class="token keyword">async</span> <span class="token keyword">function</span> <span class="token function">replyText</span><span class="token punctuation">(</span>message<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token keyword">const</span> <span class="token punctuation">{<!-- --></span> question<span class="token punctuation">,</span> sessionId <span class="token punctuation">}</span> <span class="token operator">=</span> message<span class="token punctuation">;</span><span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"replyText 执行了"</span><span class="token punctuation">)</span><span class="token comment">// 检查是否是重试操作,如果是重试操作,返回上一次的回复</span><span class="token keyword">if</span> <span class="token punctuation">(</span>question <span class="token operator">===</span> <span class="token string">'1'</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token keyword">const</span> lastMessage <span class="token operator">=</span> <span class="token keyword">await</span> Message<span class="token punctuation">.</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token punctuation">{<!-- --></span>sessionId<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">orderBy</span><span class="token punctuation">(</span><span class="token string">"createdAt"</span><span class="token punctuation">,</span> <span class="token string">"desc"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">if</span> <span class="token punctuation">(</span>lastMessage<span class="token punctuation">.</span>data<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token keyword">return</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${<!-- --></span>lastMessage<span class="token punctuation">.</span>data<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>question<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">\n------------\n</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${<!-- --></span>lastMessage<span class="token punctuation">.</span>data<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>answer<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token keyword">return</span> <span class="token constant">NO_MESSAGE</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment">// 获取上下文 id</span><span class="token keyword">const</span> res <span class="token operator">=</span> <span class="token keyword">await</span> Message<span class="token punctuation">.</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token punctuation">{<!-- --></span>sessionId<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">orderBy</span><span class="token punctuation">(</span><span class="token string">"createdAt"</span><span class="token punctuation">,</span> <span class="token string">"desc"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getOne</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">const</span> parentId <span class="token operator">=</span> res<span class="token operator">?.</span>data<span class="token operator">?.</span>parentMessageId<span class="token keyword">const</span> conId <span class="token operator">=</span> res<span class="token operator">?.</span>data<span class="token operator">?.</span>conversationId<span class="token comment">// 发送指令</span><span class="token keyword">if</span> <span class="token punctuation">(</span>question<span class="token punctuation">.</span><span class="token function">startsWith</span><span class="token punctuation">(</span><span class="token string">'11'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token keyword">return</span> <span class="token keyword">await</span> <span class="token function">processCommandText</span><span class="token punctuation">(</span>message<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment">// 获取 OpenAI 回复内容</span><span class="token keyword">const</span> <span class="token punctuation">{<!-- --></span> error<span class="token punctuation">,</span> answer<span class="token punctuation">,</span> parentMessageId<span class="token punctuation">,</span> conversationId <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token keyword">await</span> <span class="token function">getOpenAIReply</span><span class="token punctuation">(</span>question<span class="token punctuation">,</span> parentId<span class="token punctuation">,</span> conId<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">if</span> <span class="token punctuation">(</span>error<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">sessionId: </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${<!-- --></span>sessionId<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">; question: </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${<!-- --></span>question<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">; error: </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${<!-- --></span>error<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">return</span> error<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment">// 将消息保存到数据库中</span><span class="token keyword">const</span> token <span class="token operator">=</span> question<span class="token punctuation">.</span>length <span class="token operator">+</span> answer<span class="token punctuation">.</span>length<span class="token punctuation">;</span><span class="token keyword">const</span> result <span class="token operator">=</span> <span class="token keyword">await</span> Message<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token punctuation">{<!-- --></span> token<span class="token punctuation">,</span> answer<span class="token punctuation">,</span> parentMessageId<span class="token punctuation">,</span> conversationId<span class="token punctuation">,</span> <span class="token operator">...</span>message <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">debug</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">[save message] result: </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${<!-- --></span>result<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">return</span> answer<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token comment">// 获取 OpenAI API 的回复</span> <span class="token keyword">async</span> <span class="token keyword">function</span> <span class="token function">getOpenAIReply</span><span class="token punctuation">(</span>question<span class="token punctuation">,</span> parentId<span class="token punctuation">,</span> conId<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"getOpenAIReply 执行了"</span><span class="token punctuation">)</span><span class="token comment">// 引入 ChatGPTUnofficialProxyAPI 模块</span><span class="token keyword">const</span> <span class="token punctuation">{<!-- --></span> ChatGPTUnofficialProxyAPI <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token keyword">await</span> <span class="token keyword">import</span><span class="token punctuation">(</span><span class="token string">'chatgpt'</span><span class="token punctuation">)</span><span class="token comment">// 创建 ChatGPTUnofficialProxyAPI 实例</span><span class="token keyword">const</span> api <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ChatGPTUnofficialProxyAPI</span><span class="token punctuation">(</span><span class="token punctuation">{<!-- --></span>accessToken<span class="token operator">:</span> <span class="token constant">OPENAI_ACCESS_TOKEN</span><span class="token punctuation">,</span>apiReverseProxyUrl<span class="token operator">:</span> <span class="token string">"https://ai.fakeopen.com/api/conversation"</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token keyword">try</span> <span class="token punctuation">{<!-- --></span><span class="token comment">// 如果有上下文 id,就带上</span><span class="token keyword">let</span> res<span class="token punctuation">;</span><span class="token keyword">if</span> <span class="token punctuation">(</span>parentId <span class="token operator">&&</span> conId<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>res <span class="token operator">=</span> <span class="token keyword">await</span> api<span class="token punctuation">.</span><span class="token function">sendMessage</span><span class="token punctuation">(</span>question<span class="token punctuation">,</span> <span class="token punctuation">{<!-- --></span> conversationId<span class="token operator">:</span> conId<span class="token punctuation">,</span> parentMessageId<span class="token operator">:</span> parentId <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{<!-- --></span>res <span class="token operator">=</span> <span class="token keyword">await</span> api<span class="token punctuation">.</span><span class="token function">sendMessage</span><span class="token punctuation">(</span>question<span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token comment">// 返回 OpenAI 回复的内容及上下文 id</span><span class="token keyword">return</span> <span class="token punctuation">{<!-- --></span> answer<span class="token operator">:</span> res<span class="token punctuation">.</span>text<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token string">"\n\n"</span><span class="token punctuation">,</span> <span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">,</span> parentMessageId<span class="token operator">:</span> res<span class="token punctuation">.</span>parentMessageId<span class="token punctuation">,</span> conversationId<span class="token operator">:</span> res<span class="token punctuation">.</span>conversationId <span class="token punctuation">}</span><span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token keyword">return</span> <span class="token punctuation">{<!-- --></span>error<span class="token operator">:</span> <span class="token string">"问题太难了 出错了. (uДu〃)."</span><span class="token punctuation">,</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token comment">// 校验微信服务器发送的消息是否合法</span> <span class="token keyword">function</span> <span class="token function">verifySignature</span><span class="token punctuation">(</span>signature<span class="token punctuation">,</span> timestamp<span class="token punctuation">,</span> nonce<span class="token punctuation">,</span> token<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token keyword">const</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span>token<span class="token punctuation">,</span> timestamp<span class="token punctuation">,</span> nonce<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">const</span> str <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">const</span> sha1 <span class="token operator">=</span> crypto<span class="token punctuation">.</span><span class="token function">createHash</span><span class="token punctuation">(</span><span class="token string">'sha1'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>sha1<span class="token punctuation">.</span><span class="token function">update</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">return</span> sha1<span class="token punctuation">.</span><span class="token function">digest</span><span class="token punctuation">(</span><span class="token string">'hex'</span><span class="token punctuation">)</span> <span class="token operator">===</span> signature<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token comment">// 返回组装 xml</span> <span class="token keyword">function</span> <span class="token function">toXML</span><span class="token punctuation">(</span>payload<span class="token punctuation">,</span> content<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token keyword">const</span> timestamp <span class="token operator">=</span> Date<span class="token punctuation">.</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">const</span> <span class="token punctuation">{<!-- --></span> tousername<span class="token operator">:</span> fromUserName<span class="token punctuation">,</span> fromusername<span class="token operator">:</span> toUserName <span class="token punctuation">}</span> <span class="token operator">=</span> payload<span class="token punctuation">;</span><span class="token keyword">return</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string"><xml><ToUserName><![CDATA[</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${<!-- --></span>toUserName<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">]]></ToUserName><FromUserName><![CDATA[</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${<!-- --></span>fromUserName<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">]]></FromUserName><CreateTime></span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${<!-- --></span>timestamp<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string"></CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${<!-- --></span>content<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">]]></Content></xml></span><span class="token template-punctuation string">`</span></span> <span class="token punctuation">}</span><span class="token keyword">async</span> <span class="token keyword">function</span> <span class="token function">processCommandText</span><span class="token punctuation">(</span><span class="token punctuation">{<!-- --></span> sessionId<span class="token punctuation">,</span> question <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token comment">// 清理历史会话</span><span class="token keyword">if</span> <span class="token punctuation">(</span>question <span class="token operator">===</span> <span class="token string">'11'</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span><span class="token keyword">const</span> res <span class="token operator">=</span> <span class="token keyword">await</span> Message<span class="token punctuation">.</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token punctuation">{<!-- --></span> sessionId <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token punctuation">{<!-- --></span> multi<span class="token operator">:</span> <span class="token boolean">true</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token keyword">return</span> <span class="token constant">CLEAR_MESSAGE</span><span class="token punctuation">;</span><span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{<!-- --></span><span class="token keyword">return</span> <span class="token constant">HELP_MESSAGE</span><span class="token punctuation">;</span><span class="token punctuation">}</span> <span class="token punctuation">}</span></code></pre> <p>代码中有四个需要讲的点</p> <ul> <li><img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/e3c19adec65548b4b079c99f091d7d80.png" alt="file"></li> <li>const token 123456 随意配置,一会要用</li> <li>OPENAI_ACCESS_TOKEN, 这个就是聊天的token</li> </ul> <p>怎么获取呢你<br> ,需要先在浏览器中登录 ChatGPT 网页版,然后在浏览器中新建标签页访问这个 URL:https://chat.openai.com/api/auth/session 它会返回一个 JSON,里面包含了 accessToken 字段,将这个字段的值复制到代码中的OPENAI_ACCESS_TOKEN中即可。</p> <ul><li>apiReverseProxyUrl<br> 这个URL可能变化,原文就变了 现在用的是"https://ai.fakeopen.com/api/conversation" 还能用,如果以后用不了了请参考ChatGPTUnofficialProxyAPI 的使用文档</li></ul> <p>最后点击发布即可一键部署</p> <h1> <a id="_203"></a>公众号配置</h1> <p>登录微信公众平台,点击设置与开发<br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/a4048343aa7e4444bb30cbe0c9dc84ff.png" alt="file"><br> 点击「基本设置」,然后点击「服务器配置」,服务器配置那里点击修改配置:<br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/308859f4b86a445a99be9a6f0eb6b7f5.png" alt="file"><br> 这里的URL就是之前上文在laf发布旁边的网址<br> 这里的Token就是之前上文在代码中的const token = ‘123456’;<br> EncodingAESKey随机生成,提交后启用即可<br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/f058dea272324a34a34a915cd7a85200.png" alt="file"></p> <p>现在你已经完成了所有必要的设置和配置,下面就可以直接进入微信公众号「我怀里的猫」后台与机器人进行交互啦!<br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/b21fa32e6475466b9fc573b4c3e68ee5.png" alt="file"><br> 效果<br> <img referrerpolicy="no-referrer" src="https://img-blog.csdnimg.cn/41b06803a3dd461f962495f9e7c28254.png" alt="file"></p> <p>参考文档<br> https://juejin.cn/post/7221716906334994489<br> ChatGPTUnofficialProxyAPI 的使用文档<br> https://github.com/husanr/wechat_gpt_laf</p> <blockquote> <p>本文由博客一文多发平台 OpenWrite 发布!</p> </blockquote> </div> <link href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/markdown_views-98b95bb57c.css" rel="stylesheet"> <link href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/style-c216769e99.css" rel="stylesheet"> </div> <div id="treeSkill"></div> </article>
ChatGPT接入微信公众号(手把手教学)
作者
sockstack
许可协议
CC BY 4.0
发布于
2023-11-23
修改于
2024-12-23
上一篇:软件:常用 Linux 软件汇总,值得收藏
下一篇:如何实现Chatgpt写文章(附chatgpt3.5免费接口)
尚未登录
登录 / 注册
文章分类
博客重构之路
5
Spring Boot简单入门
4
k8s 入门教程
0
MySQL 知识
1
NSQ 消息队列
0
ThinkPHP5 源码分析
5
使用 Docker 从零开始搭建私人代码仓库
3
日常开发汇总
4
标签列表
springboot
hyperf
swoole
webman
php
多线程
数据结构
docker
k8s
thinkphp
mysql
tailwindcss
flowbite
css
前端