refactor: migrate to gotd (wip)

This commit is contained in:
krau
2024-11-08 23:00:57 +08:00
parent 32bd391129
commit fbdfc04ad8
14 changed files with 465 additions and 387 deletions

View File

@@ -2,12 +2,29 @@ package bot
import (
"fmt"
"regexp"
"github.com/amarnathcjd/gogram/telegram"
"github.com/celestix/gotgproto/dispatcher"
"github.com/celestix/gotgproto/types"
"github.com/gotd/td/tg"
"github.com/krau/SaveAny-Bot/storage"
)
func supportedMediaFilter(m *types.Message) (bool, error) {
if not := m.Media == nil; not {
return false, dispatcher.EndGroups
}
switch m.Media.(type) {
case *tg.MessageMediaDocument:
return true, nil
case *tg.MessageMediaWebPage:
return false, dispatcher.EndGroups
case tg.MessageMediaClass:
return false, dispatcher.EndGroups
default:
return false, nil
}
}
var StorageDisplayNames = map[string]string{
"all": "全部",
"local": "服务器磁盘",
@@ -15,48 +32,40 @@ var StorageDisplayNames = map[string]string{
"webdav": "WebDAV",
}
func AddTaskReplyMarkup(messageID int32) telegram.ReplyMarkup {
// TODO: sort storage buttons
storageButtons := make([]telegram.KeyboardButton, 0)
for name := range storage.Storages {
storageButtons = append(storageButtons, &telegram.KeyboardButtonCallback{
func getAddTaskMarkup(messageID int) *tg.ReplyInlineMarkup {
storageButtons := make([]tg.KeyboardButtonClass, 0)
for _, name := range storage.StorageKeys {
storageButtons = append(storageButtons, &tg.KeyboardButtonCallback{
Text: StorageDisplayNames[string(name)],
Data: []byte(fmt.Sprintf("add %d %s", messageID, name)),
})
}
if len(storageButtons) > 1 {
return &telegram.ReplyInlineMarkup{
Rows: []*telegram.KeyboardButtonRow{
if len(storageButtons) < 1 {
return nil
}
if len(storageButtons) == 1 {
return &tg.ReplyInlineMarkup{
Rows: []tg.KeyboardButtonRow{
{
Buttons: storageButtons,
},
{
Buttons: []telegram.KeyboardButton{
&telegram.KeyboardButtonCallback{
Text: "全部",
Data: []byte(fmt.Sprintf("add %d all", messageID)),
},
},
}
}
return &tg.ReplyInlineMarkup{
Rows: []tg.KeyboardButtonRow{
{
Buttons: storageButtons,
},
{
Buttons: []tg.KeyboardButtonClass{
&tg.KeyboardButtonCallback{
Text: "全部",
Data: []byte(fmt.Sprintf("add %d all", messageID)),
},
},
},
}
},
}
if len(storageButtons) == 1 {
return &telegram.ReplyInlineMarkup{
Rows: []*telegram.KeyboardButtonRow{
{
Buttons: storageButtons,
},
},
}
}
return nil
}
var markdownRe = regexp.MustCompile("([" + regexp.QuoteMeta(`\_*[]()~`+"`"+`>#+-=|{}.!`) + "])")
func EscapeMarkdown(text string) string {
return markdownRe.ReplaceAllString(text, "\\$1")
}