kickerbot_ng/src/handlers.go

231 lines
6.2 KiB
Go

package main
import (
"context"
"database/sql"
"errors"
"fmt"
"log"
"strconv"
"strings"
"time"
"github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
)
func defaultHandler(ctx context.Context, b *bot.Bot, update *models.Update) {
// b.SendMessage(ctx, &bot.SendMessageParams{
// ChatID: update.Message.Chat.ID,
// Text: "hello!",
// })
if update.Message != nil {
log.Println(update.Message.Text)
}
log.Println(*update)
}
func registerChat(ctx context.Context, b *bot.Bot, update *models.Update) {
msg := update.Message
log.Println("registering", msg.Chat.ID)
m, err := FetchMemberFromChat(ctx, b, msg.Chat.ID, msg.From.ID)
if err == nil {
if !IsAdmin(*m) {
log.Println("register: user is not admin")
return
}
args := strings.Split(msg.Text, " ")
if len(args) == 1 {
log.Println("register: there's no code")
return
}
if !UseActivation(args[1]) {
log.Println("register: wrong code: ", args[1])
return
}
if err := ActivateChat(msg.Chat.ID); err != nil {
log.Println("Error activating chat: ", err)
return
}
b.DeleteMessage(ctx, &bot.DeleteMessageParams{
ChatID: msg.Chat.ID,
MessageID: msg.ID,
})
sent, err := b.SendMessage(ctx, &bot.SendMessageParams{
ChatID: msg.Chat.ID,
Text: "Чат зарегистрирован",
MessageThreadID: msg.MessageThreadID,
})
if err == nil {
err := DeleteAfterSeconds(msg.Chat.ID, sent.ID, 60)
if err != nil {
log.Println("register: failed to add to delete", err)
}
}
} else {
log.Println("register: error", err)
}
}
func handleNewJoined(ctx context.Context, b *bot.Bot, u *models.Update) {
for _, user := range u.Message.NewChatMembers {
msg, _ := b.SendMessage(ctx, &bot.SendMessageParams{
ChatID: u.Message.Chat.ID,
Text: "Check the capthca!",
ReplyMarkup: models.InlineKeyboardMarkup{
InlineKeyboard: [][]models.InlineKeyboardButton{
{
{URL: fmt.Sprintf("https://t.me/nefrace_php_test_bot?start=cap_%d", u.Message.Chat.ID), Text: "Check"},
},
},
},
})
_, err := db.Exec(`INSERT INTO captchas (user_id, chat_id, message_id) values ($1, $2, $3)`, user.ID, u.Message.Chat.ID, msg.ID)
if err != nil {
log.Println("newusers: can't add to db: ", err)
return
}
_, err = b.RestrictChatMember(ctx, &bot.RestrictChatMemberParams{
ChatID: u.Message.Chat.ID,
UserID: user.ID,
Permissions: &models.ChatPermissions{
CanSendMessages: false,
CanSendAudios: false,
CanSendDocuments: false,
CanSendPhotos: false,
CanSendVideos: false,
CanSendVideoNotes: false,
CanSendVoiceNotes: false,
CanSendPolls: false,
CanSendOtherMessages: false,
CanAddWebPagePreviews: false,
CanChangeInfo: false,
CanInviteUsers: false,
CanPinMessages: false,
CanManageTopics: false,
},
})
if err != nil {
log.Println("Can't restrict new user: ", err)
}
}
}
func banUser(ctx context.Context, b *bot.Bot, u *models.Update) {
}
func handlePrivateStartCaptcha(ctx context.Context, b *bot.Bot, u *models.Update) {
args := strings.Split(u.Message.Text, " ")
userID := u.Message.From.ID
chatID := 0
if len(args) > 1 {
chat_str := strings.TrimPrefix(args[1], "cap_")
id, err := strconv.Atoi(chat_str)
if err == nil {
chatID = id
}
}
captcha := Captcha{}
var err error
if chatID != 0 {
err = db.Get(&captcha, `select * from captchas where user_id = $1 and chat_id = $2`, userID, chatID)
if errors.Is(err, sql.ErrNoRows) {
b.SendMessage(ctx, &bot.SendMessageParams{
Text: " There's no captchas for that chat you came from.",
ChatID: u.Message.Chat.ID,
})
return
}
} else {
err = db.Get(&captcha, `select * from captchas where user_id = $1 and correct_answer != 0`, userID)
if err != nil {
b.SendMessage(ctx, &bot.SendMessageParams{
Text: " There's no captchas for that chat you came from.",
ChatID: u.Message.Chat.ID,
})
return
}
}
if captcha.CorrectAnswer == 0 {
captcha.CorrectAnswer = 42
msg, err := b.SendMessage(ctx, &bot.SendMessageParams{
Text: "Get me the answer!",
ChatID: u.Message.Chat.ID,
})
if err == nil {
captcha.MessageID = msg.ID
}
_, err = db.NamedExec("update captchas set correct_answer = :correct_answer, message_id = :message_id where id = :id", captcha)
if err != nil {
log.Println("Can't update captcha:", err)
}
} else {
b.SendMessage(ctx, &bot.SendMessageParams{
Text: fmt.Sprintf("You already have captcha for chat %d", captcha.ChatID),
ChatID: u.Message.Chat.ID,
})
}
}
func handlePrivateCaptcha(ctx context.Context, b *bot.Bot, u *models.Update) {
msg := u.Message
captcha := Captcha{}
err := db.Get(&captcha, "select * from captchas where user_id = $1 and correct_answer != 0", msg.From.ID)
if err != nil {
log.Println("Can't solve user captcha: ", err)
return
}
now := time.Now().Unix()
if captcha.BlockedUntil > now {
log.Println("User needs to try again next time: ", msg.From.ID, msg.From.Username, msg.From.FirstName)
return
}
ban_minutes := 0
num, err := strconv.Atoi(msg.Text)
text := "That's not a number. Try again in 30 minutes."
if err != nil {
ban_minutes = 30
} else if num != captcha.CorrectAnswer {
text = "That's the wrong answer. Try again in 5 hours."
ban_minutes = 300
}
if ban_minutes > 0 {
b.SendMessage(ctx, &bot.SendMessageParams{ChatID: msg.From.ID, Text:text})
_, err := db.Exec("update captchas set blocked_until = $2 where id = $1", captcha.Id, time.Now().Add(time.Minute * time.Duration(ban_minutes)).Unix())
if err != nil {
log.Println("Can't block user from captcha: ", err)
}
return
}
_, err = b.RestrictChatMember(ctx, &bot.RestrictChatMemberParams{ChatID: captcha.ChatID, UserID: captcha.UserID, Permissions: &models.ChatPermissions{
CanSendMessages: true,
CanSendAudios: true,
CanSendDocuments: true,
CanSendPhotos: true,
CanSendVideos: true,
CanSendVideoNotes: true,
CanSendVoiceNotes: true,
CanSendPolls: true,
CanSendOtherMessages: true,
CanAddWebPagePreviews: true,
CanChangeInfo: true,
CanInviteUsers: true,
CanPinMessages: true,
CanManageTopics: true,
}})
if err != nil {
log.Println("Can't unrestrict user: ", err)
}
b.SendMessage(ctx, &bot.SendMessageParams{Text: "Captcha solved! Congrats!", ChatID: msg.From.ID})
}