Skip to content

Commit

Permalink
feat: load configs from config.env file
Browse files Browse the repository at this point in the history
  • Loading branch information
Jisin0 committed Jul 21, 2024
1 parent d339398 commit 13ed088
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 8 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ go.work
go.work.sum

# Vscode workspace configs
/.vscode
/.vscode

# Local environment configs
config.env
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ id - Get user id or forwarded channel id.

## Variables

Variables can also be loaded by creating a ```config.env``` file at the root of the repository. See [sample.config.env](/sample.config.env) to see the format to use.

- `BOT_TOKEN` : Optional. On vercel, a list of bot tokens allowed to connect to the app or leave empty allow anyone to connect. On servers, a single bot token.
- `ADMINS` : Optional. List of telegram IDs of users allowed to create links with the bot, seperated by spaces.
- `FSUB` : Optional. List of IDs of channels the user must join to get content, seperated by spaces.
Expand Down
31 changes: 24 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"os"
"strconv"
"strings"

"github.com/joho/godotenv"
)

const (
Expand All @@ -17,15 +19,30 @@ const (
)

var (
DBChannel = int64Environ("DB_CHANNEL") // id of database channel
Admins = int64ListEnviron("ADMINS") // admins list
FsubChannels = int64ListEnviron("FSUB") // list of force subscribe channels
BatchSizeLimit = int64Environ("BATCH_SIZE_LIMIT", defaultBatchLimit) // maximum messages allowed in a batch
DisableNotification = strings.ToLower(os.Getenv("DISABLE_NOTIFICATION")) == stringTrue // messages will be sent without a notification
ProtectContent = strings.ToLower(os.Getenv("PUBLIC_CONTENT")) == stringTrue // disable forwarding content from bot
AllowPublic = strings.ToLower(os.Getenv("ALLOW_PUBLIC")) == stringTrue || len(Admins) < 1 // indicates wether anyone can use the bot
DBChannel int64 // id of database channel
Admins []int64 // admins list
FsubChannels []int64 // list of force subscribe channels
BatchSizeLimit int64 // maximum messages allowed in a batch
DisableNotification bool // messages will be sent without a notification
ProtectContent bool // disable forwarding content from bot
AllowPublic bool // indicates wether anyone can use the bot
)

func init() {
err := godotenv.Load("config.env")
if err == nil {
fmt.Println("configs loaded from config.env file")
}

DBChannel = int64Environ("DB_CHANNEL")
Admins = int64ListEnviron("ADMINS")
FsubChannels = int64ListEnviron("FSUB")
BatchSizeLimit = int64Environ("BATCH_SIZE_LIMIT", defaultBatchLimit)
DisableNotification = strings.ToLower(os.Getenv("DISABLE_NOTIFICATION")) == stringTrue
ProtectContent = strings.ToLower(os.Getenv("PUBLIC_CONTENT")) == stringTrue
AllowPublic = strings.ToLower(os.Getenv("ALLOW_PUBLIC")) == stringTrue || len(Admins) < 1
}

// int64Environ gets a environment variable as an int64.
func int64Environ(envVar string, defaultVal ...int64) int64 {
s := os.Getenv(envVar)
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ module github.com/Jisin0/TGMessageStore
go 1.22.1

require github.com/PaulSonOfLars/gotgbot/v2 v2.0.0-rc.28

require github.com/joho/godotenv v1.5.1
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/PaulSonOfLars/gotgbot/v2 v2.0.0-rc.28 h1:3EidAXUUuDBwaRX5881fmpGGv2WPnW9oHwRMlvdQiwU=
github.com/PaulSonOfLars/gotgbot/v2 v2.0.0-rc.28/go.mod h1:kL1v4iIjlalwm3gCYGvF4NLa3hs+aKEfRkNJvj4aoDU=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
3 changes: 3 additions & 0 deletions sample.config.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BOT_TOKEN = 123456789:AATiuhBBy6oIL9r54yYLs7CSn
FSUB = -1002235842999 -1001898719761
PROTECT_CONTENT = true

0 comments on commit 13ed088

Please sign in to comment.