diff --git a/Dockerfile b/Dockerfile index 3684029..e1a424b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.15-alpine3.12 as build +FROM golang:1.15-alpine3.13 as build RUN apk add --no-cache git gcc musl-dev sqlite-dev ADD *.go /app/ ADD go.mod /app/ @@ -6,7 +6,7 @@ ADD go.sum /app/ WORKDIR /app RUN go build --tags "libsqlite3 linux sqlite_fts5" -FROM alpine:3.12 +FROM alpine:3.13 RUN apk add --no-cache sqlite-dev tzdata COPY templates/ /app/templates/ COPY --from=build /app/GoBlog /bin/ diff --git a/config.go b/config.go index 9cfda5c..7ab1fcf 100644 --- a/config.go +++ b/config.go @@ -192,9 +192,10 @@ type configNotifications struct { } type configTelegram struct { - Enabled bool `mapstructure:"enabled"` - ChatID string `mapstructure:"chatId"` - BotToken string `mapstructure:"botToken"` + Enabled bool `mapstructure:"enabled"` + ChatID string `mapstructure:"chatId"` + BotToken string `mapstructure:"botToken"` + InstantViewHash string `mapstructure:"instantViewHash"` } var appConfig = &config{} diff --git a/notifications.go b/notifications.go index 96c83de..fb48192 100644 --- a/notifications.go +++ b/notifications.go @@ -7,7 +7,7 @@ import ( func sendNotification(text string) { log.Println("Notification:", text) if appConfig.Notifications.Telegram.Enabled { - err := sendTelegramMessage(text, appConfig.Notifications.Telegram.BotToken, appConfig.Notifications.Telegram.ChatID) + err := sendTelegramMessage(text, "", appConfig.Notifications.Telegram.BotToken, appConfig.Notifications.Telegram.ChatID) if err != nil { log.Println("Failed to send Telegram notification:", err.Error()) } diff --git a/telegram.go b/telegram.go index b251159..302688c 100644 --- a/telegram.go +++ b/telegram.go @@ -7,6 +7,7 @@ import ( "log" "net/http" "net/url" + "strings" ) const telegramBaseURL = "https://api.telegram.org/bot" @@ -32,22 +33,34 @@ func (p *post) tgPost() { if tg == nil || !tg.Enabled || tg.BotToken == "" || tg.ChatID == "" { return } + replacer := strings.NewReplacer("<", "<", ">", ">", "&", "&") var message bytes.Buffer if title := p.title(); title != "" { - message.WriteString(title) + message.WriteString(replacer.Replace(title)) message.WriteString("\n\n") } - message.WriteString(p.shortURL()) - if err := sendTelegramMessage(message.String(), tg.BotToken, tg.ChatID); err != nil { + if tg.InstantViewHash != "" { + message.WriteString("") + message.WriteString(replacer.Replace(p.shortURL())) + message.WriteString("") + } else { + message.WriteString("") + message.WriteString(replacer.Replace(p.shortURL())) + message.WriteString("") + } + if err := sendTelegramMessage(message.String(), "HTML", tg.BotToken, tg.ChatID); err != nil { log.Println(err.Error()) } } -func sendTelegramMessage(text, bottoken, chatID string) error { +func sendTelegramMessage(message, mode, token, chat string) error { params := url.Values{} - params.Add("chat_id", chatID) - params.Add("text", text) - tgURL, err := url.Parse(telegramBaseURL + bottoken + "/sendMessage") + params.Add("chat_id", chat) + params.Add("text", message) + if mode != "" { + params.Add("parse_mode", mode) + } + tgURL, err := url.Parse(telegramBaseURL + token + "/sendMessage") if err != nil { return errors.New("failed to create Telegram request") }