Browse Source

Add support for TG Instant View

master
Jan-Lukas Else 3 weeks ago
parent
commit
d5b37eba73
4 changed files with 27 additions and 13 deletions
  1. +2
    -2
      Dockerfile
  2. +4
    -3
      config.go
  3. +1
    -1
      notifications.go
  4. +20
    -7
      telegram.go

+ 2
- 2
Dockerfile 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
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/


+ 4
- 3
config.go View File

@ -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{}


+ 1
- 1
notifications.go View File

@ -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())
}


+ 20
- 7
telegram.go View File

@ -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("<", "&lt;", ">", "&gt;", "&", "&amp;")
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("<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())
}
}
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")
}


Loading…
Cancel
Save