Skip to content

Commit

Permalink
feat(batch): handle batch links
Browse files Browse the repository at this point in the history
  • Loading branch information
Jisin0 committed Jul 19, 2024
1 parent 8f44196 commit c738bdf
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 13 deletions.
34 changes: 22 additions & 12 deletions config/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -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>😐 I don't recognize that command !\nCheck /help to see how to use me.</i>"

// 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
Expand Down Expand Up @@ -89,3 +77,25 @@ Add the bot to your channel and forward the post and use this command as a reply
<code>/genlink https://t.me/c/123456789/69</code>
</blockquote>`
)

// Miscellaneous.
var (
// malformed start link
InvalidLink = "<i>I'm sorry there's something wrong with this link 😕</i>"
// fetching batch messages
StartGetBatch = "<i><b>Fetching your content...</b></i>"
)

// 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.
60 changes: 59 additions & 1 deletion plugins/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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{})
}

0 comments on commit c738bdf

Please sign in to comment.