diff --git a/internal/telegram/escape_text.go b/internal/telegram/escape_text.go new file mode 100644 index 0000000..98dad28 --- /dev/null +++ b/internal/telegram/escape_text.go @@ -0,0 +1,12 @@ +package telegram + +import ( + "strings" +) + +func escapeText(t string) string { + t = strings.ReplaceAll(t, "*", `\*`) + t = strings.ReplaceAll(t, "!", `\!`) + t = strings.ReplaceAll(t, ".", `\.`) + return t +} diff --git a/internal/telegram/types.go b/internal/telegram/types.go index 54553c5..603fed2 100644 --- a/internal/telegram/types.go +++ b/internal/telegram/types.go @@ -43,7 +43,8 @@ func (w *WebhookResponse) setMethod(method string, isMedia bool) *WebhookRespons } func (w *WebhookResponse) SendDice() *WebhookResponse { - return w.setMethod("sendMessage", false) + return w.setMethod("sendMessage", false). + UseMarkdown() } func (w *WebhookResponse) SendMessage() *WebhookResponse { @@ -75,6 +76,8 @@ func (w *WebhookResponse) SetChatID(chatID string) *WebhookResponse { } func (w *WebhookResponse) SetText(text string) *WebhookResponse { + text = escapeText(text) + if w.isMedia { w.Caption = &text } else { @@ -94,6 +97,11 @@ func (w *WebhookResponse) SetLinks(data []URLButton) *WebhookResponse { return w } +func (w *WebhookResponse) UseMarkdown() *WebhookResponse { + w.ParseMode = "MarkdownV2" + return w +} + func (w *WebhookResponse) SetKeyboard(kb string) *WebhookResponse { w.ReplyMarkup = &kb return w @@ -123,11 +131,10 @@ func (w *WebhookResponse) Empty() *WebhookResponse { func (w *WebhookResponse) Marshal() ([]byte, error) { // empty response - // spare a 46 bytes + // spare 46 bytes if len(w.Method) == 0 && !w.isMedia { return []byte{}, nil } - w.ParseMode = "MarkdownV2" return json.Marshal(w) }