66 lines
1.4 KiB
Go
66 lines
1.4 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() {
|
|
b := &handlers.Bot{}
|
|
b.Config = config.Config{}
|
|
err := b.Config.LoadConfig()
|
|
if err != nil {
|
|
log.Fatal(err.Error())
|
|
}
|
|
|
|
if b.Config.BotToken == nil {
|
|
log.Fatal("Edit config.yaml and fill out bot token")
|
|
}
|
|
bot, err := api.NewBotAPI(*b.Config.BotToken)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
b.API = bot
|
|
b.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)
|
|
}
|
|
}
|
|
}
|