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"
|
|
|
|
"kickerbot/db"
|
2021-12-03 12:19:55 +03:00
|
|
|
"log"
|
2022-11-09 00:45:08 +03:00
|
|
|
"strings"
|
2021-12-03 12:19:55 +03:00
|
|
|
"time"
|
|
|
|
|
2022-11-09 00:45:08 +03:00
|
|
|
tb "github.com/NicoNex/echotron/v3"
|
2021-12-03 12:19:55 +03:00
|
|
|
)
|
|
|
|
|
2022-11-09 00:45:08 +03:00
|
|
|
type bot struct {
|
|
|
|
chatID int64
|
|
|
|
CaptchaTopic int64
|
|
|
|
Me *tb.User
|
|
|
|
tb.API
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *bot) Update(update *tb.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
|
|
|
|
Dispatcher *tb.Dispatcher
|
2021-12-03 12:19:55 +03:00
|
|
|
}
|
|
|
|
|
2022-11-09 00:45:08 +03:00
|
|
|
func (b *Kicker) NewBot(chatID int64) tb.Bot {
|
|
|
|
d := db.GetDatabase()
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
chat := db.Chat{
|
|
|
|
Id: chatID,
|
|
|
|
Title: "",
|
|
|
|
TopicId: 0,
|
|
|
|
}
|
|
|
|
if !d.ChatExists(ctx, chat) {
|
|
|
|
if err := d.NewChat(ctx, chat); err != nil {
|
|
|
|
return &bot{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
chat, _ = d.GetChat(ctx, chatID)
|
|
|
|
CaptchaTopic := chat.TopicId
|
|
|
|
result := &bot{
|
|
|
|
chatID,
|
|
|
|
CaptchaTopic,
|
|
|
|
nil,
|
|
|
|
tb.NewAPI(b.Token),
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
dsp := tb.NewDispatcher(b.Token, b.NewBot)
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
func EscapeText(parseMode tb.ParseMode, text string) string {
|
|
|
|
var replacer *strings.Replacer
|
|
|
|
|
|
|
|
if parseMode == tb.HTML {
|
|
|
|
replacer = strings.NewReplacer("<", "<", ">", ">", "&", "&")
|
|
|
|
} else if parseMode == tb.Markdown {
|
|
|
|
replacer = strings.NewReplacer("_", "\\_", "*", "\\*", "`", "\\`", "[", "\\[")
|
|
|
|
} else if parseMode == tb.MarkdownV2 {
|
|
|
|
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
|
|
|
}
|