first
This commit is contained in:
81
internal/telegram/bot.go
Normal file
81
internal/telegram/bot.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"tgchanbot/internal/config"
|
||||
"tgchanbot/internal/storage"
|
||||
"tgchanbot/internal/toc"
|
||||
|
||||
tele "gopkg.in/telebot.v3"
|
||||
)
|
||||
|
||||
type Bot struct {
|
||||
bot *tele.Bot
|
||||
cfg *config.Config
|
||||
storage *storage.Storage
|
||||
toc *toc.Manager
|
||||
states *StateManager
|
||||
}
|
||||
|
||||
func New(cfg *config.Config, store *storage.Storage) (*Bot, error) {
|
||||
pref := tele.Settings{
|
||||
Token: cfg.Bot.Token,
|
||||
Poller: &tele.LongPoller{Timeout: 10 * time.Second},
|
||||
}
|
||||
|
||||
b, err := tele.NewBot(pref)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
bot := &Bot{
|
||||
bot: b,
|
||||
cfg: cfg,
|
||||
storage: store,
|
||||
states: NewStateManager(),
|
||||
}
|
||||
|
||||
bot.toc = toc.NewManager(store, b, cfg.Channel.ID, time.Duration(cfg.TOC.DebounceSeconds)*time.Second)
|
||||
|
||||
bot.setupRoutes()
|
||||
|
||||
return bot, nil
|
||||
}
|
||||
|
||||
func (b *Bot) setupRoutes() {
|
||||
adminOnly := b.bot.Group()
|
||||
adminOnly.Use(b.AdminMiddleware())
|
||||
|
||||
// Category management
|
||||
adminOnly.Handle("/cat_add", b.handleCatAdd)
|
||||
adminOnly.Handle("/cat_del", b.handleCatDel)
|
||||
adminOnly.Handle("/cat_list", b.handleCatList)
|
||||
|
||||
// Post flow
|
||||
adminOnly.Handle("/post", b.handlePost)
|
||||
adminOnly.Handle(tele.OnText, b.handleTextOrForwarded)
|
||||
|
||||
// Entry management
|
||||
adminOnly.Handle("/del", b.handleEntryDel)
|
||||
adminOnly.Handle("/edit", b.handleEntryEdit)
|
||||
adminOnly.Handle("/move", b.handleEntryMove)
|
||||
adminOnly.Handle("/list", b.handleEntryList)
|
||||
|
||||
// TOC
|
||||
adminOnly.Handle("/refresh", b.handleRefresh)
|
||||
|
||||
// Callbacks
|
||||
b.bot.Handle(tele.OnCallback, b.handleCallback)
|
||||
}
|
||||
|
||||
func (b *Bot) Start() {
|
||||
log.Println("Bot started...")
|
||||
b.bot.Start()
|
||||
}
|
||||
|
||||
func (b *Bot) Stop() {
|
||||
b.bot.Stop()
|
||||
b.toc.Stop()
|
||||
}
|
||||
62
internal/telegram/handlers_cat.go
Normal file
62
internal/telegram/handlers_cat.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
tele "gopkg.in/telebot.v3"
|
||||
)
|
||||
|
||||
func (b *Bot) handleCatAdd(c tele.Context) error {
|
||||
args := strings.Fields(c.Message().Payload)
|
||||
if len(args) == 0 {
|
||||
return c.Reply("用法: /cat_add <名称> [排序]\n例如: /cat_add iOS 1")
|
||||
}
|
||||
|
||||
name := args[0]
|
||||
order := 0
|
||||
if len(args) > 1 {
|
||||
if o, err := strconv.Atoi(args[1]); err == nil {
|
||||
order = o
|
||||
}
|
||||
}
|
||||
|
||||
if err := b.storage.CreateCategory(name, order); err != nil {
|
||||
return c.Reply(fmt.Sprintf("❌ 创建失败: %v", err))
|
||||
}
|
||||
|
||||
return c.Reply(fmt.Sprintf("✅ 分类 [%s] 已创建 (排序: %d)", name, order))
|
||||
}
|
||||
|
||||
func (b *Bot) handleCatDel(c tele.Context) error {
|
||||
name := strings.TrimSpace(c.Message().Payload)
|
||||
if name == "" {
|
||||
return c.Reply("用法: /cat_del <名称>")
|
||||
}
|
||||
|
||||
if err := b.storage.DeleteCategory(name); err != nil {
|
||||
return c.Reply(fmt.Sprintf("❌ 删除失败: %v", err))
|
||||
}
|
||||
|
||||
return c.Reply(fmt.Sprintf("✅ 分类 [%s] 已删除", name))
|
||||
}
|
||||
|
||||
func (b *Bot) handleCatList(c tele.Context) error {
|
||||
categories, err := b.storage.ListCategories()
|
||||
if err != nil {
|
||||
return c.Reply(fmt.Sprintf("❌ 获取失败: %v", err))
|
||||
}
|
||||
|
||||
if len(categories) == 0 {
|
||||
return c.Reply("📂 暂无分类\n使用 /cat_add <名称> 创建")
|
||||
}
|
||||
|
||||
var sb strings.Builder
|
||||
sb.WriteString("📂 分类列表:\n\n")
|
||||
for i, cat := range categories {
|
||||
sb.WriteString(fmt.Sprintf("%d. %s (排序: %d)\n", i+1, cat.Name, cat.Order))
|
||||
}
|
||||
|
||||
return c.Reply(sb.String())
|
||||
}
|
||||
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("🔄 目录刷新已触发")
|
||||
}
|
||||
211
internal/telegram/handlers_post.go
Normal file
211
internal/telegram/handlers_post.go
Normal file
@@ -0,0 +1,211 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"tgchanbot/internal/storage"
|
||||
|
||||
tele "gopkg.in/telebot.v3"
|
||||
)
|
||||
|
||||
const (
|
||||
cbPrefixCat = "cat:"
|
||||
cbPrefixConfirm = "confirm:"
|
||||
cbCancel = "cancel"
|
||||
)
|
||||
|
||||
func (b *Bot) handlePost(c tele.Context) error {
|
||||
b.states.StartPost(c.Sender().ID)
|
||||
return c.Reply("📨 请转发一条来自目标频道的消息")
|
||||
}
|
||||
|
||||
func (b *Bot) handleTextOrForwarded(c tele.Context) error {
|
||||
msg := c.Message()
|
||||
if msg == nil || msg.OriginalChat == nil {
|
||||
return nil
|
||||
}
|
||||
return b.handleForwarded(c)
|
||||
}
|
||||
|
||||
func (b *Bot) handleForwarded(c tele.Context) error {
|
||||
if !b.cfg.IsAdmin(c.Sender().ID) {
|
||||
return nil
|
||||
}
|
||||
|
||||
state := b.states.Get(c.Sender().ID)
|
||||
if state == nil || state.Step != StepAwaitForward {
|
||||
return nil
|
||||
}
|
||||
|
||||
msg := c.Message()
|
||||
if msg.OriginalChat == nil {
|
||||
return c.Reply("❌ 这不是一条转发消息,请转发频道消息")
|
||||
}
|
||||
|
||||
b.states.SetForwarded(c.Sender().ID, msg)
|
||||
|
||||
categories, err := b.storage.ListCategories()
|
||||
if err != nil {
|
||||
b.states.Delete(c.Sender().ID)
|
||||
return c.Reply(fmt.Sprintf("❌ 获取分类失败: %v", err))
|
||||
}
|
||||
|
||||
if len(categories) == 0 {
|
||||
b.states.Delete(c.Sender().ID)
|
||||
return c.Reply("❌ 暂无分类,请先使用 /cat_add 创建分类")
|
||||
}
|
||||
|
||||
keyboard := b.buildCategoryKeyboard(categories)
|
||||
return c.Reply("📁 请选择分类:", keyboard)
|
||||
}
|
||||
|
||||
func (b *Bot) buildCategoryKeyboard(categories []storage.Category) *tele.ReplyMarkup {
|
||||
menu := &tele.ReplyMarkup{}
|
||||
var rows []tele.Row
|
||||
|
||||
var currentRow []tele.Btn
|
||||
for _, cat := range categories {
|
||||
btn := menu.Data(cat.Name, cbPrefixCat+cat.Name)
|
||||
currentRow = append(currentRow, btn)
|
||||
|
||||
if len(currentRow) == 3 {
|
||||
rows = append(rows, menu.Row(currentRow...))
|
||||
currentRow = nil
|
||||
}
|
||||
}
|
||||
|
||||
if len(currentRow) > 0 {
|
||||
rows = append(rows, menu.Row(currentRow...))
|
||||
}
|
||||
|
||||
cancelBtn := menu.Data("❌ 取消", cbCancel)
|
||||
rows = append(rows, menu.Row(cancelBtn))
|
||||
|
||||
menu.Inline(rows...)
|
||||
return menu
|
||||
}
|
||||
|
||||
func (b *Bot) handleCallback(c tele.Context) error {
|
||||
if !b.cfg.IsAdmin(c.Sender().ID) {
|
||||
return c.Respond(&tele.CallbackResponse{Text: "无权限"})
|
||||
}
|
||||
|
||||
data := c.Callback().Data
|
||||
userID := c.Sender().ID
|
||||
|
||||
switch {
|
||||
case data == cbCancel:
|
||||
return b.handleCancelCallback(c, userID)
|
||||
|
||||
case strings.HasPrefix(data, cbPrefixCat):
|
||||
category := strings.TrimPrefix(data, cbPrefixCat)
|
||||
return b.handleCategoryCallback(c, userID, category)
|
||||
|
||||
case strings.HasPrefix(data, cbPrefixConfirm):
|
||||
action := strings.TrimPrefix(data, cbPrefixConfirm)
|
||||
return b.handleConfirmCallback(c, userID, action)
|
||||
}
|
||||
|
||||
return c.Respond()
|
||||
}
|
||||
|
||||
func (b *Bot) handleCancelCallback(c tele.Context, userID int64) error {
|
||||
b.states.Delete(userID)
|
||||
c.Edit("❌ 已取消")
|
||||
return c.Respond(&tele.CallbackResponse{Text: "已取消"})
|
||||
}
|
||||
|
||||
func (b *Bot) handleCategoryCallback(c tele.Context, userID int64, category string) error {
|
||||
state := b.states.Get(userID)
|
||||
if state == nil || state.Step != StepAwaitCategory {
|
||||
return c.Respond(&tele.CallbackResponse{Text: "会话已过期"})
|
||||
}
|
||||
|
||||
b.states.SetCategory(userID, category)
|
||||
|
||||
channelName := "未知频道"
|
||||
if state.ForwardedMsg.OriginalChat != nil {
|
||||
channelName = state.ForwardedMsg.OriginalChat.Title
|
||||
}
|
||||
|
||||
menu := &tele.ReplyMarkup{}
|
||||
confirmBtn := menu.Data("✅ 确认", cbPrefixConfirm+"yes")
|
||||
cancelBtn := menu.Data("❌ 取消", cbCancel)
|
||||
menu.Inline(menu.Row(confirmBtn, cancelBtn))
|
||||
|
||||
text := fmt.Sprintf("📋 确认添加?\n\n频道: %s\n分类: %s", channelName, category)
|
||||
c.Edit(text, menu)
|
||||
return c.Respond()
|
||||
}
|
||||
|
||||
func (b *Bot) handleConfirmCallback(c tele.Context, userID int64, action string) error {
|
||||
if action != "yes" {
|
||||
return b.handleCancelCallback(c, userID)
|
||||
}
|
||||
|
||||
state := b.states.Get(userID)
|
||||
if state == nil || state.Step != StepAwaitConfirm {
|
||||
return c.Respond(&tele.CallbackResponse{Text: "会话已过期"})
|
||||
}
|
||||
|
||||
defer b.states.Delete(userID)
|
||||
|
||||
msg := state.ForwardedMsg
|
||||
title := extractTitle(msg)
|
||||
link := buildMessageLink(msg)
|
||||
|
||||
entry, err := b.storage.CreateEntry(state.SelectedCat, title, link)
|
||||
if err != nil {
|
||||
c.Edit(fmt.Sprintf("❌ 保存失败: %v", err))
|
||||
return c.Respond(&tele.CallbackResponse{Text: "保存失败"})
|
||||
}
|
||||
|
||||
b.toc.TriggerUpdate()
|
||||
|
||||
text := fmt.Sprintf("✅ 已添加\n\nID: %s\n分类: %s\n标题: %s", entry.ID, entry.Category, entry.Title)
|
||||
c.Edit(text)
|
||||
return c.Respond(&tele.CallbackResponse{Text: "添加成功"})
|
||||
}
|
||||
|
||||
func extractTitle(msg *tele.Message) string {
|
||||
text := msg.Text
|
||||
if text == "" {
|
||||
text = msg.Caption
|
||||
}
|
||||
|
||||
lines := strings.Split(text, "\n")
|
||||
title := strings.TrimSpace(lines[0])
|
||||
|
||||
if len(title) > 50 {
|
||||
title = title[:47] + "..."
|
||||
}
|
||||
|
||||
if title == "" {
|
||||
title = "无标题"
|
||||
}
|
||||
|
||||
return title
|
||||
}
|
||||
|
||||
func buildMessageLink(msg *tele.Message) string {
|
||||
if msg.OriginalChat == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
chat := msg.OriginalChat
|
||||
msgID := msg.OriginalMessageID
|
||||
if msgID == 0 {
|
||||
msgID = msg.ID
|
||||
}
|
||||
|
||||
if chat.Username != "" {
|
||||
return fmt.Sprintf("https://t.me/%s/%d", chat.Username, msgID)
|
||||
}
|
||||
|
||||
chatID := chat.ID
|
||||
if chatID < 0 {
|
||||
chatID = -chatID - 1000000000000
|
||||
}
|
||||
return fmt.Sprintf("https://t.me/c/%d/%d", chatID, msgID)
|
||||
}
|
||||
16
internal/telegram/middleware.go
Normal file
16
internal/telegram/middleware.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
tele "gopkg.in/telebot.v3"
|
||||
)
|
||||
|
||||
func (b *Bot) AdminMiddleware() tele.MiddlewareFunc {
|
||||
return func(next tele.HandlerFunc) tele.HandlerFunc {
|
||||
return func(c tele.Context) error {
|
||||
if !b.cfg.IsAdmin(c.Sender().ID) {
|
||||
return c.Reply("⛔ 无权限访问")
|
||||
}
|
||||
return next(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
91
internal/telegram/state.go
Normal file
91
internal/telegram/state.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
tele "gopkg.in/telebot.v3"
|
||||
)
|
||||
|
||||
type PostStep int
|
||||
|
||||
const (
|
||||
StepAwaitForward PostStep = iota
|
||||
StepAwaitCategory
|
||||
StepAwaitConfirm
|
||||
)
|
||||
|
||||
type PostState struct {
|
||||
UserID int64
|
||||
ForwardedMsg *tele.Message
|
||||
SelectedCat string
|
||||
Step PostStep
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type StateManager struct {
|
||||
mu sync.RWMutex
|
||||
states map[int64]*PostState
|
||||
}
|
||||
|
||||
func NewStateManager() *StateManager {
|
||||
return &StateManager{
|
||||
states: make(map[int64]*PostState),
|
||||
}
|
||||
}
|
||||
|
||||
func (sm *StateManager) Get(userID int64) *PostState {
|
||||
sm.mu.RLock()
|
||||
defer sm.mu.RUnlock()
|
||||
return sm.states[userID]
|
||||
}
|
||||
|
||||
func (sm *StateManager) Set(state *PostState) {
|
||||
sm.mu.Lock()
|
||||
defer sm.mu.Unlock()
|
||||
sm.states[state.UserID] = state
|
||||
}
|
||||
|
||||
func (sm *StateManager) Delete(userID int64) {
|
||||
sm.mu.Lock()
|
||||
defer sm.mu.Unlock()
|
||||
delete(sm.states, userID)
|
||||
}
|
||||
|
||||
func (sm *StateManager) StartPost(userID int64) *PostState {
|
||||
state := &PostState{
|
||||
UserID: userID,
|
||||
Step: StepAwaitForward,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
sm.Set(state)
|
||||
return state
|
||||
}
|
||||
|
||||
func (sm *StateManager) SetForwarded(userID int64, msg *tele.Message) *PostState {
|
||||
sm.mu.Lock()
|
||||
defer sm.mu.Unlock()
|
||||
|
||||
state := sm.states[userID]
|
||||
if state == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
state.ForwardedMsg = msg
|
||||
state.Step = StepAwaitCategory
|
||||
return state
|
||||
}
|
||||
|
||||
func (sm *StateManager) SetCategory(userID int64, category string) *PostState {
|
||||
sm.mu.Lock()
|
||||
defer sm.mu.Unlock()
|
||||
|
||||
state := sm.states[userID]
|
||||
if state == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
state.SelectedCat = category
|
||||
state.Step = StepAwaitConfirm
|
||||
return state
|
||||
}
|
||||
Reference in New Issue
Block a user