Skip to content

Commit

Permalink
update source update control, documents, code struct (#15)
Browse files Browse the repository at this point in the history
update source update control, documents, code struct
  • Loading branch information
indes committed Oct 29, 2019
2 parents f69ebd1 + d4c9d83 commit 7fad674
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 135 deletions.
9 changes: 9 additions & 0 deletions ChangeLog.md
@@ -0,0 +1,9 @@
Change Log
=

2019-10-28:

* update: add source update fetch control by `/set` command (usually used when source updating paused by reached 100 error when getting update data)
* update: merge `/set` command's response message template to a function for more struct

***
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -7,6 +7,7 @@

DEMO: [https://t.me/rssflowbot](https://t.me/rssflowbot)
[问题反馈群组](https://t.me/joinchat/FJ-cikd-yN1Bf1SxWbAKjw)
[Change Log](ChangeLog.md)

<img src="https://github.com/rssflow/img/raw/master/images/rssflow_demo.gif" width = "300"/>

Expand Down Expand Up @@ -45,6 +46,10 @@ make build

```yml
bot_token: XXX
#多个stelegraph_token可采用数组格式:
# telegraph_token:
# - token_1
# - token_2
telegraph_token: xxxx
socks5: 127.0.0.1:1080
update_interval: 10
Expand Down Expand Up @@ -112,6 +117,11 @@ Channel 订阅支持的命令:
1. 将 Bot 添加到 debug 频道管理员列表中
2. 给 Bot 发送 `/sub @debug http://www.ruanyifeng.com/blog/atom.xml` 命令

## 常见问题
* Q:日志中大量类似于`Create telegraph page error: FLOOD_WAIT_7`的提示
A:原因是创建telegraph页面请求过快触发了接口限制,可尝试在配置文件中添加多个telegraph token


### 问题反馈

如果你在使用过程中遇到问题,请提交 Issue,或者到[问题反馈群组](https://t.me/joinchat/FJ-cikd-yN1Bf1SxWbAKjw) 反馈。
Expand Down
241 changes: 106 additions & 135 deletions bot/bot.go
Expand Up @@ -21,7 +21,31 @@ var (
socks5Proxy = config.Socks5
UserState map[int64]fsm.UserStatus = make(map[int64]fsm.UserStatus)

B *tb.Bot
B *tb.Bot
botSettingTmpl = `
订阅<b>设置</b>
[id] {{ .sub.ID}}
[标题] {{ .source.Title }}
[Link] {{.source.Link}}
[抓取更新] {{if ge .source.ErrorCount 100}}暂停{{else if lt .source.ErrorCount 100}}抓取中{{end}}
[通知] {{if eq .sub.EnableNotification 0}}关闭{{else if eq .sub.EnableNotification 1}}开启{{end}}
[Telegraph] {{if eq .sub.EnableTelegraph 0}}关闭{{else if eq .sub.EnableTelegraph 1}}开启{{end}}
`
)

var (
toggleNoticeKey = tb.InlineButton{
Unique: "toggle_notice",
Text: "切换通知",
}
toggleTelegraphKey = tb.InlineButton{
Unique: "toggle_telegraph",
Text: "切换 Telegraph",
}
toggleEnabledKey = tb.InlineButton{
Unique: "toggle_enabled",
Text: "抓取开关",
}
)

func init() {
Expand Down Expand Up @@ -74,144 +98,93 @@ func init() {

}

//Start run bot
func Start() {
makeHandle()
B.Start()
}

func makeHandle() {
toggleNoticeKey := tb.InlineButton{
Unique: "toggle_notice",
Text: "切换通知",
func toggleCtrlButtons(c *tb.Callback, action string) {
msg := strings.Split(c.Message.Text, "\n")
subID, err := strconv.Atoi(strings.Split(msg[1], " ")[1])
if err != nil {
_ = B.Respond(c, &tb.CallbackResponse{
Text: "error",
})
return
}
toggleTelegraphKey := tb.InlineButton{
Unique: "toggle_telegraph",
Text: "切换 Telegraph",
sub, err := model.GetSubscribeByID(int(subID))
if sub == nil || err != nil {
_ = B.Respond(c, &tb.CallbackResponse{
Text: "error",
})
return
}

B.Handle(&toggleNoticeKey, func(c *tb.Callback) {

msg := strings.Split(c.Message.Text, "\n")
subID, err := strconv.Atoi(strings.Split(msg[1], " ")[1])
if err != nil {
_ = B.Respond(c, &tb.CallbackResponse{
Text: "error",
})
return
}
sub, err := model.GetSubscribeByID(int(subID))
if sub == nil || err != nil {
_ = B.Respond(c, &tb.CallbackResponse{
Text: "error",
})
return
}

source, _ := model.GetSourceById(int(sub.SourceID))
t := template.New("setting template")
_, _ = t.Parse(`
订阅<b>设置</b>
[id] {{ .sub.ID}}
[标题] {{ .source.Title }}
[Link] {{.source.Link}}
[通知] {{if eq .sub.EnableNotification 0}}关闭{{else if eq .sub.EnableNotification 1}}开启{{end}}
[Telegraph] {{if eq .sub.EnableTelegraph 0}}关闭{{else if eq .sub.EnableTelegraph 1}}开启{{end}}
`)
feedSettingKeys := [][]tb.InlineButton{}
source, _ := model.GetSourceById(int(sub.SourceID))
t := template.New("setting template")
_, _ = t.Parse(botSettingTmpl)
feedSettingKeys := [][]tb.InlineButton{}

switch action {
case "toggleNoticeKey":
err = sub.ToggleNotification()
if err != nil {
_ = B.Respond(c, &tb.CallbackResponse{
Text: "error",
})
return
}
sub.Save()
text := new(bytes.Buffer)
if sub.EnableNotification == 1 {
toggleNoticeKey.Text = "关闭通知"
} else {
toggleNoticeKey.Text = "开启通知"
case "toggleTelegraphKey":
err = sub.ToggleTelegraph()
case "toggleEnabledKey":
err = source.ToggleEnabled()
}

}
if sub.EnableTelegraph == 1 {
toggleTelegraphKey.Text = "关闭 Telegraph 转码"
} else {
toggleTelegraphKey.Text = "开启 Telegraph 转码"
}
feedSettingKeys = append(feedSettingKeys, []tb.InlineButton{toggleNoticeKey, toggleTelegraphKey})
_ = t.Execute(text, map[string]interface{}{"source": source, "sub": sub})
if err != nil {
_ = B.Respond(c, &tb.CallbackResponse{
Text: "修改成功",
})
_, _ = B.Edit(c.Message, text.String(), &tb.SendOptions{
ParseMode: tb.ModeHTML,
}, &tb.ReplyMarkup{
InlineKeyboard: feedSettingKeys,
Text: "error",
})
return
}

sub.Save()

text := new(bytes.Buffer)
if sub.EnableNotification == 1 {
toggleNoticeKey.Text = "关闭通知"
} else {
toggleNoticeKey.Text = "开启通知"
}
if sub.EnableTelegraph == 1 {
toggleTelegraphKey.Text = "关闭 Telegraph 转码"
} else {
toggleTelegraphKey.Text = "开启 Telegraph 转码"
}
if source.ErrorCount >= 100 {
toggleEnabledKey.Text = "重启更新"
} else {
toggleEnabledKey.Text = "暂停更新"
}

feedSettingKeys = append(feedSettingKeys, []tb.InlineButton{toggleEnabledKey, toggleNoticeKey, toggleTelegraphKey})
_ = t.Execute(text, map[string]interface{}{"source": source, "sub": sub})
_ = B.Respond(c, &tb.CallbackResponse{
Text: "修改成功",
})
_, _ = B.Edit(c.Message, text.String(), &tb.SendOptions{
ParseMode: tb.ModeHTML,
}, &tb.ReplyMarkup{
InlineKeyboard: feedSettingKeys,
})
}

B.Handle(&toggleTelegraphKey, func(c *tb.Callback) {
//Start run bot
func Start() {
makeHandle()
B.Start()
}

msg := strings.Split(c.Message.Text, "\n")
subID, err := strconv.Atoi(strings.Split(msg[1], " ")[1])
if err != nil {
_ = B.Respond(c, &tb.CallbackResponse{
Text: "error",
})
return
}
sub, err := model.GetSubscribeByID(int(subID))
if sub == nil || err != nil {
_ = B.Respond(c, &tb.CallbackResponse{
Text: "error",
})
return
}
func makeHandle() {

source, _ := model.GetSourceById(int(sub.SourceID))
t := template.New("setting template")
_, _ = t.Parse(`
订阅<b>设置</b>
[id] {{ .sub.ID}}
[标题] {{ .source.Title }}
[Link] {{.source.Link}}
[通知] {{if eq .sub.EnableNotification 0}}关闭{{else if eq .sub.EnableNotification 1}}开启{{end}}
[Telegraph] {{if eq .sub.EnableTelegraph 0}}关闭{{else if eq .sub.EnableTelegraph 1}}开启{{end}}
`)
feedSettingKeys := [][]tb.InlineButton{}
B.Handle(&toggleNoticeKey, func(c *tb.Callback) {
toggleCtrlButtons(c, "toggleNoticeKey")
})

err = sub.ToggleTelegraph()
if err != nil {
_ = B.Respond(c, &tb.CallbackResponse{
Text: "error",
})
return
}
sub.Save()
text := new(bytes.Buffer)
if sub.EnableNotification == 1 {
toggleNoticeKey.Text = "关闭通知"
} else {
toggleNoticeKey.Text = "开启通知"
B.Handle(&toggleTelegraphKey, func(c *tb.Callback) {
toggleCtrlButtons(c, "toggleTelegraphKey")
})

}
if sub.EnableTelegraph == 1 {
toggleTelegraphKey.Text = "关闭 Telegraph 转码"
} else {
toggleTelegraphKey.Text = "开启 Telegraph 转码"
}
feedSettingKeys = append(feedSettingKeys, []tb.InlineButton{toggleNoticeKey, toggleTelegraphKey})
_ = t.Execute(text, map[string]interface{}{"source": source, "sub": sub})
_ = B.Respond(c, &tb.CallbackResponse{
Text: "修改成功",
})
_, _ = B.Edit(c.Message, text.String(), &tb.SendOptions{
ParseMode: tb.ModeHTML,
}, &tb.ReplyMarkup{
InlineKeyboard: feedSettingKeys,
})
B.Handle(&toggleEnabledKey, func(c *tb.Callback) {
toggleCtrlButtons(c, "toggleEnabledKey")
})

B.Handle("/start", func(m *tb.Message) {
Expand Down Expand Up @@ -559,14 +532,8 @@ func makeHandle() {
return
}
t := template.New("setting template")
_, _ = t.Parse(`
订阅<b>设置</b>
[id] {{ .sub.ID}}
[标题] {{ .source.Title }}
[Link] {{.source.Link}}
[通知] {{if eq .sub.EnableNotification 0}}关闭{{else if eq .sub.EnableNotification 1}}开启{{end}}
[Telegraph] {{if eq .sub.EnableTelegraph 0}}关闭{{else if eq .sub.EnableTelegraph 1}}开启{{end}}
`)
_, _ = t.Parse(botSettingTmpl)

feedSettingKeys := [][]tb.InlineButton{}

text := new(bytes.Buffer)
Expand All @@ -575,15 +542,19 @@ func makeHandle() {
toggleNoticeKey.Text = "关闭通知"
} else {
toggleNoticeKey.Text = "开启通知"

}
if sub.EnableTelegraph == 1 {
toggleTelegraphKey.Text = "关闭 Telegraph 转码"
} else {
toggleTelegraphKey.Text = "开启 Telegraph 转码"
}
if source.ErrorCount >= 100 {
toggleEnabledKey.Text = "重启更新"
} else {
toggleEnabledKey.Text = "暂停更新"
}

feedSettingKeys = append(feedSettingKeys, []tb.InlineButton{toggleNoticeKey, toggleTelegraphKey})
feedSettingKeys = append(feedSettingKeys, []tb.InlineButton{toggleEnabledKey, toggleNoticeKey, toggleTelegraphKey})
_ = t.Execute(text, map[string]interface{}{"source": source, "sub": sub})

// send null message to remove old keyboard
Expand Down
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -9,6 +9,7 @@ require (
github.com/indes/telegraph-go v1.0.1
github.com/jinzhu/gorm v1.9.11
github.com/spf13/viper v1.4.0
go.uber.org/zap v1.10.0
golang.org/x/net v0.0.0-20191014212845-da9a3fd4c582
gopkg.in/tucnak/telebot.v2 v2.0.0-20191005061224-d0707a9d73c4
)

0 comments on commit 7fad674

Please sign in to comment.