-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.go
More file actions
97 lines (79 loc) · 2.56 KB
/
Copy pathmain.go
File metadata and controls
97 lines (79 loc) · 2.56 KB
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
package main
import (
"context"
"os"
"os/signal"
"time"
"github.com/JackBekket/hellper/lib/bot/handlers"
"github.com/JackBekket/hellper/lib/config"
"github.com/JackBekket/hellper/lib/database"
"github.com/go-telegram/bot"
"github.com/joho/godotenv"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func main() {
log.Logger = log.Output(zerolog.ConsoleWriter{
Out: os.Stdout,
TimeFormat: time.DateTime,
TimeLocation: time.Local,
})
//In the future, a check for empty variables in the .env file should be implemented
err := godotenv.Load()
if err != nil {
log.Fatal().Err(err).Msg("failed to load .env file")
}
log.Info().Msg(".env file loaded successfully")
token := os.Getenv("TG_KEY")
dbLink := os.Getenv("DB_LINK")
dbHandler, err := database.NewHandler(dbLink)
if err != nil {
log.Fatal().Err(err).Msg("failed to create database service")
}
if err := dbHandler.DB.Ping(); err != nil {
log.Fatal().Err(err).Msg("failed to ping database")
}
log.Info().Msg("database ping successful")
db_service, err := database.NewAIService(dbHandler)
if err != nil {
log.Fatal().Err(err).Msg("something wrong")
}
cache := database.NewMemoryCache()
botHandlers := handlers.NewHandlersBot(
cache, db_service, dbLink,
&config.AIConfig{
ModelsListEndpoint: os.Getenv("MODELS_LIST_ENDPOINT"),
ImageGenerationModel: os.Getenv("IMAGE_GENERATION_MODEL"),
ImageGenerationEndpoint: os.Getenv("IMAGE_GENERATION_ENDPOINT"),
ImageRecognitionModel: os.Getenv("IMAGE_RECOGNITION_MODEL"),
ImageRecognitionEndpoint: os.Getenv("IMAGE_RECOGNITION_ENDPOINT"),
VoiceRecognitionModel: os.Getenv("VOICE_RECOGNITION_MODEL"),
VoiceRecognitionEndpoint: os.Getenv("VOICE_RECOGNITION_ENDPOINT"),
},
)
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
opts := []bot.Option{
bot.WithMiddlewares(botHandlers.IdentifyUserMiddleware),
}
tgb, err := bot.New(token, opts...)
if err != nil {
log.Fatal().Err(err).Msg("token is missing")
}
botHandlers.NewRegisterHandlers(ctx, tgb)
botSelf, err := tgb.GetMe(ctx)
if err != nil {
log.Fatal().Err(err).Msg("invalid token")
}
go tgb.Start(ctx)
log.Info().Msg("Bot is starting")
log.Info().Int64("id", botSelf.ID).Msgf("authorized on account: %s", botSelf.Username)
<-ctx.Done()
log.Info().Msg("Termination signal received. Shutting down...")
if err := dbHandler.DB.Close(); err != nil {
log.Error().Err(err).Msg("failed to close database connection")
} else {
log.Info().Msg("database connection closed")
}
log.Info().Msg("Completed.")
}