Skip to content

Commit

Permalink
✨ Better formatting for telegram message
Browse files Browse the repository at this point in the history
  • Loading branch information
Law Zava committed Apr 20, 2020
1 parent 582a536 commit aee1d54
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
34 changes: 20 additions & 14 deletions broadcast/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,35 @@ func (t Telegram) New() (Broadcast, error) {
}

func (t Telegram) Send(message Message) error {
type inlineKeyboard struct {
Text string `json:"text"`
URL string `json:"url"`
SwitchInlineQuery string `json:"switch_inline_query"`
}

type replyMarkup struct {
InlineKeyboard [][]inlineKeyboard `json:"inline_keyboard"`
}

telegramMessage := struct {
ChatID string `json:"chat_id"`
ParseMode string `json:"parse_mode"`
Text string `json:"text"`
ChatID string `json:"chat_id"`
ParseMode string `json:"parse_mode"`
Text string `json:"text"`
ReplyMarkup replyMarkup `json:"reply_markup"`
}{
ChatID: t.ChatID,
ParseMode: "MarkdownV2",
Text: fmt.Sprintf(`*%s*
*Link:* [%s](%s)`,
%s`, // empty line is intended
escapeTelegramText(message.Title),
escapeTelegramText(message.Link),
escapeTelegramLink(message.Link),
),
ReplyMarkup: replyMarkup{
InlineKeyboard: [][]inlineKeyboard{
{{Text: "Read", URL: message.Link}},
},
},
}

requestBody, err := json.Marshal(telegramMessage)
Expand Down Expand Up @@ -108,12 +123,3 @@ func escapeTelegramText(text string) string {

return replacer.Replace(text)
}

func escapeTelegramLink(link string) string {
replacer := strings.NewReplacer(
")", "\\)",
"\\", "\\\\",
)

return replacer.Replace(link)
}
2 changes: 1 addition & 1 deletion snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: mynews
version: '1.1.7'
version: '1.1.8'
summary: Personalized news feed parser & broadcast
description: |
Easily specify your RSS/Atom sources and broadcast preferences to get personalized news feed.
Expand Down

0 comments on commit aee1d54

Please # to comment.