1717
1818@ Data
1919public class Chatbot {
20+ private String cfClearance ;
2021 private Map <String , String > config = new HashMap <>();
2122 private String conversationId ;
23+
24+ private String userAgent ;
2225 private String parentId ;
2326 private Map <String , String > headers ;
2427
@@ -30,13 +33,21 @@ public Chatbot(Map<String, String> config, String conversationId) {
3033 this .config = config ;
3134 this .conversationId = conversationId ;
3235 this .parentId = UUID .randomUUID ().toString ();
33- if (config .containsKey ("session_token" ) || (config .containsKey ("email" )
34- && config .containsKey ("password" ))) {
36+ if (config .containsKey ("session_token" )) {
3537 refreshSession ();
3638 }
3739 }
3840
39- public Chatbot (String sessionToken ) {
41+ /**
42+ * 初始化
43+ *
44+ * @param sessionToken
45+ * @param cfClearance
46+ * @param userAgent
47+ */
48+ public Chatbot (String sessionToken , String cfClearance , String userAgent ) {
49+ this .userAgent = userAgent ;
50+ this .cfClearance = cfClearance ;
4051 config .put ("session_token" , sessionToken );
4152 this .parentId = UUID .randomUUID ().toString ();
4253 refreshSession ();
@@ -61,9 +72,7 @@ public void refreshHeaders() {
6172 put ("Accept" , "text/event-stream" );
6273 put ("Authorization" , "Bearer " + config .get ("Authorization" ));
6374 put ("Content-Type" , "application/json" );
64- put ("User-Agent" , "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1" +
65- ".15 (KHTML, like Gecko) " +
66- "Version/16.1 Safari/605.1.15" );
75+ put ("User-Agent" , userAgent );
6776 put ("X-Openai-Assistant-App-Id" , "" );
6877 put ("Connection" , "close" );
6978 put ("Accept-Language" , "en-US,en;q=0.9" );
@@ -135,6 +144,7 @@ public Map<String, Object> getChatText(Map<String, Object> data) {
135144 // Set multiple cookies
136145 session .getCookies ().put ("__Secure-next-auth.session-token" , config .get ("session_token" ));
137146 session .getCookies ().put ("__Secure-next-auth.callback-url" , "https://chat.openai.com/" );
147+ session .getCookies ().put ("cf_clearance" , cfClearance );
138148
139149 // Set proxies
140150 setupProxy (session );
@@ -230,10 +240,9 @@ public Map<String, Object> getChatResponse(String prompt) {
230240
231241 @ SneakyThrows
232242 public void refreshSession () {
233- if (!config .containsKey ("session_token" ) && (!config .containsKey ("email" ) ||
234- !config .containsKey ("password" ))) {
243+ if (!config .containsKey ("session_token" )) {
235244 throw new RuntimeException ("No tokens provided" );
236- } else if ( config . containsKey ( "session_token" )) {
245+ } else {
237246 String sessionToken = config .get ("session_token" );
238247 if (sessionToken == null || sessionToken .equals ("" )) {
239248 throw new RuntimeException ("No tokens provided" );
@@ -246,15 +255,11 @@ public void refreshSession() {
246255 // Set cookies
247256 session .getCookies ().put ("__Secure-next-auth.session-token" , config .get (
248257 "session_token" ));
258+ session .getCookies ().put ("cf_clearance" , cfClearance );
249259
250260 String urlSession = "https://chat.openai.com/api/auth/session" ;
251261 HttpResponse response = session .get2 (urlSession ,
252- Collections .singletonMap (
253- "User-Agent" ,
254- "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15" +
255- " (KHTML," +
256- " like Gecko) Version/16.1 Safari/605.1.15 "
257- ));
262+ Collections .singletonMap ("User-Agent" , userAgent ));
258263
259264 try {
260265 String name = "__Secure-next-auth.session-token" ;
@@ -275,20 +280,11 @@ public void refreshSession() {
275280 System .out .println ("Error refreshing session" );
276281 throw new Exception ("Error refreshing session" , e );
277282 }
278- } else if (config .containsKey ("email" ) && config .containsKey ("password" )) {
279- try {
280- this .login (config .get ("email" ), config .get ("password" ));
281- } catch (Exception e ) {
282- System .out .println ("Error refreshing session: " );
283- System .out .println (e );
284- throw e ;
285- }
286- } else {
287- throw new RuntimeException ("No tokens provided" );
288283 }
289284 }
290285
291286
287+ @ Deprecated
292288 public void login (String email , String password ) {
293289 System .out .println ("Logging in..." );
294290 boolean useProxy = false ;
0 commit comments