Browse Source

Add Telegram syndication

master
Jan-Lukas Else 2 months ago
parent
commit
78c2ad722b
4 changed files with 44 additions and 24 deletions
  1. +1
    -0
      config.go
  2. +1
    -0
      hooks.go
  3. +2
    -24
      notifications.go
  4. +40
    -0
      telegram.go

+ 1
- 0
config.go View File

@ -56,6 +56,7 @@ type configBlog struct {
Photos *photos `mapstructure:"photos"`
DefaultSection string `mapstructure:"defaultsection"`
CustomPages []*customPage `mapstructure:"custompages"`
Telegram *configTelegram `mapstructure:"telegram"`
}
type section struct {


+ 1
- 0
hooks.go View File

@ -27,6 +27,7 @@ func (p *post) postPostHooks() {
}(p, cmdTmplString)
}
go p.apPost()
go p.tgPost()
go p.sendWebmentions()
}


+ 2
- 24
notifications.go View File

@ -1,37 +1,15 @@
package main
import (
"errors"
"log"
"net/http"
"net/url"
)
func sendNotification(text string) {
log.Println("Notification:", text)
if appConfig.Notifications.Telegram.Enabled {
err := sendTelegramMessage(text)
err := sendTelegramMessage(text, appConfig.Notifications.Telegram.BotToken, appConfig.Notifications.Telegram.ChatID)
if err != nil {
log.Println("Failed to send Telegram message:", err.Error())
log.Println("Failed to send Telegram notification:", err.Error())
}
}
}
const telegramBaseURL = "https://api.telegram.org/bot"
func sendTelegramMessage(text string) error {
params := url.Values{}
params.Add("chat_id", appConfig.Notifications.Telegram.ChatID)
params.Add("text", text)
tgURL, err := url.Parse(telegramBaseURL + appConfig.Notifications.Telegram.BotToken + "/sendMessage")
if err != nil {
return errors.New("failed to create Telegram request")
}
tgURL.RawQuery = params.Encode()
req, _ := http.NewRequest(http.MethodPost, tgURL.String(), nil)
resp, err := http.DefaultClient.Do(req)
if err != nil || resp.StatusCode != http.StatusOK {
return errors.New("failed to send Telegram message")
}
return nil
}

+ 40
- 0
telegram.go View File

@ -0,0 +1,40 @@
package main
import (
"bytes"
"errors"
"net/http"
"net/url"
)
const telegramBaseURL = "https://api.telegram.org/bot"
func (p *post) tgPost() {
if !appConfig.Blogs[p.Blog].Telegram.Enabled {
return
}
var message bytes.Buffer
if title := p.title(); title != "" {
message.WriteString(title)
message.WriteString("\n\n")
}
message.WriteString(appConfig.Server.PublicAddress + p.Path)
sendTelegramMessage(message.String(), appConfig.Blogs[p.Blog].Telegram.BotToken, appConfig.Blogs[p.Blog].Telegram.ChatID)
}
func sendTelegramMessage(text, bottoken, chatID string) error {
params := url.Values{}
params.Add("chat_id", chatID)
params.Add("text", text)
tgURL, err := url.Parse(telegramBaseURL + bottoken + "/sendMessage")
if err != nil {
return errors.New("failed to create Telegram request")
}
tgURL.RawQuery = params.Encode()
req, _ := http.NewRequest(http.MethodPost, tgURL.String(), nil)
resp, err := http.DefaultClient.Do(req)
if err != nil || resp.StatusCode != http.StatusOK {
return errors.New("failed to send Telegram message")
}
return nil
}

Loading…
Cancel
Save