wangrongding / wechat-bot

🤖一个基于 WeChaty 结合 OpenAi ChatGPT / Kimi / 讯飞等Ai服务实现的微信机器人 ,可以用来帮助你自动回复微信消息,或者管理微信群/好友,检测僵尸粉等...
MIT License
4.96k stars 710 forks source link

为什么接收到消息就报错了 #9

Closed yilin1203 closed 1 year ago

yilin1203 commented 1 year ago

🚀🚀🚀 / content 你好 file:///C:/Users/Administrator/Desktop/wechat-bot/nodemodules/chatgpt/build/index.js:1 import b from"expiry-map";import{v4 as T}from"uuid";import p from"node-fetch";import{createParser as S}from"eventsource-parser";async function u(i,s){let{onMessage:e,...r}=s,t=await p(i,r),n=S(o=>{o.type==="event"&&e(o.data)});t.body.on("readable",()=>{let o;for(;(o=t.body.read())!==null;)n.feed(o.toString())})}import{remark as x}from"remark";import E from"strip-markdown";function l(i){return x().use(E).processSync(i??"").toString()}var ="accessToken",v="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36",w=class{constructor(s){this._accessTokenCache=new b(10*1e3);let{sessionToken:e,markdown:r=!0,apiBaseUrl:t="https://chat.openai.com/api",backendApiBaseUrl:n="https://chat.openai.com/backend-api",userAgent:o=v}=s;if(this._sessionToken=e,this._markdown=!!r,this._apiBaseUrl=t,this._backendApiBaseUrl=n,this._userAgent=o,!this._sessionToken)throw new Error("ChatGPT invalid session token")}async getIsAuthenticated(){try{return await this.refreshAccessToken(),!0}catch{return!1}}async ensureAuth(){return await this.refreshAccessToken()}async sendMessage(s,e={}){let{conversationId:r=T(),onProgress:t}=e,n=await this.refreshAccessToken(),o={action:"next",messages:[{id:T(),role:"user",content:{content_type:"text",parts:[s]}}],model:"text-davinci-002-render",parent_message_id:r},y=${this._backendApiBaseUrl}/conversation,d="";return new Promise((A,f)=>{u(y,{method:"POST",headers:{Authorization:Bearer ${n},"Content-Type":"application/json","user-agent":this._userAgent},body:JSON.stringify(o),onMessage:m=>{var g,k;if(m==="[DONE]")return A(d);try{let c=JSON.parse(m).message;if(c){let a=(k=(g=c==null?void 0:c.content)==null?void 0:g.parts)==null?void 0:k[0];a&&(this._markdown||(a=l(a)),d=a,t&&t(a))}}catch(h){console.warn("fetchSSE onMessage unexpected error",h),f(h)}}}).catch(f)})}async refreshAccessToken(){let s=this.accessTokenCache.get();if(s)return s;try{let e=await p("https://chat.openai.com/api/auth/session",{headers:{cookie:`__Secure-next-auth.session-token=${this._sessionToken}`,"user-agent":this._userAgent}}).then(n=>n.json()),r=e==null?void 0:e.accessToken;if(!r)throw new Error("Unauthorized");let t=e==null?void 0:e.error;if(t)throw t==="RefreshAccessTokenError"?new Error("session token has expired"):new Error(t);return this.accessTokenCache.set(,r),r}catch(e){throw new Error(ChatGPT failed to refresh auth token. ${e.toString()})}}};export{w as ChatGPTAPI,l as markdownToText};

Error: ChatGPT failed to refresh auth token. FetchError: invalid json response body at https://chat.openai.com/api/auth/session reason: Unexpected token < in JSON at position 0 at w.refreshAccessToken (file:///C:/Users/Administrator/Desktop/wechat-bot/node_modules/chatgpt/build/index.js:1:2376) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async w.ensureAuth (file:///C:/Users/Administrator/Desktop/wechat-bot/node_modules/chatgpt/build/index.js:1:1081) at async getChatGPTReply (file:///C:/Users/Administrator/Desktop/wechat-bot/src/chatgpt.js:15:3) at async WechatyImpl.onMessage (file:///C:/Users/Administrator/Desktop/wechat-bot/src/wechaty.js:60:19)

Node.js v18.12.1

wangrongding commented 1 year ago

你应该 token过期了