-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
185 lines (159 loc) · 4.73 KB
/
service.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package tgbot
import (
"context"
"fmt"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/guionardo/go-tgbot/tgbot/config"
"github.com/guionardo/go-tgbot/tgbot/infra"
"github.com/guionardo/go-tgbot/tgbot/runners"
"gorm.io/gorm"
)
type GoTGBotService struct {
BotRunner
bot *tgbotapi.BotAPI
context context.Context
listener *BotListener
worker *BotWorker
publisher *BotPublisher
cancel context.CancelFunc
stop context.CancelFunc
repository IRepository
configuration *config.Configuration
setupLevel SetupLevel
}
// CreateBotService creates a new empty bot service
// This is the first step to create a bot service
//
// After this step, you should:
//
// * LoadConfigurationFromFile or LoadConfigurationFromEnv
//
// * InitBot
//
// * AddRepository (optional, if not set, automatic repository will be created)
func CreateBotService() *GoTGBotService {
return &GoTGBotService{
setupLevel: Instance,
}
}
func (svc *GoTGBotService) LoadConfigurationFromFile(filename string) *GoTGBotService {
cfg, err := config.CreateConfigurationFromFile(filename)
if err != nil {
panic(err)
}
svc.configuration = cfg
svc.setupLevel = Set(svc.setupLevel, Configuration)
return svc
}
func (svc *GoTGBotService) LoadConfigurationFromEnv(prefix string) *GoTGBotService {
cfg, err := config.CreateConfigurationFromEnv(prefix)
if err != nil {
panic(err)
}
svc.configuration = cfg
svc.setupLevel = Set(svc.setupLevel, Configuration)
return svc
}
// InitBot setups bot, loggin
func (svc *GoTGBotService) InitBot() *GoTGBotService {
if !Has(svc.setupLevel, Configuration) || svc.configuration == nil {
panic("configuration not set")
}
// Setup logging
infra.CreateLoggerFactory(&svc.configuration.Logging)
svc.logger = infra.GetLogger("GoTGBotService")
svc.logger.Infof("Init %s", svc.name)
// Setup bot
if err := tgbotapi.SetLogger(infra.CreateBotLogger(infra.GetLogger("tgbot"))); err != nil {
panic(err)
}
bot, err := tgbotapi.NewBotAPI(svc.configuration.Bot.Token)
if err != nil {
panic(err)
}
svc.bot = bot
svc.logger.Infof("Authorized on account %s", bot.Self.UserName)
svc.setupLevel = Set(svc.setupLevel, Init)
// Create listener
svc.listener = createBotListener(svc.bot)
// Create worker
svc.worker = createBotWorker()
// Create publisher
svc.publisher = createBotPublisher()
svc.setupLevel = Set(svc.setupLevel, Workers)
svc.Init("GoTGBotService")
return svc
}
// SetDatabase sets the database for the bot repository
//
// If not set, the bot repository will be created with the default connection string
func (svc *GoTGBotService) SetDatabase(dbs ...*gorm.DB) *GoTGBotService {
var db *gorm.DB
var err error
if len(dbs) == 0 {
// Create default SQLite repository from connection string
db, err = infra.GetSQLiteDB(svc.configuration.Repository.ConnectionString)
} else {
db = dbs[0]
}
if err != nil {
panic(err)
}
svc.repository = infra.CreateMessageRepository(db)
svc.setupLevel = Set(svc.setupLevel, Repository)
return svc
}
// SetRepository sets the repository for the bot
//
// You must implement a IRepository interface and pass it to this method
//
// Do not need to use SetDatabase
func (svc *GoTGBotService) SetRepository(repository IRepository) *GoTGBotService {
svc.repository = repository
svc.setupLevel = Set(svc.setupLevel, Repository)
return svc
}
func (svc *GoTGBotService) Start() error {
if svc.cancel != nil {
return fmt.Errorf("service already started")
}
if !Has(svc.setupLevel, Init) {
return fmt.Errorf("service not initialized by svc.InitBot()")
}
if !Has(svc.setupLevel, Workers) {
return fmt.Errorf("service not initialized by svc.AddWorkers()")
}
if !Has(svc.setupLevel, Repository) {
svc.logger.Warnf("automatic repository created: %s", svc.configuration.Repository.ConnectionString)
svc.SetDatabase()
}
botContext, cancel := CreateBotContext(svc)
svc.cancel = cancel
runnerCollection := runners.NewRunnerCollection()
runnerCollection.CreateRunnerCustomLoop("listener", BotListenerAction, svc)
runnerCollection.CreateRunnerCustomLoop("publisher", BotPublisherAction, svc)
runnerCollection.CreateRunner("worker", BotWorkerRunnerAction, svc)
runnerCollection.RunAll(botContext)
return nil
}
func (svc *GoTGBotService) Stop() error {
if svc.cancel == nil {
return fmt.Errorf("service not started")
}
svc.logger.Info("stopping")
svc.cancel()
svc.logger.Info("stopped")
return nil
}
func (svc *GoTGBotService) Publisher() *BotPublisher {
return svc.publisher
}
func (svc *GoTGBotService) Repository() IRepository {
return svc.repository
}
func (svc *GoTGBotService) Listener() *BotListener {
return svc.listener
}
func (svc *GoTGBotService) Configuration() *config.Configuration {
return svc.configuration
}