Add support for TG Instant View

This commit is contained in:
Jan-Lukas Else 2021-02-08 20:33:48 +01:00
parent 424d1e9944
commit d5b37eba73
4 changed files with 27 additions and 13 deletions

View File

@ -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 RUN apk add --no-cache git gcc musl-dev sqlite-dev
ADD *.go /app/ ADD *.go /app/
ADD go.mod /app/ ADD go.mod /app/
@ -6,7 +6,7 @@ ADD go.sum /app/
WORKDIR /app WORKDIR /app
RUN go build --tags "libsqlite3 linux sqlite_fts5" RUN go build --tags "libsqlite3 linux sqlite_fts5"
FROM alpine:3.12 FROM alpine:3.13
RUN apk add --no-cache sqlite-dev tzdata RUN apk add --no-cache sqlite-dev tzdata
COPY templates/ /app/templates/ COPY templates/ /app/templates/
COPY --from=build /app/GoBlog /bin/ COPY --from=build /app/GoBlog /bin/

View File

@ -192,9 +192,10 @@ type configNotifications struct {
} }
type configTelegram struct { type configTelegram struct {
Enabled bool `mapstructure:"enabled"` Enabled bool `mapstructure:"enabled"`
ChatID string `mapstructure:"chatId"` ChatID string `mapstructure:"chatId"`
BotToken string `mapstructure:"botToken"` BotToken string `mapstructure:"botToken"`
InstantViewHash string `mapstructure:"instantViewHash"`
} }
var appConfig = &config{} var appConfig = &config{}

View File

@ -7,7 +7,7 @@ import (
func sendNotification(text string) { func sendNotification(text string) {
log.Println("Notification:", text) log.Println("Notification:", text)
if appConfig.Notifications.Telegram.Enabled { 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 { if err != nil {
log.Println("Failed to send Telegram notification:", err.Error()) log.Println("Failed to send Telegram notification:", err.Error())
} }

View File

@ -7,6 +7,7 @@ import (
"log" "log"
"net/http" "net/http"
"net/url" "net/url"
"strings"
) )
const telegramBaseURL = "https://api.telegram.org/bot" const telegramBaseURL = "https://api.telegram.org/bot"
@ -32,22 +33,34 @@ func (p *post) tgPost() {
if tg == nil || !tg.Enabled || tg.BotToken == "" || tg.ChatID == "" { if tg == nil || !tg.Enabled || tg.BotToken == "" || tg.ChatID == "" {
return return
} }
replacer := strings.NewReplacer("<", "&lt;", ">", "&gt;", "&", "&amp;")
var message bytes.Buffer var message bytes.Buffer
if title := p.title(); title != "" { if title := p.title(); title != "" {
message.WriteString(title) message.WriteString(replacer.Replace(title))
message.WriteString("\n\n") message.WriteString("\n\n")
} }
message.WriteString(p.shortURL()) if tg.InstantViewHash != "" {
if err := sendTelegramMessage(message.String(), tg.BotToken, tg.ChatID); err != nil { message.WriteString("<a href=\"https://t.me/iv?rhash=" + tg.InstantViewHash + "&url=" + url.QueryEscape(p.fullURL()) + "\">")
message.WriteString(replacer.Replace(p.shortURL()))
message.WriteString("</a>")
} else {
message.WriteString("<a href=\"" + p.shortURL() + "\">")
message.WriteString(replacer.Replace(p.shortURL()))
message.WriteString("</a>")
}
if err := sendTelegramMessage(message.String(), "HTML", tg.BotToken, tg.ChatID); err != nil {
log.Println(err.Error()) log.Println(err.Error())
} }
} }
func sendTelegramMessage(text, bottoken, chatID string) error { func sendTelegramMessage(message, mode, token, chat string) error {
params := url.Values{} params := url.Values{}
params.Add("chat_id", chatID) params.Add("chat_id", chat)
params.Add("text", text) params.Add("text", message)
tgURL, err := url.Parse(telegramBaseURL + bottoken + "/sendMessage") if mode != "" {
params.Add("parse_mode", mode)
}
tgURL, err := url.Parse(telegramBaseURL + token + "/sendMessage")
if err != nil { if err != nil {
return errors.New("failed to create Telegram request") return errors.New("failed to create Telegram request")
} }