Skip to content

Commit

Permalink
use markdown for a subset of responses
Browse files Browse the repository at this point in the history
  • Loading branch information
zenlor committed Jan 11, 2024
1 parent 242697b commit 855bb41
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
12 changes: 12 additions & 0 deletions internal/telegram/escape_text.go
Original file line number Diff line number Diff line change
@@ -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
}
13 changes: 10 additions & 3 deletions internal/telegram/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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)
}

0 comments on commit 855bb41

Please sign in to comment.