code refactor

This commit is contained in:
Astra 2026-02-17 20:05:06 +00:00
parent 7275b93666
commit d157f9b2c9
6 changed files with 187 additions and 193 deletions

View file

@ -10,27 +10,28 @@ import (
)
func EscapeMarkdown(s string) string {
toEscape := []string{"*", "_", "`", "[", "]", "(", ")", "\\", "#", "-"}
replacements := make([]string, 0, len(toEscape)*2)
for _, char := range toEscape {
replacements = append(replacements, char, "\\"+char)
}
replacer := strings.NewReplacer(replacements...)
replacer := strings.NewReplacer(
"\\", "\\\\",
"*", "\\*",
"_", "\\_",
"`", "\\`",
"[", "\\[",
"]", "\\]",
"(", "\\(",
")", "\\)",
"#", "\\#",
"-", "\\-",
)
return replacer.Replace(s)
}
func EscapeHTML(s string) string {
toEscape := []string{"&", "<", ">", "\"", "'"}
replacements := make([]string, 0, len(toEscape)*2)
replacements = append(replacements, "&", "&amp;")
replacements = append(replacements, "<", "&lt;")
replacements = append(replacements, ">", "&gt;")
replacements = append(replacements, "\"", "&quot;")
replacer := strings.NewReplacer(replacements...)
replacer := strings.NewReplacer(
"&", "&amp;",
"<", "&lt;",
">", "&gt;",
"\"", "&quot;",
)
return replacer.Replace(s)
}
@ -109,7 +110,7 @@ func BuildUserString(user *api.User) string {
return name.String()
}
func SendMessage(botAPI *api.BotAPI, chatID int64, topicID int, text string) (resp api.Message, err error) {
func SendMessage(botAPI *api.BotAPI, chatID int64, topicID int, text string) (api.Message, error) {
msg := api.NewMessage(chatID, text)
msg.ParseMode = api.ModeHTML
if topicID != 0 {
@ -118,22 +119,20 @@ func SendMessage(botAPI *api.BotAPI, chatID int64, topicID int, text string) (re
return botAPI.Send(msg)
}
func EditMessage(botAPI *api.BotAPI, chatID int64, messageID int, text string) (resp api.Message, err error) {
func EditMessage(botAPI *api.BotAPI, chatID int64, messageID int, text string) (api.Message, error) {
edit := api.NewEditMessageText(chatID, messageID, text)
edit.ParseMode = api.ModeHTML
return botAPI.Send(edit)
}
func LeaveChatRequest(botAPI *api.BotAPI, chatIDs []int64) error {
var err error
for _, chatID := range chatIDs {
leaveChatConfig := api.LeaveChatConfig{ChatConfig: api.ChatConfig{ChatID: chatID}}
_, err = botAPI.Request(leaveChatConfig)
_, err := botAPI.Request(api.LeaveChatConfig{ChatConfig: api.ChatConfig{ChatID: chatID}})
if err != nil {
return err
}
}
return err
return nil
}
func EditMessageWithKeyboard(botAPI *api.BotAPI, chatID int64, messageID int, text string, keyboard *api.InlineKeyboardMarkup) {
@ -145,8 +144,7 @@ func EditMessageWithKeyboard(botAPI *api.BotAPI, chatID int64, messageID int, te
botAPI.Send(edit)
}
// ParseIntArg parses a string argument as int, returns (value, error message).
// Error message is empty on success.
// ParseIntArg parses a string argument as int. Returns (value, errMsg); errMsg is empty on success.
func ParseIntArg(arg string) (int, string) {
val, err := strconv.Atoi(arg)
if err != nil {
@ -155,8 +153,7 @@ func ParseIntArg(arg string) (int, string) {
return val, ""
}
// ParseInt64Arg parses a string argument as int64, returns (value, error message).
// Error message is empty on success.
// ParseInt64Arg parses a string argument as int64. Returns (value, errMsg); errMsg is empty on success.
func ParseInt64Arg(arg string) (int64, string) {
val, err := strconv.ParseInt(arg, 10, 64)
if err != nil {