71 lines
1.5 KiB
Go
71 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
|
|
"git.zio.sh/astra/telegram-approval-join/config"
|
|
"git.zio.sh/astra/telegram-approval-join/handlers"
|
|
api "github.com/OvyFlash/telegram-bot-api"
|
|
)
|
|
|
|
func main() {
|
|
cfg := config.Config{}
|
|
if err := cfg.LoadConfig(); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
if cfg.BotToken == nil {
|
|
log.Fatal("Edit config.yaml and fill out bot token")
|
|
}
|
|
|
|
bot, err := api.NewBotAPI(*cfg.BotToken)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
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
|
|
|
|
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)
|
|
}
|
|
}
|