some fixes and small changes

This commit is contained in:
2026-03-06 14:31:45 +03:00
parent 4dc172a3b5
commit 8b9a974da9
4 changed files with 60 additions and 39 deletions

View File

@@ -5,7 +5,6 @@ import (
"sync/atomic" "sync/atomic"
"git.nix13.pw/scuroneko/laniakea/tgapi" "git.nix13.pw/scuroneko/laniakea/tgapi"
"git.nix13.pw/scuroneko/laniakea/utils"
) )
type draftIdGenerator interface { type draftIdGenerator interface {
@@ -88,7 +87,7 @@ func (d *DraftProvider) NewDraft() *Draft {
} }
func (d *Draft) push(text string, escapeMd bool) error { func (d *Draft) push(text string, escapeMd bool) error {
if escapeMd { if escapeMd {
text += utils.EscapeMarkdownV2(text) text += EscapeMarkdownV2(text)
} else { } else {
text += text text += text
} }

View File

@@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"git.nix13.pw/scuroneko/laniakea/tgapi" "git.nix13.pw/scuroneko/laniakea/tgapi"
"git.nix13.pw/scuroneko/laniakea/utils"
"git.nix13.pw/scuroneko/slog" "git.nix13.pw/scuroneko/slog"
) )
@@ -35,12 +34,15 @@ type AnswerMessage struct {
ctx *MsgContext 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{ params := tgapi.EditMessageTextP{
MessageID: messageId, MessageID: messageId,
ChatID: ctx.Msg.Chat.ID, ChatID: ctx.Msg.Chat.ID,
Text: text, Text: text,
ParseMode: tgapi.ParseMD, ParseMode: tgapi.ParseMDV2,
}
if escapeMd {
params.Text = EscapeMarkdownV2(text)
} }
if keyboard != nil { if keyboard != nil {
params.ReplyMarkup = keyboard.Get() 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 { 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 { func (ctx *MsgContext) EditCallback(text string, keyboard *InlineKeyboard) *AnswerMessage {
if ctx.CallbackMsgId == 0 { if ctx.CallbackMsgId == 0 {
@@ -63,7 +68,15 @@ func (ctx *MsgContext) EditCallback(text string, keyboard *InlineKeyboard) *Answ
return nil 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 { func (ctx *MsgContext) EditCallbackf(format string, keyboard *InlineKeyboard, args ...any) *AnswerMessage {
return ctx.EditCallback(fmt.Sprintf(format, args...), keyboard) 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, ParseMode: tgapi.ParseMDV2,
} }
if escapeMd { if escapeMd {
params.Text = utils.EscapeMarkdownV2(text) params.Text = EscapeMarkdownV2(text)
} }
if keyboard != nil { if keyboard != nil {
params.ReplyMarkup = keyboard.Get() params.ReplyMarkup = keyboard.Get()
@@ -159,7 +172,7 @@ func (ctx *MsgContext) answerPhoto(photoId, text string, kb *InlineKeyboard, esc
Photo: photoId, Photo: photoId,
} }
if escapeMd { if escapeMd {
params.Caption = utils.EscapeMarkdownV2(text) params.Caption = EscapeMarkdownV2(text)
} }
if kb != nil { if kb != nil {
params.ReplyMarkup = kb.Get() params.ReplyMarkup = kb.Get()

View File

@@ -1,6 +1,10 @@
package laniakea 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 Ptr[T any](v T) *T { return &v }
func Val[T any](p *T, def T) T { func Val[T any](p *T, def T) T {
@@ -9,7 +13,39 @@ func Val[T any](p *T, def T) T {
} }
return def 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, "<", "&lt;")
s = strings.ReplaceAll(s, ">", "&gt;")
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 const VersionString = utils.VersionString

View File

@@ -2,7 +2,6 @@ package utils
import ( import (
"os" "os"
"strings"
"git.nix13.pw/scuroneko/slog" "git.nix13.pw/scuroneko/slog"
) )
@@ -14,29 +13,3 @@ func GetLoggerLevel() slog.LogLevel {
} }
return level 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, "&", "&amp;")
s = strings.ReplaceAll(s, "<", "&lt;")
s = strings.ReplaceAll(s, ">", "&gt;")
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
}