KickerBot/kicker/kicker.go

152 lines
3.4 KiB
Go
Raw Normal View History

2021-12-03 14:30:10 +03:00
package kicker
2021-12-03 12:19:55 +03:00
import (
2022-11-09 00:45:08 +03:00
"context"
"fmt"
2022-11-09 00:45:08 +03:00
"kickerbot/db"
2021-12-03 12:19:55 +03:00
"log"
"regexp"
2022-11-09 00:45:08 +03:00
"strings"
2021-12-03 12:19:55 +03:00
"time"
2023-02-14 00:50:21 +03:00
"git.nefrace.ru/nefrace/tongo"
"github.com/NicoNex/echotron/v3"
2021-12-03 12:19:55 +03:00
)
2023-02-14 00:50:21 +03:00
var Client *tongo.Database
2022-11-09 00:45:08 +03:00
type bot struct {
chatID int64
CaptchaTopic int64
2023-02-14 00:50:21 +03:00
Me *echotron.User
echotron.API
2022-11-09 00:45:08 +03:00
}
2023-02-14 00:50:21 +03:00
func (b *bot) Update(update *echotron.Update) {
2022-11-09 01:23:32 +03:00
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)
}
2022-11-09 00:45:08 +03:00
}
2022-11-09 01:23:32 +03:00
userJoined(b, update)
return
2022-11-09 00:45:08 +03:00
}
2022-11-09 01:23:32 +03:00
if update.Message.LeftChatMember != nil {
userLeft(b, update)
2022-11-09 00:45:08 +03:00
return
}
2022-11-09 01:23:32 +03:00
if update.Message.Text != "" {
if update.Message.Text == "/settopic" {
setTopic(b, update)
return
}
checkCaptcha(b, update)
}
2022-11-09 00:45:08 +03:00
}
2021-12-03 12:19:55 +03:00
}
2021-12-03 14:35:07 +03:00
// Базовая структура для бота
2021-12-03 14:30:10 +03:00
type Kicker struct {
2022-11-09 00:45:08 +03:00
Token string
2023-02-14 00:50:21 +03:00
Dispatcher *echotron.Dispatcher
2021-12-03 12:19:55 +03:00
}
2023-02-14 00:50:21 +03:00
func (b *Kicker) NewBot(chatID int64) echotron.Bot {
2023-02-14 01:15:00 +03:00
store := tongo.NewStore[db.Chat](Client)
2022-11-09 00:45:08 +03:00
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
2023-02-14 11:16:17 +03:00
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 {
2022-11-09 00:45:08 +03:00
return &bot{}
}
}
CaptchaTopic := chat.TopicId
result := &bot{
chatID,
CaptchaTopic,
nil,
2023-02-14 00:50:21 +03:00
echotron.NewAPI(b.Token),
2022-11-09 00:45:08 +03:00
}
2023-02-14 11:17:01 +03:00
log.Println("New bot created with CaptchaTopic", result.CaptchaTopic)
2022-11-09 00:45:08 +03:00
me, err := result.GetMe()
2021-12-03 12:19:55 +03:00
if err != nil {
2022-11-09 00:45:08 +03:00
log.Println(err)
2021-12-03 12:19:55 +03:00
}
2022-11-09 00:45:08 +03:00
result.Me = me.Result
return result
}
// Initialize bot with token
func (b *Kicker) Init() error {
2023-02-14 00:50:21 +03:00
dsp := echotron.NewDispatcher(b.Token, b.NewBot)
2022-11-09 00:45:08 +03:00
b.Dispatcher = dsp
2021-12-03 12:19:55 +03:00
return nil
}
2022-11-09 00:45:08 +03:00
func (b *Kicker) Start() error {
return b.Dispatcher.Poll()
}
2023-02-14 00:50:21 +03:00
func EscapeText(parseMode echotron.ParseMode, text string) string {
2022-11-09 00:45:08 +03:00
var replacer *strings.Replacer
2023-02-14 00:50:21 +03:00
if parseMode == echotron.HTML {
2022-11-09 00:45:08 +03:00
replacer = strings.NewReplacer("<", "&lt;", ">", "&gt;", "&", "&amp;")
2023-02-14 00:50:21 +03:00
} else if parseMode == echotron.Markdown {
2022-11-09 00:45:08 +03:00
replacer = strings.NewReplacer("_", "\\_", "*", "\\*", "`", "\\`", "[", "\\[")
2023-02-14 00:50:21 +03:00
} else if parseMode == echotron.MarkdownV2 {
2022-11-09 00:45:08 +03:00
replacer = strings.NewReplacer(
"_", "\\_", "*", "\\*", "[", "\\[", "]", "\\]", "(",
"\\(", ")", "\\)", "~", "\\~", "`", "\\`", ">", "\\>",
"#", "\\#", "+", "\\+", "-", "\\-", "=", "\\=", "|",
"\\|", "{", "\\{", "}", "\\}", ".", "\\.", "!", "\\!",
)
} else {
return ""
2021-12-03 12:19:55 +03:00
}
2022-11-09 00:45:08 +03:00
return replacer.Replace(text)
2021-12-03 12:19:55 +03:00
}
2023-02-14 00:50:21 +03:00
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)
2023-02-14 00:50:21 +03:00
}