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 notification setting
Browse files Browse the repository at this point in the history
  • Loading branch information
indes committed Feb 2, 2019
1 parent 9e4b280 commit 5acb828
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 17 deletions.
76 changes: 59 additions & 17 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"html/template"
"log"
"net/http"
"strconv"
"strings"
"time"
)
Expand Down Expand Up @@ -80,26 +81,66 @@ func makeHandle() {
Unique: "toggle_telegraph",
Text: "切换 Telegraph",
}
feedSettingKeys := [][]tb.InlineButton{}

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

// always respond!
B.Respond(c, &tb.CallbackResponse{
Text: "修改成功",
})
})
B.Handle("/key", func(m *tb.Message) {
if !m.Private() {
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
}
toggleNoticeKey.Text = "开启通知"

feedSettingKeys = append(
feedSettingKeys,
[]tb.InlineButton{toggleNoticeKey},
)
_, _ = B.Send(m.Sender, "Hello!", &tb.ReplyMarkup{
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.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 = "开启通知"

}
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,
})
})
Expand Down Expand Up @@ -282,6 +323,7 @@ func makeHandle() {
[通知] {{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{}

text := new(bytes.Buffer)
if sub.EnableNotification == 1 {
Expand All @@ -296,14 +338,15 @@ func makeHandle() {
} else {
toggleTelegraphKey.Text = "开启 Telegraph 转码"
}

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

// send null message to remove old keyboard
delKeyMessage, err := B.Send(m.Sender, "processing", &tb.ReplyMarkup{ReplyKeyboardRemove: true})
err = B.Delete(delKeyMessage)
//_, err = B.Edit(message, "hello")
log.Println(err)

//_, _ = B.Edit(message,
// text.String(),
// &tb.SendOptions{
Expand All @@ -319,8 +362,7 @@ func makeHandle() {
&tb.SendOptions{
ParseMode: tb.ModeHTML,
}, &tb.ReplyMarkup{
InlineKeyboard: feedSettingKeys,
ReplyKeyboardRemove: true,
InlineKeyboard: feedSettingKeys,
},
)
UserState[m.Sender.ID] = fsm.None
Expand Down
1 change: 1 addition & 0 deletions bot/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package bot
23 changes: 23 additions & 0 deletions model/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,26 @@ func UnsubByUserIDAndSourceURL(userID int, url string) error {
err = UnsubByUserIDAndSource(userID, source)
return err
}

func GetSubscribeByID(id int) (*Subscribe, error) {
db := getConnect()
defer db.Close()
var sub Subscribe
db.Where("id=? ", id).First(&sub)
return &sub, nil
}

func (s *Subscribe) ToggleNotification() error {
if s.EnableNotification != 1 {
s.EnableNotification = 1
} else {
s.EnableNotification = 0
}
return nil
}
func (s *Subscribe) Save() {
db := getConnect()
defer db.Close()
db.Save(&s)
return
}

0 comments on commit 5acb828

Please sign in to comment.