Add Telegram notification for webmentions

kcoram/uplift
Jan-Lukas Else 2019-12-23 10:21:21 +01:00
parent a7b987cca2
commit 1026a3b2af
5 changed files with 93 additions and 6 deletions

View File

@ -4,6 +4,7 @@ import (
"errors" "errors"
"log" "log"
"os" "os"
"strconv"
"strings" "strings"
) )
@ -14,6 +15,7 @@ var (
SelectedStorage Storage SelectedStorage Storage
SelectedCdn Cdn SelectedCdn Cdn
SelectedSocials Socials SelectedSocials Socials
SelectedNotificationServices NotificationServices
) )
type SyndicationTarget struct { type SyndicationTarget struct {
@ -85,6 +87,23 @@ func init() {
if SelectedSocials == nil { if SelectedSocials == nil {
log.Println("No social networks configured") log.Println("No social networks configured")
} }
// Find configured notification services (optional)
SelectedNotificationServices = func() NotificationServices {
var notificationServices []NotificationService = nil
// Telegram
telegramUserId, err1 := telegramUserId()
telegramBotToken, err2 := telegramBotToken()
if err1 == nil && err2 == nil {
notificationServices = append(notificationServices, &Telegram{
userId: telegramUserId,
botToken: telegramBotToken,
})
}
return notificationServices
}()
if SelectedNotificationServices == nil {
log.Println("No notification services configured")
}
} }
func giteaEndpoint() (string, error) { func giteaEndpoint() (string, error) {
@ -135,6 +154,23 @@ func microblogToken() (string, error) {
return microblogToken, nil return microblogToken, nil
} }
func telegramUserId() (int64, error) {
telegramUserIdString := os.Getenv("TELEGRAM_USER_ID")
telegramUserId, err := strconv.ParseInt(telegramUserIdString, 10, 64)
if err != nil || len(telegramUserIdString) == 0 || telegramUserIdString == "" {
return 0, errors.New("TELEGRAM_USER_ID not specified, Telegram features are deactivated")
}
return telegramUserId, nil
}
func telegramBotToken() (string, error) {
telegramBotToken := os.Getenv("TELEGRAM_BOT_TOKEN")
if len(telegramBotToken) == 0 || telegramBotToken == "" {
return "", errors.New("TELEGRAM_BOT_TOKEN not specified, Telegram features are deactivated")
}
return telegramBotToken, nil
}
func ignoredWebmentionUrls() ([]string, error) { func ignoredWebmentionUrls() ([]string, error) {
webmentionIgnored := os.Getenv("WEBMENTION_IGNORED") webmentionIgnored := os.Getenv("WEBMENTION_IGNORED")
if len(webmentionIgnored) == 0 { if len(webmentionIgnored) == 0 {

2
go.mod
View File

@ -3,6 +3,8 @@ module codeberg.org/jlelse/hugo-micropub
go 1.13 go 1.13
require ( require (
github.com/go-telegram-bot-api/telegram-bot-api v4.6.5-0.20190904012038-b33efeebc785+incompatible
github.com/technoweenie/multipartstreamer v1.0.1 // indirect
gopkg.in/yaml.v2 v2.2.7 gopkg.in/yaml.v2 v2.2.7
willnorris.com/go/webmention v0.0.0-20191104072158-c7fb13569b62 willnorris.com/go/webmention v0.0.0-20191104072158-c7fb13569b62
) )

4
go.sum
View File

@ -1,7 +1,11 @@
github.com/andybalholm/cascadia v1.0.0 h1:hOCXnnZ5A+3eVDX8pvgl4kofXv2ELss0bKcqRySc45o= github.com/andybalholm/cascadia v1.0.0 h1:hOCXnnZ5A+3eVDX8pvgl4kofXv2ELss0bKcqRySc45o=
github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y= github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
github.com/go-telegram-bot-api/telegram-bot-api v4.6.5-0.20190904012038-b33efeebc785+incompatible h1:OT02onvXX618RBcjxeUA4H7d1PSm5Apg4IET72VgVlE=
github.com/go-telegram-bot-api/telegram-bot-api v4.6.5-0.20190904012038-b33efeebc785+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM=
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY= github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQDeX7m2XsSOlQEnM=
github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog=
github.com/wsxiaoys/terminal v0.0.0-20160513160801-0940f3fc43a0/go.mod h1:IXCdmsXIht47RaVFLEdVnh1t+pgYtTAhQGj73kz+2DM= github.com/wsxiaoys/terminal v0.0.0-20160513160801-0940f3fc43a0/go.mod h1:IXCdmsXIht47RaVFLEdVnh1t+pgYtTAhQGj73kz+2DM=
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180911220305-26e67e76b6c3 h1:czFLhve3vsQetD6JOJ8NZZvGQIXlnN3/yXxbT6/awxI= golang.org/x/net v0.0.0-20180911220305-26e67e76b6c3 h1:czFLhve3vsQetD6JOJ8NZZvGQIXlnN3/yXxbT6/awxI=

40
notification.go Normal file
View File

@ -0,0 +1,40 @@
package main
import (
"fmt"
telegramBotApi "github.com/go-telegram-bot-api/telegram-bot-api"
)
type NotificationService interface {
Post(message string)
}
type NotificationServices []NotificationService
// Post to all Notification Services
func (notificationServices *NotificationServices) Post(message string) {
for _, notificationService := range *notificationServices {
notificationService.Post(message)
}
}
// Telegram
type Telegram struct {
userId int64
botToken string
}
func (t *Telegram) Post(message string) {
bot, err := telegramBotApi.NewBotAPI(t.botToken)
if err != nil {
fmt.Println("Failed to setup Telegram bot")
return
}
msg := telegramBotApi.NewMessage(t.userId, message)
_, err = bot.Send(msg)
if err != nil {
fmt.Println("Failed to send Telegram message")
return
}
return
}

View File

@ -130,6 +130,11 @@ func HandleWebmention(w http.ResponseWriter, r *http.Request) {
return return
} }
returnSuccess(targetUrl.String(), w, r) returnSuccess(targetUrl.String(), w, r)
go func() {
if SelectedNotificationServices != nil {
SelectedNotificationServices.Post("New webmention: " + sourceUrl.String())
}
}()
return return
} }