-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.go
87 lines (72 loc) · 2.16 KB
/
bot.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
package main
import (
"cloud.google.com/go/vertexai/genai"
"context"
"github.com/getsentry/sentry-go"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/sashabaranov/go-openai"
"github.com/shabablinchikow/nafanya-bot/internal/aihandler"
"github.com/shabablinchikow/nafanya-bot/internal/cfg"
"github.com/shabablinchikow/nafanya-bot/internal/domain"
"github.com/shabablinchikow/nafanya-bot/internal/tghandler"
"google.golang.org/api/option"
"gorm.io/gorm"
"gorm.io/gorm/schema"
"log"
"time"
)
//nolint:funlen
func main() {
// Load the config from the environment variables
config := cfg.LoadConfig()
var env string
if config.DebugMode {
env = "development"
} else {
env = "production"
}
errSentry := sentry.Init(sentry.ClientOptions{
Dsn: config.SentryDSN,
AttachStacktrace: true,
TracesSampleRate: 1.0,
EnableTracing: true,
Environment: env,
})
if errSentry != nil {
log.Fatalf("sentry.Init: %s", errSentry)
}
defer sentry.Recover()
defer sentry.Flush(2 * time.Second)
aiOAI := openai.NewClient(config.OAIToken)
aiGoogle, err2 := genai.NewClient(context.Background(), "gnomed-1695577860628", "europe-west3", option.WithCredentialsJSON([]byte(config.GoogleToken)))
if err2 != nil {
sentry.CaptureException(err2)
log.Panic(err2)
}
aiHndlr := aihandler.NewHandler(aiOAI, aiGoogle)
dbDSN := "host=" + config.DBHost + " user=" + config.DBUser + " password=" + config.DBPass + " dbname=" + config.DBName + " port=" + config.DBPort + " sslmode=" + config.DBSSL
dbConfig := &gorm.Config{
NamingStrategy: schema.NamingStrategy{
TablePrefix: config.DBPrefix,
},
}
db, err := domain.NewHandler(dbDSN, dbConfig, config.DefaultAdmin)
if err != nil {
sentry.CaptureException(err)
log.Panic(err)
}
bot, err2 := tgbotapi.NewBotAPI(config.BotToken)
if err2 != nil {
sentry.CaptureException(err2)
log.Panic(err2)
}
bot.Debug = config.DebugMode
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates := bot.GetUpdatesChan(u)
handler := tghandler.NewHandler(bot, aiHndlr, db)
for update := range updates {
handler.HandleEvents(update)
}
}