-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.go
64 lines (48 loc) · 1.73 KB
/
utility.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"fmt"
"log"
"net/http"
"strings"
"github.com/emaele/rss-telegram-notifier/entities"
"gorm.io/gorm"
tg "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
func writeHTTPResponse(statusCode int, body string, writer http.ResponseWriter) {
writer.WriteHeader(statusCode)
_, err := writer.Write([]byte(body))
if err != nil {
log.Println(err)
}
}
func createTelegramKeyboard(URL string) tg.InlineKeyboardMarkup {
var keyboard tg.InlineKeyboardMarkup
row := tg.NewInlineKeyboardRow(tg.NewInlineKeyboardButtonURL("🔗 Link", URL))
keyboard.InlineKeyboard = append(keyboard.InlineKeyboard, row)
//We finally append the lower row to the keyboard
keyboard.InlineKeyboard = append(keyboard.InlineKeyboard)
return keyboard
}
func createTelegramMessage(db *gorm.DB, element entities.RssItem, telegramChatID int64) tg.MessageConfig {
feedTitle := retrieveFeedTitle(db, element.Feed)
if feedTitle == "" {
feedTitle = "New Feed!"
}
var text string
// Pre-parsing our elements for markdown Reserved Chars
for _, char := range tgMarkdownReservedChars {
element.Title = strings.ReplaceAll(element.Title, char, fmt.Sprintf(`\%s`, char))
element.ImageURL = strings.ReplaceAll(element.ImageURL, char, fmt.Sprintf(`\%s`, char))
feedTitle = strings.ReplaceAll(feedTitle, char, fmt.Sprintf(`\%s`, char))
}
// Creating the message with pre-parsed items
if element.ImageURL != "" {
text = fmt.Sprintf("📣 *%s*\n\n[➡️](%s) %s", feedTitle, element.ImageURL, element.Title)
} else {
text = fmt.Sprintf("📣 *%s*\n\n➡️ %s", feedTitle, element.Title)
}
message := tg.NewMessage(telegramChatID, text)
message.ParseMode = "MarkdownV2"
message.ReplyMarkup = createTelegramKeyboard(element.URL)
return message
}