diff --git a/drafts.go b/drafts.go index cb3492d..64f5712 100644 --- a/drafts.go +++ b/drafts.go @@ -5,7 +5,6 @@ import ( "sync/atomic" "git.nix13.pw/scuroneko/laniakea/tgapi" - "git.nix13.pw/scuroneko/laniakea/utils" ) type draftIdGenerator interface { @@ -88,7 +87,7 @@ func (d *DraftProvider) NewDraft() *Draft { } func (d *Draft) push(text string, escapeMd bool) error { if escapeMd { - text += utils.EscapeMarkdownV2(text) + text += EscapeMarkdownV2(text) } else { text += text } diff --git a/msg_context.go b/msg_context.go index 229c2a8..2d2b3ac 100644 --- a/msg_context.go +++ b/msg_context.go @@ -5,7 +5,6 @@ import ( "fmt" "git.nix13.pw/scuroneko/laniakea/tgapi" - "git.nix13.pw/scuroneko/laniakea/utils" "git.nix13.pw/scuroneko/slog" ) @@ -35,12 +34,15 @@ type AnswerMessage struct { ctx *MsgContext } -func (ctx *MsgContext) edit(messageId int, text string, keyboard *InlineKeyboard) *AnswerMessage { +func (ctx *MsgContext) edit(messageId int, text string, keyboard *InlineKeyboard, escapeMd bool) *AnswerMessage { params := tgapi.EditMessageTextP{ MessageID: messageId, ChatID: ctx.Msg.Chat.ID, Text: text, - ParseMode: tgapi.ParseMD, + ParseMode: tgapi.ParseMDV2, + } + if escapeMd { + params.Text = EscapeMarkdownV2(text) } if keyboard != nil { params.ReplyMarkup = keyboard.Get() @@ -55,7 +57,10 @@ func (ctx *MsgContext) edit(messageId int, text string, keyboard *InlineKeyboard } } func (m *AnswerMessage) Edit(text string) *AnswerMessage { - return m.ctx.edit(m.MessageID, text, nil) + return m.ctx.edit(m.MessageID, text, nil, true) +} +func (m *AnswerMessage) EditMarkdown(text string) *AnswerMessage { + return m.ctx.edit(m.MessageID, text, nil, false) } func (ctx *MsgContext) EditCallback(text string, keyboard *InlineKeyboard) *AnswerMessage { if ctx.CallbackMsgId == 0 { @@ -63,7 +68,15 @@ func (ctx *MsgContext) EditCallback(text string, keyboard *InlineKeyboard) *Answ return nil } - return ctx.edit(ctx.CallbackMsgId, text, keyboard) + return ctx.edit(ctx.CallbackMsgId, text, keyboard, true) +} +func (ctx *MsgContext) EditCallbackMarkdown(text string, keyboard *InlineKeyboard) *AnswerMessage { + if ctx.CallbackMsgId == 0 { + ctx.botLogger.Errorln("Can't edit non-callback update message") + return nil + } + + return ctx.edit(ctx.CallbackMsgId, text, keyboard, false) } func (ctx *MsgContext) EditCallbackf(format string, keyboard *InlineKeyboard, args ...any) *AnswerMessage { return ctx.EditCallback(fmt.Sprintf(format, args...), keyboard) @@ -106,7 +119,7 @@ func (ctx *MsgContext) answer(text string, keyboard *InlineKeyboard, escapeMd bo ParseMode: tgapi.ParseMDV2, } if escapeMd { - params.Text = utils.EscapeMarkdownV2(text) + params.Text = EscapeMarkdownV2(text) } if keyboard != nil { params.ReplyMarkup = keyboard.Get() @@ -159,7 +172,7 @@ func (ctx *MsgContext) answerPhoto(photoId, text string, kb *InlineKeyboard, esc Photo: photoId, } if escapeMd { - params.Caption = utils.EscapeMarkdownV2(text) + params.Caption = EscapeMarkdownV2(text) } if kb != nil { params.ReplyMarkup = kb.Get() diff --git a/utils.go b/utils.go index 0afbcfc..ed48cf6 100644 --- a/utils.go +++ b/utils.go @@ -1,6 +1,10 @@ package laniakea -import "git.nix13.pw/scuroneko/laniakea/utils" +import ( + "strings" + + "git.nix13.pw/scuroneko/laniakea/utils" +) func Ptr[T any](v T) *T { return &v } func Val[T any](p *T, def T) T { @@ -9,7 +13,39 @@ func Val[T any](p *T, def T) T { } return def } -func EscapeMarkdown(s string) string { return utils.EscapeMarkdown(s) } -func EscapeMarkdownV2(s string) string { return utils.EscapeMarkdownV2(s) } + +// EscapeMarkdown +// Deprecated. Use MarkdownV2 +func EscapeMarkdown(s string) string { + s = strings.ReplaceAll(s, "_", `\_`) + s = strings.ReplaceAll(s, "*", `\*`) + s = strings.ReplaceAll(s, "[", `\[`) + return strings.ReplaceAll(s, "`", "\\`") +} + +// EscapeHTML escapes special characters for Telegram HTML parse mode. +func EscapeHTML(s string) string { + s = strings.ReplaceAll(s, "&", "&") + s = strings.ReplaceAll(s, "<", "<") + s = strings.ReplaceAll(s, ">", ">") + return s +} + +// EscapeMarkdownV2 escapes special characters for Telegram MarkdownV2. +// https://core.telegram.org/bots/api#markdownv2-style +func EscapeMarkdownV2(s string) string { + symbols := []string{"\\", "_", "*", "[", "]", "(", ")", "~", "`", ">", "#", "+", "-", "=", "|", "{", "}", ".", "!"} + for _, symbol := range symbols { + s = strings.ReplaceAll(s, symbol, "\\"+symbol) + } + return s +} +func EscapePunctuation(s string) string { + symbols := []string{".", "!", "-"} + for _, symbol := range symbols { + s = strings.ReplaceAll(s, symbol, "\\"+symbol) + } + return s +} const VersionString = utils.VersionString diff --git a/utils/utils.go b/utils/utils.go index 2727c05..51ce3c6 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -2,7 +2,6 @@ package utils import ( "os" - "strings" "git.nix13.pw/scuroneko/slog" ) @@ -14,29 +13,3 @@ func GetLoggerLevel() slog.LogLevel { } return level } - -// EscapeMarkdown Deprecated. Use MarkdownV2 -func EscapeMarkdown(s string) string { - s = strings.ReplaceAll(s, "_", `\_`) - s = strings.ReplaceAll(s, "*", `\*`) - s = strings.ReplaceAll(s, "[", `\[`) - return strings.ReplaceAll(s, "`", "\\`") -} - -// EscapeHTML escapes special characters for Telegram HTML parse mode. -func EscapeHTML(s string) string { - s = strings.ReplaceAll(s, "&", "&") - s = strings.ReplaceAll(s, "<", "<") - s = strings.ReplaceAll(s, ">", ">") - return s -} - -// EscapeMarkdownV2 escapes special characters for Telegram MarkdownV2. -// https://core.telegram.org/bots/api#markdownv2-style -func EscapeMarkdownV2(s string) string { - symbols := []string{"_", "*", "[", "]", "(", ")", "~", "`", ">", "#", "+", "-", "=", "|", "{", "}", ".", "!", "\\"} - for _, symbol := range symbols { - s = strings.ReplaceAll(s, symbol, "\\"+symbol) - } - return s -}