@@ -14,6 +14,7 @@ import (
14
14
15
15
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
16
16
"github.com/joho/godotenv"
17
+ "github.com/m1guelpf/chatgpt-telegram/util/ref"
17
18
"github.com/playwright-community/playwright-go"
18
19
)
19
20
@@ -35,24 +36,22 @@ func main() {
35
36
browser , page := launchBrowser (pw , "https://chat.openai.com" , true )
36
37
37
38
for ! isLoggedIn (page ) {
38
- cookies := <- logIn (pw )
39
- for _ , cookie := range cookies {
40
- convertedCookie := playwright.BrowserContextAddCookiesOptionsCookies {
41
- Name : & cookie .Name ,
42
- Value : & cookie .Value ,
43
- Domain : & cookie .Domain ,
44
- Path : & cookie .Path ,
45
- Expires : & cookie .Expires ,
46
- Secure : & cookie .Secure ,
47
- HttpOnly : & cookie .HttpOnly ,
48
- SameSite : & cookie .SameSite ,
49
- }
50
- if err := browser .AddCookies (convertedCookie ); err != nil {
51
- log .Fatalf ("Couldn't add cookies: %v" , err )
52
- }
39
+ authCookie := playwright.BrowserContextAddCookiesOptionsCookies {
40
+ Path : ref .Of ("/" ),
41
+ Secure : ref .Of (true ),
42
+ HttpOnly : ref .Of (true ),
43
+ Value : ref .Of (<- logIn (pw )),
44
+ Domain : ref .Of ("chat.openai.com" ),
45
+ SameSite : playwright .SameSiteAttributeLax ,
46
+ Name : ref .Of ("__Secure-next-auth.session-token" ),
47
+ Expires : ref .Of (float64 (time .Now ().AddDate (0 , 1 , 0 ).Unix ())),
48
+ }
49
+
50
+ if err := browser .AddCookies (authCookie ); err != nil {
51
+ log .Fatalf ("Couldn't add cookie: %v" , err )
53
52
}
54
53
55
- if _ , err = page .Goto ("https://chat.openai.com" ); err != nil {
54
+ if _ , err = page .Goto ("https://chat.openai.com/chat " ); err != nil {
56
55
log .Fatalf ("Couldn't reload website: %v" , err )
57
56
}
58
57
}
@@ -270,9 +269,9 @@ func getChatBox(page playwright.Page) playwright.ElementHandle {
270
269
return input
271
270
}
272
271
273
- func logIn (pw * playwright.Playwright ) <- chan [] * playwright. BrowserContextCookiesResult {
272
+ func logIn (pw * playwright.Playwright ) <- chan string {
274
273
var lock sync.Mutex
275
- r := make (chan [] * playwright. BrowserContextCookiesResult )
274
+ r := make (chan string )
276
275
277
276
lock .Lock ()
278
277
go func () {
@@ -297,11 +296,19 @@ func logIn(pw *playwright.Playwright) <-chan []*playwright.BrowserContextCookies
297
296
log .Fatalf ("Couldn't store authentication: %v" , err )
298
297
}
299
298
299
+ var sessionToken string
300
+ for _ , cookie := range cookies {
301
+ if cookie .Name == "__Secure-next-auth.session-token" {
302
+ sessionToken = cookie .Value
303
+ break
304
+ }
305
+ }
306
+
300
307
if err := browser .Close (); err != nil {
301
308
log .Fatalf ("could not close browser: %v" , err )
302
309
}
303
310
304
- r <- cookies
311
+ r <- sessionToken
305
312
}()
306
313
307
314
return r
0 commit comments