package kicker import ( "context" "kickerbot/db" "log" "strings" "time" "git.nefrace.ru/nefrace/tongo" "github.com/NicoNex/echotron/v3" ) var Client *tongo.Database type bot struct { chatID int64 CaptchaTopic int64 Me *echotron.User echotron.API } func (b *bot) Update(update *echotron.Update) { if update.Message != nil { if len(update.Message.NewChatMembers) != 0 { for _, user := range update.Message.NewChatMembers { if user.ID == b.Me.ID { botAdded(b, update) } } userJoined(b, update) return } if update.Message.LeftChatMember != nil { userLeft(b, update) return } if update.Message.Text != "" { if update.Message.Text == "/settopic" { setTopic(b, update) return } checkCaptcha(b, update) } } } // Базовая структура для бота type Kicker struct { Token string Dispatcher *echotron.Dispatcher } func (b *Kicker) NewBot(chatID int64) echotron.Bot { store := tongo.NewStore[db.Chat](Client) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() chat := db.Chat{ Item: tongo.NewID(), ChatId: chatID, Title: "", TopicId: 0, } if _, err := store.GetOne(ctx, tongo.E("chat_id", chatID)); err != nil { if _, err := store.InsertOne(ctx, &chat); err != nil { return &bot{} } } CaptchaTopic := chat.TopicId result := &bot{ chatID, CaptchaTopic, nil, echotron.NewAPI(b.Token), } me, err := result.GetMe() if err != nil { log.Println(err) } result.Me = me.Result return result } // Initialize bot with token func (b *Kicker) Init() error { dsp := echotron.NewDispatcher(b.Token, b.NewBot) b.Dispatcher = dsp return nil } func (b *Kicker) Start() error { return b.Dispatcher.Poll() } func EscapeText(parseMode echotron.ParseMode, text string) string { var replacer *strings.Replacer if parseMode == echotron.HTML { replacer = strings.NewReplacer("<", "<", ">", ">", "&", "&") } else if parseMode == echotron.Markdown { replacer = strings.NewReplacer("_", "\\_", "*", "\\*", "`", "\\`", "[", "\\[") } else if parseMode == echotron.MarkdownV2 { replacer = strings.NewReplacer( "_", "\\_", "*", "\\*", "[", "\\[", "]", "\\]", "(", "\\(", ")", "\\)", "~", "\\~", "`", "\\`", ">", "\\>", "#", "\\#", "+", "\\+", "-", "\\-", "=", "\\=", "|", "\\|", "{", "\\{", "}", "\\}", ".", "\\.", "!", "\\!", ) } else { return "" } return replacer.Replace(text) } func waitAndDelete(b *echotron.API, message *echotron.Message, t time.Duration) { time.Sleep(t) b.DeleteMessage(message.Chat.ID, message.ID) }