package kicker import ( "context" "fmt" "kickerbot/db" "log" "regexp" "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, err := store.GetOne(ctx, tongo.E("chat_id", chatID)) if err != nil { chat = &db.Chat{ Item: tongo.NewID(), ChatId: chatID, Title: "", TopicId: 0, } if _, err := store.InsertOne(ctx, chat); err != nil { return &bot{} } } CaptchaTopic := chat.TopicId result := &bot{ chatID, CaptchaTopic, nil, echotron.NewAPI(b.Token), } log.Println("New bot created with CaptchaTopic", result.CaptchaTopic) 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) if _, err := b.DeleteMessage(message.Chat.ID, message.ID); err != nil { log.Printf("Can't delay-delete message: %v", err) } } func MentionUser(user *echotron.User) string { return fmt.Sprintf("[%s](tg://user?id=%d)", EscapeText(echotron.MarkdownV2, user.FirstName), user.ID) } var chars = []string{"_", "\\*", "\\[", "\\]", "\\(", "\\)", "~", "`", ">", "#", "\\+", "\\-", "=", "|", "{", "}", "\\.", "!"} var r = strings.Join(chars, "") var reg = regexp.MustCompile("[" + r + "]+") func EscapeMd2(s string) string { return reg.ReplaceAllString(s, "\\$0") } func Mention(name string, id int64) string { return fmt.Sprintf("[%s](tg://user?id=%d)", EscapeMd2(name), id) } func UserMention(u *echotron.User) string { return Mention(u.FirstName, u.ID) } func UserMentionDB(u *db.User) string { return Mention(u.FirstName, u.UserId) }