From 47e75ec47408e5dd1cdd4d9667ce9e020673f78f Mon Sep 17 00:00:00 2001 From: Sebastian Plaza Date: Fri, 9 Dec 2022 09:08:18 +0100 Subject: [PATCH 1/2] feat: add docker image --- .config/chatgpt.json | 3 +++ Dockerfile | 2 +- README.md | 6 ++++++ docker-compose.yml | 9 +++++++++ main.go | 5 +---- 5 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 .config/chatgpt.json create mode 100644 docker-compose.yml diff --git a/.config/chatgpt.json b/.config/chatgpt.json new file mode 100644 index 0000000..5880545 --- /dev/null +++ b/.config/chatgpt.json @@ -0,0 +1,3 @@ +{ + "openaisession": "YOUR_COOKIE_HERE" +} diff --git a/Dockerfile b/Dockerfile index 8880dfa..388b912 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ FROM golang:1-alpine as builder -RUN apk update && apk add make +RUN apk update && apk add gcc make g++ git WORKDIR /build ADD . . RUN make build diff --git a/README.md b/README.md index e8f287e..836b7ce 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,12 @@ You will then have to create a config file in the following location depending o Finally, add your cookie to the file and save it. It should look like this: `{ "openaisession": "YOUR_COOKIE_HERE" }`. +## Docker + +It is also possible to launch the bot via the docker image included. + +Check [docker-compose](./docker-compose.yml) file to understands how to do it. + ## License This repository is licensed under the [MIT License](LICENSE). diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..d0a2975 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,9 @@ +services: + chatgpt-telegram: + build: . + container_name: chatgpt-telegram + volumes: + - .config/:/root/.config + environment: + - TELEGRAM_ID= + - TELEGRAM_TOKEN= diff --git a/main.go b/main.go index ccc1e77..e8f69ed 100644 --- a/main.go +++ b/main.go @@ -36,10 +36,7 @@ func main() { envConfig, err := config.LoadEnvConfig(".env") if err != nil { - log.Fatalf("Couldn't load .env config: %v", err) - } - if err := envConfig.ValidateWithDefaults(); err != nil { - log.Fatalf("Invalid .env config: %v", err) + log.Printf("Couldn't load .env file: %v. Using shell exposed env variables...", err) } bot, err := tgbot.New(envConfig.TelegramToken, time.Duration(envConfig.EditWaitSeconds)) From ea82e04aae088c6717a3d546912593477ee2206f Mon Sep 17 00:00:00 2001 From: Sergey Krashevich Date: Mon, 12 Dec 2022 15:47:52 +0300 Subject: [PATCH 2/2] Define session token via ENV --- .config/chatgpt.json | 3 --- Dockerfile | 6 +++++- docker-compose.yml | 3 +-- env.example | 1 + main.go | 36 +++++++++++++++++++++++------------- 5 files changed, 30 insertions(+), 19 deletions(-) delete mode 100644 .config/chatgpt.json diff --git a/.config/chatgpt.json b/.config/chatgpt.json deleted file mode 100644 index 5880545..0000000 --- a/.config/chatgpt.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "openaisession": "YOUR_COOKIE_HERE" -} diff --git a/Dockerfile b/Dockerfile index 388b912..3352e22 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,6 +6,10 @@ RUN make build FROM alpine COPY --from=builder /build/chatgpt-telegram /bin/chatgpt-telegram -RUN chmod +x /bin/chatgpt-telegram +RUN chmod +x /bin/chatgpt-telegram && mkdir -p /root/.config + +ENV TELEGRAM_ID "" +ENV TELEGRAM_TOKEN "" +ENV OPENAI_SESSION "" ENTRYPOINT ["/bin/chatgpt-telegram"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index d0a2975..3e55eb4 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,8 +2,7 @@ services: chatgpt-telegram: build: . container_name: chatgpt-telegram - volumes: - - .config/:/root/.config environment: - TELEGRAM_ID= - TELEGRAM_TOKEN= + - OPENAI_SESSION= diff --git a/env.example b/env.example index 81e2546..c93e13a 100644 --- a/env.example +++ b/env.example @@ -1,3 +1,4 @@ TELEGRAM_ID= TELEGRAM_TOKEN= EDIT_WAIT_SECONDS=1 +OPENAI_SESSION= diff --git a/main.go b/main.go index e8f69ed..e86e7c5 100644 --- a/main.go +++ b/main.go @@ -20,26 +20,36 @@ func main() { log.Fatalf("Couldn't load config: %v", err) } - if persistentConfig.OpenAISession == "" { - token, err := session.GetSession() - if err != nil { - log.Fatalf("Couldn't get OpenAI session: %v", err) - } + err = godotenv.Load() + if err != nil { + log.Printf("Couldn't load .env file: %v. Using shell exposed env variables...", err) + } + + + if config.OpenAISession == "" { + if os.Getenv("OPENAI_SESSION") == "" { + session, err := session.GetSession() + if err != nil { + log.Fatalf("Couldn't get OpenAI session: %v", err) + } - if err = persistentConfig.SetSessionToken(token); err != nil { - log.Fatalf("Couldn't save OpenAI session: %v", err) + err = config.Set("OpenAISession", session) + if err != nil { + log.Fatalf("Couldn't save OpenAI session: %v", err) + } + } else + { + err = config.Set("OpenAISession", os.Getenv("OPENAI_SESSION")) + if err != nil { + log.Fatalf("Couldn't save OpenAI session: %v", err) + } } } chatGPT := chatgpt.Init(persistentConfig) log.Println("Started ChatGPT") - envConfig, err := config.LoadEnvConfig(".env") - if err != nil { - log.Printf("Couldn't load .env file: %v. Using shell exposed env variables...", err) - } - - bot, err := tgbot.New(envConfig.TelegramToken, time.Duration(envConfig.EditWaitSeconds)) + bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_TOKEN")) if err != nil { log.Fatalf("Couldn't start Telegram bot: %v", err) }