Skip to content

Commit

Permalink
Add doc for timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
leafduo committed Mar 4, 2023
1 parent b69fb5c commit 1364068
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export ALLOWED_TELEGRAM_ID=<your_telegram_id>,<your_friend_telegram_id>
# optional, default is 1.0. Higher temperature means more random responses.
# See https://platform.openai.com/docs/api-reference/chat/create#chat/create-temperature
export MODEL_TEMPERATURE=1.0
# optional, default is 900. Max idle duration for a certain conversation.
# After this duration, a new conversation will be started.
export CONVERSATION_IDLE_TIMEOUT_SECONDS=900

chatgpt-telegram-bot
```
13 changes: 7 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import (
)

var cfg struct {
TelegramAPIToken string `env:"TELEGRAM_APITOKEN"`
OpenAIAPIKey string `env:"OPENAI_API_KEY"`
ModelTemperature float32 `env:"MODEL_TEMPERATURE" envDefault:"1.0"`
AllowedTelegramID []int64 `env:"ALLOWED_TELEGRAM_ID" envSeparator:","`
ConversationTimeoutSeconds int `env:"CONVERSATION_TIMEOUT_SECONDS" envDefault:"900"`
TelegramAPIToken string `env:"TELEGRAM_APITOKEN"`
OpenAIAPIKey string `env:"OPENAI_API_KEY"`
ModelTemperature float32 `env:"MODEL_TEMPERATURE" envDefault:"1.0"`
AllowedTelegramID []int64 `env:"ALLOWED_TELEGRAM_ID" envSeparator:","`
ConversationIdleTimeoutSeconds int `env:"CONVERSATION_IDLE_TIMEOUT_SECONDS" envDefault:"900"`
}

type User struct {
Expand Down Expand Up @@ -220,7 +220,8 @@ func handleUserPrompt(userID int64, msg string) (string, bool, error) {

func clearUserContextIfExpires(userID int64) bool {
user := users[userID]
if user != nil && user.LastActiveTime.Add(time.Duration(cfg.ConversationTimeoutSeconds)*time.Second).Before(time.Now()) {
if user != nil &&
user.LastActiveTime.Add(time.Duration(cfg.ConversationIdleTimeoutSeconds)*time.Second).Before(time.Now()) {
resetUser(userID)
return true
}
Expand Down

0 comments on commit 1364068

Please sign in to comment.