-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (49 loc) · 1.62 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
package main
import (
"time"
"github.com/cloudedcat/debt-bot/bot"
"github.com/cloudedcat/debt-bot/bunt"
"github.com/cloudedcat/debt-bot/calculator"
"github.com/cloudedcat/debt-bot/handle"
"github.com/cloudedcat/debt-bot/log"
"github.com/cloudedcat/debt-bot/manager"
"github.com/cloudedcat/debt-bot/model"
"github.com/tidwall/buntdb"
tb "gopkg.in/tucnak/telebot.v2"
)
func newBuntRepositories(db *buntdb.DB) (
model.GroupRepository, model.ParticipantRepository, model.DebtRepository) {
return bunt.NewGroupRepository(db), bunt.NewParticipantRepository(db), bunt.NewDebtRepository(db)
}
func main() {
logger := log.NewZapLogger()
logger.Infow("Bot initializing...")
db, err := bunt.Open(config.DBName, true)
if err != nil {
logger.Fatalw(err.Error())
}
defer func() {
err := db.Close()
logger.IfErrorw(err, "failed to close db")
}()
groups, partics, debts := newBuntRepositories(db)
managerService := manager.NewService(groups, partics)
calculatorService := calculator.NewService(debts, partics)
_ = calculatorService
bot, err := bot.NewTelegramBot(tb.Settings{
Token: config.BotToken,
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
}, logger)
if err != nil {
logger.Fatalw(err.Error())
}
logger.Infow("Bot authorized")
handle.AddToChat(bot, managerService, logger)
handle.ListParticipants(bot, managerService, logger)
handle.RegisterParticipant(bot, managerService, logger)
handle.ShareDebt(bot, calculatorService, logger)
handle.Calculate(bot, calculatorService, logger)
handle.ShowDebtHistory(bot, calculatorService, logger)
handle.Amnesty(bot, calculatorService, logger)
bot.Start()
}