-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
65 lines (51 loc) · 1.85 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
package main
import (
"os"
"time"
tele "gopkg.in/telebot.v3"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"github.com/s4lat/gosavingsbot/handlers"
"github.com/s4lat/gosavingsbot/locale"
"github.com/s4lat/gosavingsbot/log"
"github.com/s4lat/gosavingsbot/middleware"
"github.com/s4lat/gosavingsbot/models"
)
func main() {
log.InitLoggers()
locale.InitLocales()
log.InfoLogger.Print("Connecting to db...")
db, err := gorm.Open(sqlite.Open("./data/data.db"), &gorm.Config{})
if err != nil {
log.ErrorLogger.Fatal("Failed to connect database")
}
db.AutoMigrate(&models.Spend{}, &models.User{})
pref := tele.Settings{
Token: os.Getenv("BOT_TOKEN"),
Poller: &tele.LongPoller{Timeout: 10 * time.Second},
}
b, err := tele.NewBot(pref)
if err != nil {
log.ErrorLogger.Fatal(err)
return
}
b.Use(
middleware.PassData(map[string]interface{}{"db": db}),
middleware.SetLang(),
)
b.Handle("/help", handlers.StartHandler)
b.Handle("/set_lang", handlers.LangAskHandler)
b.Handle("/start", handlers.StartHandler, middleware.SetLocation())
b.Handle("/delete_my_data", handlers.AskToDeleteUserData, middleware.SetLocation())
b.Handle("Today", handlers.DaySpendsHandler, middleware.SetLocation())
b.Handle("Сегодня", handlers.DaySpendsHandler, middleware.SetLocation())
b.Handle("Statistics", handlers.YearSpendsHandler, middleware.SetLocation())
b.Handle("Статистика", handlers.YearSpendsHandler, middleware.SetLocation())
b.Handle("Settings", handlers.SettingsHandler, middleware.SetLocation())
b.Handle("Настройки", handlers.SettingsHandler, middleware.SetLocation())
b.Handle(tele.OnLocation, handlers.LocationHandler)
b.Handle(tele.OnText, handlers.OnTextHandler, middleware.SetLocation())
b.Handle(tele.OnCallback, handlers.CallbackHandler, middleware.SetLocation())
log.InfoLogger.Print("Starting bot...")
b.Start()
}