2023-01-24 02:24:45 +03:00
|
|
|
package main
|
|
|
|
|
2023-01-24 23:17:30 +03:00
|
|
|
import (
|
2023-09-14 01:46:42 +03:00
|
|
|
"context"
|
2023-06-16 01:06:51 +03:00
|
|
|
"encoding/json"
|
2023-09-14 01:46:42 +03:00
|
|
|
"errors"
|
2023-01-24 23:17:30 +03:00
|
|
|
"fmt"
|
2023-06-07 21:21:54 +03:00
|
|
|
"log"
|
2023-06-15 17:03:57 +03:00
|
|
|
"time"
|
2023-01-24 23:17:30 +03:00
|
|
|
|
|
|
|
"git.nefrace.ru/nefrace/nechotron"
|
|
|
|
"git.nefrace.ru/nefrace/tongo"
|
|
|
|
)
|
|
|
|
|
2023-06-13 02:22:44 +03:00
|
|
|
// Docs
|
2023-04-13 11:13:24 +03:00
|
|
|
|
2023-06-13 02:22:44 +03:00
|
|
|
func handleMe(u *nechotron.Update, text string) error {
|
|
|
|
u.DeleteMessage()
|
|
|
|
_, err := u.AnswerMarkdown(fmt.Sprintf("_*%s* %s_", nechotron.EscapeMd2(u.From().FirstName), nechotron.EscapeMd2(text)))
|
2023-09-14 01:46:42 +03:00
|
|
|
if err != nil {
|
|
|
|
return errors.Join(fmt.Errorf("can't handle /me: %s", u.Text()), err)
|
|
|
|
}
|
|
|
|
return nil
|
2023-02-13 01:40:20 +03:00
|
|
|
}
|
|
|
|
|
2023-06-13 02:22:44 +03:00
|
|
|
var helpText = `
|
|
|
|
Вот мои команды:
|
|
|
|
%s
|
2023-06-08 01:44:38 +03:00
|
|
|
|
2023-06-13 02:22:44 +03:00
|
|
|
Время сборки бота: %s`
|
|
|
|
|
|
|
|
func handleHelp(u *nechotron.Update, text string) error {
|
|
|
|
commands := nechotron.MakeCommandList("`%s` \\- _%s_\n", commandHelp, commandMe)
|
|
|
|
_, err := u.AnswerMarkdown(fmt.Sprintf(helpText, commands, BuildTime))
|
2023-09-14 01:46:42 +03:00
|
|
|
if err != nil {
|
|
|
|
return errors.Join(fmt.Errorf("can't handle /help: %s", u.Text()), err)
|
|
|
|
}
|
|
|
|
return nil
|
2023-06-08 01:44:38 +03:00
|
|
|
}
|
|
|
|
|
2023-06-13 02:22:44 +03:00
|
|
|
func handleSay(u *nechotron.Update, text string) error {
|
|
|
|
u.DeleteMessage()
|
2023-06-15 17:03:57 +03:00
|
|
|
if text == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2023-06-13 02:22:44 +03:00
|
|
|
_, err := u.AnswerMarkdown(fmt.Sprintf("*_%s_*", nechotron.EscapeMd2(text)))
|
2023-09-14 01:46:42 +03:00
|
|
|
if err != nil {
|
|
|
|
return errors.Join(fmt.Errorf("can't handle /say: %s", u.Text()), err)
|
2023-04-13 11:13:24 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleDeleteCallback(u *nechotron.Update) error {
|
|
|
|
u.DeleteMessage()
|
|
|
|
return nil
|
2023-01-24 02:24:45 +03:00
|
|
|
}
|
2023-06-13 02:22:44 +03:00
|
|
|
|
|
|
|
func handleUsersImport(u *nechotron.Update) error {
|
|
|
|
f, _ := u.Bot.GetFile(u.Message.Document.FileID)
|
|
|
|
file, _ := u.Bot.DownloadFile(f.Result.FilePath)
|
2023-06-16 01:06:51 +03:00
|
|
|
var users []oldUser
|
|
|
|
|
|
|
|
err := json.Unmarshal(file, &users)
|
|
|
|
if err != nil {
|
|
|
|
u.AnswerPlain(err.Error())
|
|
|
|
}
|
2023-09-14 01:46:42 +03:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
|
|
|
|
defer cancel()
|
2023-06-16 01:06:51 +03:00
|
|
|
store := tongo.NewStore[User](db)
|
|
|
|
for _, user := range users {
|
|
|
|
if user.MessagesCount == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
log.Printf("%+v", user)
|
|
|
|
newUser := User{
|
2023-09-14 01:46:42 +03:00
|
|
|
Item: tongo.NewID(),
|
|
|
|
Username: user.Username,
|
|
|
|
Name: user.Name,
|
|
|
|
ID: user.ID,
|
|
|
|
Karma: user.Karma,
|
2023-06-16 01:06:51 +03:00
|
|
|
}
|
2023-09-14 01:46:42 +03:00
|
|
|
_, err := store.InsertOne(ctx, &newUser)
|
2023-06-16 01:06:51 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
2023-06-13 02:22:44 +03:00
|
|
|
return nil
|
|
|
|
}
|
2023-06-15 17:03:57 +03:00
|
|
|
|
2023-06-16 01:06:51 +03:00
|
|
|
type oldUser struct {
|
|
|
|
ID int64 `json:"uid"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
MessagesCount int `json:"messagesCount"`
|
|
|
|
Name string `json:"full_name"`
|
|
|
|
Karma int64 `json:"karma"`
|
|
|
|
}
|
|
|
|
|
2023-06-15 17:03:57 +03:00
|
|
|
func handleRegisterFeed(u *nechotron.Update, _ string) error {
|
|
|
|
feedChats := tongo.NewStore[FeedChat](db)
|
|
|
|
chat, err := feedChats.GetOne(u.Ctx, tongo.E("chatid", u.ChatID()))
|
|
|
|
if err != nil {
|
|
|
|
chat = &FeedChat{
|
|
|
|
Item: tongo.NewID(),
|
|
|
|
ChatID: u.ChatID(),
|
|
|
|
Title: u.Message.Chat.Title,
|
|
|
|
ThreadID: u.Message.ThreadID,
|
|
|
|
}
|
|
|
|
feedChats.InsertOne(u.Ctx, chat)
|
|
|
|
res, _ := u.AnswerPlain("Чат зарегистрирован для рассылки")
|
|
|
|
go func() {
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
u.Bot.DeleteMessage(u.ChatID(), res.Result.ID)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleDeleteFeed(u *nechotron.Update, _ string) error {
|
|
|
|
feedChats := tongo.NewStore[FeedChat](db)
|
|
|
|
chat, err := feedChats.GetOne(u.Ctx, tongo.E("chatid", u.ChatID()))
|
|
|
|
if err != nil {
|
|
|
|
res, _ := u.AnswerPlain("Чат не подписан на рассылку")
|
|
|
|
go func() {
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
u.Bot.DeleteMessage(u.ChatID(), res.Result.ID)
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
feedChats.DeleteByID(u.Ctx, chat.Id)
|
|
|
|
res, _ := u.AnswerPlain("Чат удалён из рассылки")
|
|
|
|
go func() {
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
u.Bot.DeleteMessage(u.ChatID(), res.Result.ID)
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|