Skip to content

Commit 655fd9c

Browse files
committed
feat: support docker compose env
1 parent 9733804 commit 655fd9c

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
TG_TOKEN=
2+
TG_CHATID=

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ RUN set -e; \
2020
chmod 0755 /var/log/aio /etc/aio; \
2121
LATEST=$(curl -sL https://api.github.com/repos/uerax/all-in-one-bot/releases/latest | grep "tag_name" | cut -d '"' -f 4); \
2222
curl -L "https://github.com/uerax/all-in-one-bot/releases/download/$LATEST/$AIO_BIN" -o /usr/local/bin/aio; \
23-
chmod +x /usr/local/bin/aio;
23+
chmod +x /usr/local/bin/aio; \
24+
curl -L "https://raw.githubusercontent.com/uerax/all-in-one-bot/refs/heads/master/all-in-one-bot.yml" -o /etc/aio/all-in-one-bot.yml;
2425

2526
VOLUME ["/var/log/aio"]
2627
VOLUME ["/etc/aio"]

docker-compose.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ services:
1212
# and restart the container with: docker compose restart bot
1313
- ./config:/etc/aio
1414
- ./logs:/var/log/aio
15+
environment:
16+
TG_TOKEN: ${TG_TOKEN}
17+
TG_CHATID: ${TG_CHATID}
1518
networks:
1619
- botnet
1720
healthcheck:

tg/server.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package tg
22

33
import (
44
"log"
5+
"os"
6+
"strconv"
57
"time"
68

79
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
@@ -13,14 +15,30 @@ var ChatId int64
1315

1416
func Server() {
1517
// Create a new bot instance
16-
token, err := goconf.VarString("telegram", "token")
17-
if err != nil {
18-
log.Println("启动失败: 没有填写bot的token")
19-
return
18+
token, _ := goconf.VarString("telegram", "token")
19+
20+
envToken := os.Getenv("TG_TOKEN")
21+
if envToken != "" {
22+
token = envToken
23+
}
24+
25+
if token == "" {
26+
// 此时,无论是 goconf 还是 TG_TOKEN 都没有提供有效的非空值
27+
log.Println("启动失败: 没有填写bot的token,请检查配置文件或TG_TOKEN环境变量")
28+
return
2029
}
21-
id := goconf.VarIntOrDefault(0, "telegram", "chatId")
2230

31+
id := goconf.VarIntOrDefault(0, "telegram", "chatId")
2332
ChatId = int64(id)
33+
envID := os.Getenv("TG_CHATID")
34+
if envID != "" {
35+
id, err := strconv.ParseInt(envID, 10, 64)
36+
if err != nil {
37+
log.Printf("ChatId转换失败: %v\n", err)
38+
} else {
39+
ChatId = id
40+
}
41+
}
2442

2543
api.NewBot(token, goconf.VarStringOrDefault("http://localhost:8081/", "telegram", "local"))
2644

0 commit comments

Comments
 (0)