From 6c8cc8ce1a110e197ada4355c7452182b495de11 Mon Sep 17 00:00:00 2001
From: boypt <1033514+boypt@users.noreply.github.com>
Date: Mon, 16 Dec 2019 14:15:00 +0800
Subject: [PATCH 1/3] BroadNews use text template
---
bot/service.go | 110 +++++++++++++----------------------------
bot/util.go | 29 +++++++++++
config/autoload.go | 120 +++++++++++++++++++++++++++++++++++++++++----
3 files changed, 173 insertions(+), 86 deletions(-)
create mode 100644 bot/util.go
diff --git a/bot/service.go b/bot/service.go
index 2e2696ef..f86553cb 100644
--- a/bot/service.go
+++ b/bot/service.go
@@ -1,14 +1,14 @@
package bot
import (
+ "bytes"
"fmt"
- strip "github.com/grokify/html-strip-tags-go"
+ "log"
+ "strings"
+
"github.com/indes/flowerss-bot/config"
"github.com/indes/flowerss-bot/model"
tb "gopkg.in/tucnak/telebot.v2"
- "log"
- "regexp"
- "strings"
)
func FeedForChannelRegister(m *tb.Message, url string, channelMention string) {
@@ -92,84 +92,40 @@ func SendError(c *tb.Chat) {
func BroadNews(source *model.Source, subs []model.Subscribe, contents []model.Content) {
log.Printf("Source Title: <%s> Subscriber: %d New Contents: %d", source.Title, len(subs), len(contents))
- var u tb.User
- var message string
for _, content := range contents {
- for _, sub := range subs {
- var disableNotification bool
- if sub.EnableNotification == 1 {
- disableNotification = false
- } else {
- disableNotification = true
- }
-
- u.ID = int(sub.UserID)
- previewText := ""
-
- if config.PreviewText > 0 {
- contentDesc := strings.Trim(
- strip.StripTags(
- regexp.MustCompile(`\n+`).ReplaceAllLiteralString(
- strings.ReplaceAll(
- regexp.MustCompile(`
`).ReplaceAllString(content.Description, "
"),
- "
", "\n"),
- "\n")),
- "\n")
+ previewText := trimDescription(content.Description, config.PreviewText)
+ tpldata := &config.TplData{
+ SourceTitle: source.Title,
+ ContentTitle: content.Title,
+ RawLink: content.RawLink,
+ PreviewText: previewText,
+ TelegraphURL: content.TelegraphUrl,
+ EnableTelegraph: config.EnableTelegraph,
+ }
- contentDescRune := []rune(contentDesc)
+ var buf []byte
+ wb := bytes.NewBuffer(buf)
+ if err := config.MessageTpl.Execute(wb, tpldata); err != nil {
+ log.Println(err)
+ return
+ }
- previewText += "\n---------- Preview ----------\n"
- if len(contentDescRune) > config.PreviewText {
- previewText += string(contentDescRune[0:config.PreviewText])
- } else {
- previewText += contentDesc
- }
- previewText += "\n-----------------------------"
+ for _, sub := range subs {
+ u := &tb.User{
+ ID: int(sub.UserID),
}
-
- if sub.EnableTelegraph == 1 && content.TelegraphUrl != "" {
- message = `
-%s%s
-%s | Telegraph | 原文
-`
- message = fmt.Sprintf(message, source.Title, previewText, content.Title, content.TelegraphUrl, content.RawLink)
- _, err := B.Send(&u, message, &tb.SendOptions{
- DisableWebPagePreview: false,
- ParseMode: tb.ModeHTML,
- DisableNotification: disableNotification,
- })
-
- if err != nil {
- log.Println(err)
- if strings.Contains(err.Error(), "Forbidden") {
- log.Printf("Unsubscribe UserID:%d SourceID:%d", sub.UserID, sub.SourceID)
- _ = sub.Unsub()
- }
- }
-
- } else {
- message = `
-%s%s
-%s
-`
- message = fmt.Sprintf(message, source.Title, previewText, content.RawLink, content.Title)
-
- _, err := B.Send(&u, message, &tb.SendOptions{
- DisableWebPagePreview: true,
- ParseMode: tb.ModeHTML,
- DisableNotification: disableNotification,
- })
-
- if err != nil {
- log.Println(err)
- if err != nil {
- log.Println(err)
- if strings.Contains(err.Error(), "Forbidden") {
- log.Printf("Unsubscribe UserID:%d SourceID:%d", sub.UserID, sub.SourceID)
- _ = sub.Unsub()
- }
- }
+ o := &tb.SendOptions{
+ DisableWebPagePreview: false,
+ ParseMode: config.MessageMode,
+ DisableNotification: sub.EnableNotification != 1,
+ }
+ msg := strings.TrimSpace(string(wb.Bytes()))
+ if _, err := B.Send(u, msg, o); err != nil {
+ log.Println(err)
+ if strings.Contains(err.Error(), "Forbidden") {
+ log.Printf("Unsubscribe UserID:%d SourceID:%d", sub.UserID, sub.SourceID)
+ _ = sub.Unsub()
}
}
}
diff --git a/bot/util.go b/bot/util.go
new file mode 100644
index 00000000..c267353f
--- /dev/null
+++ b/bot/util.go
@@ -0,0 +1,29 @@
+package bot
+
+import (
+ "regexp"
+ "strings"
+
+ strip "github.com/grokify/html-strip-tags-go"
+)
+
+func trimDescription(desc string, limit int) string {
+ if limit == 0 {
+ return ""
+ }
+ desc = strings.Trim(
+ strip.StripTags(
+ regexp.MustCompile(`\n+`).ReplaceAllLiteralString(
+ strings.ReplaceAll(
+ regexp.MustCompile(`
`).ReplaceAllString(desc, "
"),
+ "
", "\n"),
+ "\n")),
+ "\n")
+
+ contentDescRune := []rune(desc)
+ if len(contentDescRune) > limit {
+ desc = string(contentDescRune[:limit])
+ }
+
+ return desc
+}
diff --git a/config/autoload.go b/config/autoload.go
index 699a7f7a..a82124d0 100644
--- a/config/autoload.go
+++ b/config/autoload.go
@@ -1,13 +1,17 @@
package config
import (
+ "bytes"
"flag"
"fmt"
"github.com/spf13/viper"
+ tb "gopkg.in/tucnak/telebot.v2"
"log"
"os"
"path"
"strconv"
+ "strings"
+ "text/template"
)
var (
@@ -21,6 +25,29 @@ var (
EnableMysql bool
UpdateInterval int = 10
ErrorThreshold uint = 100
+ MessageTpl *template.Template
+ MessageMode tb.ParseMode
+)
+
+const (
+ logo = `
+ __ _
+ / _| | _____ _____ _ __ ___ ___
+ | |_| |/ _ \ \ /\ / / _ \ '__/ __/ __|
+ | _| | (_) \ V V / __/ | \__ \__ \
+ |_| |_|\___/ \_/\_/ \___|_| |___/___/
+
+`
+ defaultMessageTplMode = "md"
+ defaultMessageTpl = `** {{.SourceTitle}} **{{ if .PreviewText }}
+---------- Preview ----------
+{{.PreviewText}}
+-----------------------------
+{{- end}}{{if .EnableTelegraph}}
+{{.ContentTitle}} [Telegraph]({{.TelegraphURL}}) | [原文]({{.RawLink}})
+{{- else }}
+[{{.ContentTitle}}]({{.RawLink}})
+{{- end }}`
)
type MysqlConfig struct {
@@ -31,17 +58,83 @@ type MysqlConfig struct {
DB string
}
-func init() {
+type TplData struct {
+ SourceTitle string
+ ContentTitle string
+ RawLink string
+ PreviewText string
+ TelegraphURL string
+ EnableTelegraph bool
+}
- logo := `
- __ _
- / _| | _____ _____ _ __ ___ ___
- | |_| |/ _ \ \ /\ / / _ \ '__/ __/ __|
- | _| | (_) \ V V / __/ | \__ \__ \
- |_| |_|\___/ \_/\_/ \___|_| |___/___/
+func validateTPL() {
+ testData := []TplData{
+ TplData{
+ "RSS 源标识 - 无预览无telegraph的消息",
+ "这是标题",
+ "https://www.github.com/",
+ "",
+ "",
+ false,
+ },
+ TplData{
+ "RSS源标识 - 有预览无telegraph的消息",
+ "这是标题",
+ "https://www.github.com/",
+ "这里是很长很长很长的消息预览字数补丁紫薯补丁紫薯补丁紫薯补丁紫薯补丁",
+ "",
+ false,
+ },
+ TplData{
+ "RSS源标识 - 有预览有telegraph的消息",
+ "这是标题",
+ "https://www.github.com/",
+ "这里是很长很长很长的消息预览字数补丁紫薯补丁紫薯补丁紫薯补丁紫薯补丁",
+ "https://telegra.ph/markdown-07-07",
+ true,
+ },
+ }
+
+ var buf []byte
+ w := bytes.NewBuffer(buf)
+
+ for _, d := range testData {
+ w.Reset()
+ fmt.Println("\n////////////////////////////////////////////")
+ if err := MessageTpl.Execute(os.Stdout, d); err != nil {
+ log.Fatal(err)
+ }
+ }
+ fmt.Println("\n////////////////////////////////////////////")
+}
+
+func initTPL() {
+
+ var tplMsg string
+ if viper.IsSet("message_tpl") {
+ tplMsg = viper.GetString("message_tpl")
+ } else {
+ tplMsg = defaultMessageTpl
+ }
+ MessageTpl = template.Must(template.New("message").Parse(tplMsg))
+
+ if viper.IsSet("message_tpl_mode") {
+ switch strings.ToLower(viper.GetString("message_tpl_mode")) {
+ case "md", "markdown":
+ MessageMode = tb.ModeMarkdown
+ case "html":
+ MessageMode = tb.ModeHTML
+ default:
+ MessageMode = tb.ModeDefault
+ }
+ } else {
+ MessageMode = tb.ModeMarkdown
+ }
+
+}
+
+func init() {
-`
- fmt.Println(logo)
telegramTokenCli := flag.String("b", "", "Telegram Bot Token")
telegraphTokenCli := flag.String("t", "", "Telegraph API Token")
previewTextCli := flag.Int("p", 0, "Preview Text Length")
@@ -49,6 +142,7 @@ func init() {
errorThresholdCli := flag.Int("threshold", 0, "Error Threshold")
socks5Cli := flag.String("s", "", "Socks5 Proxy")
intervalCli := flag.Int("i", 0, "Update Interval")
+ testTpl := flag.Bool("testtpl", false, "Test Template")
flag.Parse()
projectName := "flowerss-bot"
@@ -63,6 +157,14 @@ func init() {
panic(fmt.Errorf("Fatal error config file: %s", err))
}
+ initTPL()
+ if *testTpl {
+ validateTPL()
+ os.Exit(0)
+ }
+
+ fmt.Println(logo)
+
if *telegramTokenCli == "" {
BotToken = viper.GetString("bot_token")
From f503cea2d647275cb6f5c7f36144cc61aa368127 Mon Sep 17 00:00:00 2001
From: boypt <1033514+boypt@users.noreply.github.com>
Date: Mon, 16 Dec 2019 15:54:49 +0800
Subject: [PATCH 2/3] fix tpl_mode too verbose
---
config/autoload.go | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/config/autoload.go b/config/autoload.go
index a82124d0..9cc26d33 100644
--- a/config/autoload.go
+++ b/config/autoload.go
@@ -118,8 +118,8 @@ func initTPL() {
}
MessageTpl = template.Must(template.New("message").Parse(tplMsg))
- if viper.IsSet("message_tpl_mode") {
- switch strings.ToLower(viper.GetString("message_tpl_mode")) {
+ if viper.IsSet("message_mode") {
+ switch strings.ToLower(viper.GetString("message_mode")) {
case "md", "markdown":
MessageMode = tb.ModeMarkdown
case "html":
From ee354e32501e10d8078fc07f8eced67073a7e36f Mon Sep 17 00:00:00 2001
From: boypt <1033514+boypt@users.noreply.github.com>
Date: Tue, 17 Dec 2019 01:05:39 +0800
Subject: [PATCH 3/3] fix misuse of sub.EnableTelegraph
---
bot/service.go | 32 +++++++++++++++++---------------
1 file changed, 17 insertions(+), 15 deletions(-)
diff --git a/bot/service.go b/bot/service.go
index f86553cb..ecd1807d 100644
--- a/bot/service.go
+++ b/bot/service.go
@@ -92,26 +92,28 @@ func SendError(c *tb.Chat) {
func BroadNews(source *model.Source, subs []model.Subscribe, contents []model.Content) {
log.Printf("Source Title: <%s> Subscriber: %d New Contents: %d", source.Title, len(subs), len(contents))
+ var buf []byte
+ wb := bytes.NewBuffer(buf)
for _, content := range contents {
previewText := trimDescription(content.Description, config.PreviewText)
- tpldata := &config.TplData{
- SourceTitle: source.Title,
- ContentTitle: content.Title,
- RawLink: content.RawLink,
- PreviewText: previewText,
- TelegraphURL: content.TelegraphUrl,
- EnableTelegraph: config.EnableTelegraph,
- }
-
- var buf []byte
- wb := bytes.NewBuffer(buf)
- if err := config.MessageTpl.Execute(wb, tpldata); err != nil {
- log.Println(err)
- return
- }
for _, sub := range subs {
+ tpldata := &config.TplData{
+ SourceTitle: source.Title,
+ ContentTitle: content.Title,
+ RawLink: content.RawLink,
+ PreviewText: previewText,
+ TelegraphURL: content.TelegraphUrl,
+ EnableTelegraph: sub.EnableTelegraph == 1 && content.TelegraphUrl != "",
+ }
+
+ wb.Reset()
+ if err := config.MessageTpl.Execute(wb, tpldata); err != nil {
+ log.Println(err)
+ return
+ }
+
u := &tb.User{
ID: int(sub.UserID),
}