diff --git a/config/text.go b/config/text.go index 1bd4070..da51e71 100644 --- a/config/text.go +++ b/config/text.go @@ -42,18 +42,6 @@ var Commands map[string]string = map[string]string{ // Message that is sent when an unrecognized command is sent. var CommandNotFound = "😐 I don't recognize that command !\nCheck /help to see how to use me." -// GetCommand returns the content for a command. -func GetCommand(command string) (string, [][]gotgbot.InlineKeyboardButton) { - command = strings.ToUpper(command) - - text, ok := Commands[command] - if !ok { - text = CommandNotFound // default msg if not found - } - - return text, Buttons[command] -} - // Batch command texts. var ( // Unauthorized use of /batch @@ -89,3 +77,25 @@ Add the bot to your channel and forward the post and use this command as a reply /genlink https://t.me/c/123456789/69 ` ) + +// Miscellaneous. +var ( + // malformed start link + InvalidLink = "I'm sorry there's something wrong with this link 😕" + // fetching batch messages + StartGetBatch = "Fetching your content..." +) + +// GetCommand returns the content for a command. +func GetCommand(command string) (string, [][]gotgbot.InlineKeyboardButton) { + command = strings.ToUpper(command) + + text, ok := Commands[command] + if !ok { + text = CommandNotFound // default msg if not found + } + + return text, Buttons[command] +} + +// GetCommandText returns only text for a command. diff --git a/plugins/start.go b/plugins/start.go index fa03e46..9fb0e35 100644 --- a/plugins/start.go +++ b/plugins/start.go @@ -5,8 +5,13 @@ package plugins import ( + "encoding/base64" + "fmt" + "strings" + "github.com/Jisin0/TGMessageStore/config" "github.com/Jisin0/TGMessageStore/utils/format" + "github.com/Jisin0/TGMessageStore/utils/url" "github.com/PaulSonOfLars/gotgbot/v2" "github.com/PaulSonOfLars/gotgbot/v2/ext" ) @@ -15,6 +20,59 @@ func Start(bot *gotgbot.Bot, ctx *ext.Context) error { update := ctx.EffectiveMessage user := ctx.EffectiveUser - update.Reply(bot, format.BasicFormat(config.Commands["START"], user), &gotgbot.SendMessageOpts{}) + split := strings.Fields(update.Text) + if len(split) < 2 { + text, buttons := config.GetCommand("START") + update.Reply(bot, format.BasicFormat(text, user), &gotgbot.SendMessageOpts{ReplyMarkup: gotgbot.InlineKeyboardMarkup{InlineKeyboard: buttons}, ParseMode: gotgbot.ParseModeHTML}) + return nil + } + + rawData, err := base64.RawStdEncoding.DecodeString(split[1]) + if err != nil { + fmt.Println(err) + update.Reply(bot, format.BasicFormat(config.InvalidLink, user), &gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML}) + return nil + } + + startData := string(rawData) + + chatID, startID, endID, err := url.DecodeData(startData) + if err != nil { + fmt.Println(err) + update.Reply(bot, format.BasicFormat(config.InvalidLink, user), &gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML}) + return nil + } + + sendBatch(bot, update, chatID, startID, endID) + return nil } + +// sendBatch sends a batch from the input data to the target. +func sendBatch(bot *gotgbot.Bot, inputMessage *gotgbot.Message, chatID, startID, endID int64) { + statMessage, err := inputMessage.Reply(bot, format.BasicFormat(config.StartGetBatch, inputMessage.From), &gotgbot.SendMessageOpts{ParseMode: gotgbot.ParseModeHTML}) + if err != nil { + fmt.Printf("sendBatch: failed to send stat: %v\n", err) + return + } + + for i := startID; i <= endID; i++ { + _, err := bot.CopyMessage(inputMessage.Chat.Id, chatID, i, &gotgbot.CopyMessageOpts{}) + if err != nil { + switch { + case strings.Contains(err.Error(), "chat not found"): + statMessage.EditText(bot, format.BasicFormat(config.BatchUnknownChat, inputMessage.From), &gotgbot.EditMessageTextOpts{}) + return + case strings.Contains(err.Error(), "message not found"): + // ignore and continue + case strings.Contains(err.Error(), "flood"): + fmt.Println("cancelled batch due to flood") + return + default: + fmt.Printf("sendBatch: unknown error: %v", err) + } + } + } + + statMessage.Delete(bot, &gotgbot.DeleteMessageOpts{}) +}