forked from carrydela/mygoTgChanBot
first
This commit is contained in:
104
internal/telegram/handlers_entry.go
Normal file
104
internal/telegram/handlers_entry.go
Normal file
@@ -0,0 +1,104 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
tele "gopkg.in/telebot.v3"
|
||||
)
|
||||
|
||||
func (b *Bot) handleEntryDel(c tele.Context) error {
|
||||
id := strings.TrimSpace(c.Message().Payload)
|
||||
if id == "" {
|
||||
return c.Reply("用法: /del <ID>")
|
||||
}
|
||||
|
||||
entry, err := b.storage.GetEntry(id)
|
||||
if err != nil {
|
||||
return c.Reply(fmt.Sprintf("❌ %v", err))
|
||||
}
|
||||
|
||||
if err := b.storage.DeleteEntry(id); err != nil {
|
||||
return c.Reply(fmt.Sprintf("❌ 删除失败: %v", err))
|
||||
}
|
||||
|
||||
b.toc.TriggerUpdate()
|
||||
return c.Reply(fmt.Sprintf("✅ 已删除: [%s] %s", entry.Category, entry.Title))
|
||||
}
|
||||
|
||||
func (b *Bot) handleEntryEdit(c tele.Context) error {
|
||||
args := strings.SplitN(c.Message().Payload, " ", 2)
|
||||
if len(args) < 2 {
|
||||
return c.Reply("用法: /edit <ID> <新标题>")
|
||||
}
|
||||
|
||||
id := args[0]
|
||||
newTitle := strings.TrimSpace(args[1])
|
||||
|
||||
if err := b.storage.UpdateEntryTitle(id, newTitle); err != nil {
|
||||
return c.Reply(fmt.Sprintf("❌ %v", err))
|
||||
}
|
||||
|
||||
b.toc.TriggerUpdate()
|
||||
return c.Reply(fmt.Sprintf("✅ 标题已更新为: %s", newTitle))
|
||||
}
|
||||
|
||||
func (b *Bot) handleEntryMove(c tele.Context) error {
|
||||
args := strings.Fields(c.Message().Payload)
|
||||
if len(args) < 2 {
|
||||
return c.Reply("用法: /move <ID> <新分类>")
|
||||
}
|
||||
|
||||
id := args[0]
|
||||
newCategory := args[1]
|
||||
|
||||
if !b.storage.CategoryExists(newCategory) {
|
||||
return c.Reply(fmt.Sprintf("❌ 分类 [%s] 不存在", newCategory))
|
||||
}
|
||||
|
||||
if err := b.storage.UpdateEntryCategory(id, newCategory); err != nil {
|
||||
return c.Reply(fmt.Sprintf("❌ %v", err))
|
||||
}
|
||||
|
||||
b.toc.TriggerUpdate()
|
||||
return c.Reply(fmt.Sprintf("✅ 已移动到分类: %s", newCategory))
|
||||
}
|
||||
|
||||
func (b *Bot) handleEntryList(c tele.Context) error {
|
||||
category := strings.TrimSpace(c.Message().Payload)
|
||||
|
||||
entries, err := b.storage.ListEntries(category)
|
||||
if err != nil {
|
||||
return c.Reply(fmt.Sprintf("❌ %v", err))
|
||||
}
|
||||
|
||||
if len(entries) == 0 {
|
||||
if category != "" {
|
||||
return c.Reply(fmt.Sprintf("📭 分类 [%s] 暂无条目", category))
|
||||
}
|
||||
return c.Reply("📭 暂无条目")
|
||||
}
|
||||
|
||||
var sb strings.Builder
|
||||
if category != "" {
|
||||
sb.WriteString(fmt.Sprintf("📋 分类 [%s] 的条目:\n\n", category))
|
||||
} else {
|
||||
sb.WriteString("📋 所有条目:\n\n")
|
||||
}
|
||||
|
||||
currentCat := ""
|
||||
for _, e := range entries {
|
||||
if category == "" && e.Category != currentCat {
|
||||
currentCat = e.Category
|
||||
sb.WriteString(fmt.Sprintf("\n【%s】\n", currentCat))
|
||||
}
|
||||
sb.WriteString(fmt.Sprintf("• [%s] %s\n", e.ID, e.Title))
|
||||
}
|
||||
|
||||
return c.Reply(sb.String())
|
||||
}
|
||||
|
||||
func (b *Bot) handleRefresh(c tele.Context) error {
|
||||
b.toc.TriggerUpdate()
|
||||
return c.Reply("🔄 目录刷新已触发")
|
||||
}
|
||||
Reference in New Issue
Block a user