Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dockerfile and ability to set session token via env variable #72

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
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

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"]
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
8 changes: 8 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
services:
chatgpt-telegram:
build: .
container_name: chatgpt-telegram
environment:
- TELEGRAM_ID=
- TELEGRAM_TOKEN=
- OPENAI_SESSION=
1 change: 1 addition & 0 deletions env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
TELEGRAM_ID=
TELEGRAM_TOKEN=
EDIT_WAIT_SECONDS=1
OPENAI_SESSION=
39 changes: 23 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +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 err = persistentConfig.SetSessionToken(token); err != nil {
log.Fatalf("Couldn't save OpenAI session: %v", 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)
}

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.Fatalf("Couldn't load .env config: %v", err)
}
if err := envConfig.ValidateWithDefaults(); err != nil {
log.Fatalf("Invalid .env config: %v", 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)
}
Expand Down