/
main.go
123 lines (102 loc) · 2.82 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"context"
"flag"
"fmt"
"os"
"time"
"github.com/alicerum/pilly/pkg/commands/dispatch"
"github.com/alicerum/pilly/pkg/config"
pillyDB "github.com/alicerum/pilly/pkg/db"
"github.com/alicerum/pilly/pkg/db/users"
"github.com/rs/zerolog/log"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
func printErrAndExit(errMsg string, err error) {
fmt.Fprintf(os.Stderr, "%v: %v\n", errMsg, err)
os.Exit(1)
}
func main() {
configPath := flag.String("config", "", "config path")
flag.Parse()
ctx := context.Background()
cfg, err := config.ReadConfig(configPath)
if err != nil {
printErrAndExit("error while reading config file", err)
}
fmt.Printf("token is %v\n", cfg.Bot.Token)
if err := cfg.Log.SetLogger(); err != nil {
printErrAndExit("could not set logging level", err)
}
db, err := cfg.DB.CreateDB(ctx)
if err != nil {
printErrAndExit("could not connect to the db", err)
}
defer db.Close()
dbSvc := pillyDB.New(db)
dispatcher := dispatch.NewDispatcher(dbSvc)
bot, err := tgbotapi.NewBotAPI(cfg.Bot.Token)
if err != nil {
printErrAndExit("could not create bot api", err)
}
upd := tgbotapi.NewUpdate(0)
upd.Timeout = 60
updates := bot.GetUpdatesChan(upd)
for update := range updates {
// ignore all public communications
if !update.FromChat().IsPrivate() {
continue
}
if update.Message != nil {
user := users.User{
ID: update.Message.From.ID,
Username: update.Message.From.UserName,
FirstName: update.Message.From.FirstName,
LastName: update.Message.From.LastName,
Created: time.Now().UTC(),
}
dbSvc.Users().Persist(&user)
if update.Message.IsCommand() {
response, err := dispatcher.ProcessCmd(update.Message)
if err != nil {
log.Error().Err(err).Msg("could not process tg message")
continue
}
_, err = bot.Send(response)
if err != nil {
log.Error().Err(err).Msg("could not send response")
continue
}
} else {
msg, err := dispatcher.ProcessInput(update.Message)
if err != nil {
log.Error().Err(err).Msg("error while processing user input")
continue
}
if msg == nil {
log.Debug().
Int64("userID", update.Message.From.ID).
Msg("not expected input from user")
continue
}
if _, err := bot.Send(msg); err != nil {
log.Error().Err(err).Msg("could not send input response to user")
}
}
}
if update.CallbackQuery != nil {
cbk, msg, err := dispatcher.ProcessCallback(update.CallbackQuery)
if err != nil {
log.Error().Err(err).Msg("could not process callback")
continue
}
if _, err := bot.Request(cbk); err != nil {
log.Error().Err(err).Msg("could not request callback")
continue
}
if _, err := bot.Send(msg); err != nil {
log.Error().Err(err).Msg("could not send callback response")
}
}
}
}