Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
feat(set): toggle subscribe telegraph setting
Browse files Browse the repository at this point in the history
  • Loading branch information
indes committed Feb 2, 2019
1 parent 5acb828 commit f4e498d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
65 changes: 63 additions & 2 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ func makeHandle() {
}

B.Handle(&toggleNoticeKey, func(c *tb.Callback) {
// on inline button pressed (callback!)
// always respond!

msg := strings.Split(c.Message.Text, "\n")
subID, err := strconv.Atoi(strings.Split(msg[1], " ")[1])
if err != nil {
Expand Down Expand Up @@ -145,6 +144,68 @@ func makeHandle() {
})
})

B.Handle(&toggleTelegraphKey, 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{}

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 = "开启通知"

}
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("/start", func(m *tb.Message) {
user := model.FindOrInitUser(m.Chat.ID)
log.Printf("/start %d", user.ID)
Expand Down
10 changes: 10 additions & 0 deletions model/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ func (s *Subscribe) ToggleNotification() error {
}
return nil
}

func (s *Subscribe) ToggleTelegraph() error {
if s.EnableTelegraph != 1 {
s.EnableTelegraph = 1
} else {
s.EnableTelegraph = 0
}
return nil
}

func (s *Subscribe) Save() {
db := getConnect()
defer db.Close()
Expand Down

0 comments on commit f4e498d

Please sign in to comment.