Skip to content

Commit

Permalink
feat: telegraph switch button handler
Browse files Browse the repository at this point in the history
  • Loading branch information
indes committed Feb 25, 2022
1 parent f9af26e commit 6eeef93
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 6 deletions.
2 changes: 1 addition & 1 deletion internal/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func setCommands() {
handler.NewRemoveSubscriptionItemButton(),
handler.NewNotificationSwitchButton(B),
handler.NewSetSubscriptionTagButton(B),
handler.NewTelegraphSwitchButton(B),
}

for _, h := range ButtonHandlers {
Expand All @@ -123,6 +124,5 @@ func setCommands() {
zap.S().Errorw("set bot commands failed", "error", err.Error())
}

B.Handle(&tb.InlineButton{Unique: "set_toggle_telegraph_btn"}, setToggleTelegraphBtnCtr)
B.Handle(&tb.InlineButton{Unique: "set_toggle_update_btn"}, setToggleUpdateBtnCtr)
}
4 changes: 0 additions & 4 deletions internal/bot/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,6 @@ func genFeedSetBtn(
return feedSettingKeys
}

func setToggleTelegraphBtnCtr(ctx tb.Context) error {
return toggleCtrlButtons(ctx, "toggleTelegraph")
}

func setToggleUpdateBtnCtr(ctx tb.Context) error {
return toggleCtrlButtons(ctx, "toggleUpdate")
}
2 changes: 1 addition & 1 deletion internal/bot/handler/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func genFeedSetBtn(
}

toggleTelegraphKey := tb.InlineButton{
Unique: "set_toggle_telegraph_btn",
Unique: TelegraphSwitchButtonUnique,
Text: "开启 Telegraph 转码",
Data: c.Data,
}
Expand Down
86 changes: 86 additions & 0 deletions internal/bot/handler/telegraph_switch_button.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package handler

import (
"bytes"
"strconv"
"strings"
"text/template"

tb "gopkg.in/telebot.v3"

"github.com/indes/flowerss-bot/internal/bot/chat"
"github.com/indes/flowerss-bot/internal/config"
"github.com/indes/flowerss-bot/internal/model"
)

const (
TelegraphSwitchButtonUnique = "set_toggle_telegraph_btn"
)

type TelegraphSwitchButton struct {
bot *tb.Bot
}

func NewTelegraphSwitchButton(bot *tb.Bot) *TelegraphSwitchButton {
return &TelegraphSwitchButton{bot: bot}
}

func (b *TelegraphSwitchButton) CallbackUnique() string {
return "\f" + TelegraphSwitchButtonUnique
}

func (b *TelegraphSwitchButton) Description() string {
return ""
}

func (b *TelegraphSwitchButton) Handle(ctx tb.Context) error {
c := ctx.Callback()
if c == nil {
return ctx.Respond(&tb.CallbackResponse{Text: "error"})
}

data := strings.Split(c.Data, ":")
subscriberID, _ := strconv.ParseInt(data[0], 10, 64)
if subscriberID != c.Sender.ID {
// 如果订阅者与按钮点击者id不一致,需要验证管理员权限
channelChat, err := b.bot.ChatByID(subscriberID)
if err != nil {
return ctx.Respond(&tb.CallbackResponse{Text: "error"})
}
if !chat.IsChatAdmin(b.bot, channelChat, c.Sender.ID) {
return ctx.Respond(&tb.CallbackResponse{Text: "error"})
}
}

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

source, _ := model.GetSourceById(sub.SourceID)
t := template.New("setting template")
_, _ = t.Parse(feedSettingTmpl)

err = sub.ToggleTelegraph()
if err != nil {
return ctx.Respond(&tb.CallbackResponse{Text: "error"})
}
sub.Save()
text := new(bytes.Buffer)
_ = t.Execute(text, map[string]interface{}{"source": source, "sub": sub, "Count": config.ErrorThreshold})
_ = ctx.Respond(&tb.CallbackResponse{Text: "修改成功"})
return ctx.Edit(
text.String(),
&tb.SendOptions{ParseMode: tb.ModeHTML},
&tb.ReplyMarkup{InlineKeyboard: genFeedSetBtn(c, sub, source)},
)
}

func (b *TelegraphSwitchButton) Middlewares() []tb.MiddlewareFunc {
return nil
}

0 comments on commit 6eeef93

Please sign in to comment.