mirror of https://github.com/jlelse/GoBlog
Add Telegram syndication
This commit is contained in:
parent
7364cfed59
commit
78c2ad722b
|
@ -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
hooks.go
1
hooks.go
|
@ -27,6 +27,7 @@ func (p *post) postPostHooks() {
|
|||
}(p, cmdTmplString)
|
||||
}
|
||||
go p.apPost()
|
||||
go p.tgPost()
|
||||
go p.sendWebmentions()
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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…
Reference in New Issue