code refactor
This commit is contained in:
parent
7275b93666
commit
d157f9b2c9
6 changed files with 187 additions and 193 deletions
85
main.go
85
main.go
|
|
@ -9,58 +9,63 @@ import (
|
|||
)
|
||||
|
||||
func main() {
|
||||
b := &handlers.Bot{}
|
||||
b.Config = config.Config{}
|
||||
err := b.Config.LoadConfig()
|
||||
if err != nil {
|
||||
log.Fatal(err.Error())
|
||||
cfg := config.Config{}
|
||||
if err := cfg.LoadConfig(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if b.Config.BotToken == nil {
|
||||
if cfg.BotToken == nil {
|
||||
log.Fatal("Edit config.yaml and fill out bot token")
|
||||
}
|
||||
bot, err := api.NewBotAPI(*b.Config.BotToken)
|
||||
|
||||
bot, err := api.NewBotAPI(*cfg.BotToken)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
b.API = bot
|
||||
b.WaitingForApproval = make(map[int64]*handlers.ExtendedChatJoinRequest)
|
||||
b := &handlers.Bot{
|
||||
API: bot,
|
||||
Config: cfg,
|
||||
WaitingForApproval: make(map[int64]*handlers.ExtendedChatJoinRequest),
|
||||
}
|
||||
|
||||
log.Printf("Authorized on account %s", bot.Self.UserName)
|
||||
|
||||
updateConfig := api.NewUpdate(0)
|
||||
updateConfig.Timeout = 60
|
||||
updatesChannel := b.API.GetUpdatesChan(updateConfig)
|
||||
|
||||
for update := range updatesChannel {
|
||||
if update.ChatJoinRequest != nil {
|
||||
if update.ChatJoinRequest.Chat.ID == *b.Config.TargetChatId {
|
||||
b.HandleJoinRequest(update.ChatJoinRequest)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if update.CallbackQuery != nil {
|
||||
b.HandleCallbackQuery(update.CallbackQuery)
|
||||
continue
|
||||
}
|
||||
|
||||
if update.Message == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if user, ok := b.WaitingForApproval[update.Message.From.ID]; ok {
|
||||
if update.Message.Chat.ID == update.Message.From.ID {
|
||||
b.HandleJoinRequestResponse(user, update.Message)
|
||||
}
|
||||
}
|
||||
|
||||
if update.Message.Chat.ID == *b.Config.AdminChatId {
|
||||
if update.Message.ReplyToMessage != nil {
|
||||
b.HandleDeclineReason(&update)
|
||||
}
|
||||
b.HandleAdminCommands(&update)
|
||||
}
|
||||
for update := range b.API.GetUpdatesChan(updateConfig) {
|
||||
processUpdate(b, update)
|
||||
}
|
||||
}
|
||||
|
||||
func processUpdate(b *handlers.Bot, update api.Update) {
|
||||
if update.ChatJoinRequest != nil {
|
||||
if update.ChatJoinRequest.Chat.ID == *b.Config.TargetChatId {
|
||||
b.HandleJoinRequest(update.ChatJoinRequest)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if update.CallbackQuery != nil {
|
||||
b.HandleCallbackQuery(update.CallbackQuery)
|
||||
return
|
||||
}
|
||||
|
||||
if update.Message == nil || update.Message.From == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if user := b.GetPendingUser(update.Message.From.ID); user != nil {
|
||||
if update.Message.Chat.ID == update.Message.From.ID {
|
||||
b.HandleJoinRequestResponse(user, update.Message)
|
||||
}
|
||||
}
|
||||
|
||||
if update.Message.Chat.ID == *b.Config.AdminChatId {
|
||||
if update.Message.ReplyToMessage != nil {
|
||||
b.HandleDeclineReason(&update)
|
||||
}
|
||||
b.HandleAdminCommands(&update)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue