Skip to content

Commit 401e262

Browse files
committed
we only care about the session token
1 parent c950a26 commit 401e262

File tree

3 files changed

+31
-21
lines changed

3 files changed

+31
-21
lines changed

Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
GIT_COMMIT=$(shell git describe --always)
22

3-
.PHONY: all build clean test
4-
53
all: build
64
default: build
75

main.go

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
1616
"github.com/joho/godotenv"
17+
"github.com/m1guelpf/chatgpt-telegram/util/ref"
1718
"github.com/playwright-community/playwright-go"
1819
)
1920

@@ -35,24 +36,22 @@ func main() {
3536
browser, page := launchBrowser(pw, "https://chat.openai.com", true)
3637

3738
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)
5352
}
5453

55-
if _, err = page.Goto("https://chat.openai.com"); err != nil {
54+
if _, err = page.Goto("https://chat.openai.com/chat"); err != nil {
5655
log.Fatalf("Couldn't reload website: %v", err)
5756
}
5857
}
@@ -270,9 +269,9 @@ func getChatBox(page playwright.Page) playwright.ElementHandle {
270269
return input
271270
}
272271

273-
func logIn(pw *playwright.Playwright) <-chan []*playwright.BrowserContextCookiesResult {
272+
func logIn(pw *playwright.Playwright) <-chan string {
274273
var lock sync.Mutex
275-
r := make(chan []*playwright.BrowserContextCookiesResult)
274+
r := make(chan string)
276275

277276
lock.Lock()
278277
go func() {
@@ -297,11 +296,19 @@ func logIn(pw *playwright.Playwright) <-chan []*playwright.BrowserContextCookies
297296
log.Fatalf("Couldn't store authentication: %v", err)
298297
}
299298

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+
300307
if err := browser.Close(); err != nil {
301308
log.Fatalf("could not close browser: %v", err)
302309
}
303310

304-
r <- cookies
311+
r <- sessionToken
305312
}()
306313

307314
return r

util/ref/ref.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package ref
2+
3+
func Of[E any](e E) *E {
4+
return &e
5+
}

0 commit comments

Comments
 (0)