some fixes and small changes
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
42
utils.go
42
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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user