-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (56 loc) · 1.21 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"context"
"log"
"os"
"os/signal"
"strings"
"trade_calc_bot/utils"
"github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
"github.com/joho/godotenv"
)
func main() {
var err error
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
systemEnv := make(map[string]string)
for _, env := range os.Environ() {
kv := strings.Split(env, "=")
systemEnv[kv[0]] = kv[1]
}
if utils.IsProduction() {
err = godotenv.Load(".env")
} else {
err = godotenv.Load(".env.development")
}
if err != nil {
log.Fatal("Error loading .env file: ", err)
}
for k, v := range systemEnv {
os.Setenv(k, v)
}
token := os.Getenv("BOT_TOKEN")
opts := []bot.Option{
bot.WithDefaultHandler(defaultHandler),
}
b, err := bot.New(token, opts...)
if err != nil {
panic(err)
}
b.Start(ctx)
}
// 默认处理函数
func defaultHandler(ctx context.Context, b *bot.Bot, update *models.Update) {
if update.Message == nil {
return
}
if update.Message.Text == "/start" {
if !taskManager.HasTask(update.Message.Chat.ID) {
t := NewTask(update.Message.Chat.ID)
taskManager.AddTask(t)
}
NewMainMenu().Show(ctx, b, update.Message.Chat.ID)
return
}
}