目录保留消息上下文并增加直达链接
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"tgchanbot/internal/storage"
|
||||
|
||||
@@ -21,10 +22,12 @@ const (
|
||||
)
|
||||
|
||||
// 相册收集定时器
|
||||
var (
|
||||
albumTimers = make(map[int64]*time.Timer)
|
||||
albumTimersMu sync.Mutex
|
||||
)
|
||||
var (
|
||||
albumTimers = make(map[int64]*time.Timer)
|
||||
albumTimersMu sync.Mutex
|
||||
)
|
||||
|
||||
var directURLPattern = regexp.MustCompile(`https?://[^\s<>"']+`)
|
||||
|
||||
func (b *Bot) handlePost(c tele.Context) error {
|
||||
msg := c.Message()
|
||||
@@ -69,20 +72,20 @@ func (b *Bot) handleQuickPost(c tele.Context, replyMsg *tele.Message, payload st
|
||||
title = extractTitle(replyMsg)
|
||||
}
|
||||
|
||||
// 构建链接
|
||||
link := buildMessageLinkFromReply(c.Message(), replyMsg)
|
||||
|
||||
// 创建条目
|
||||
entry, err := b.storage.CreateEntry(category, title, link)
|
||||
if err != nil {
|
||||
return c.Reply(fmt.Sprintf("❌ 保存失败: %v", err))
|
||||
}
|
||||
channelLink := buildMessageLinkFromReply(c.Message(), replyMsg)
|
||||
directLink := extractDirectLink(replyMsg)
|
||||
|
||||
// 创建条目
|
||||
entry, err := b.storage.CreateEntry(category, title, channelLink, directLink)
|
||||
if err != nil {
|
||||
return c.Reply(fmt.Sprintf("❌ 保存失败: %v", err))
|
||||
}
|
||||
|
||||
b.toc.TriggerUpdate()
|
||||
|
||||
return c.Reply(fmt.Sprintf("✅ 已添加\n\nID: `%s`\n分类: %s\n标题: %s\n链接: %s",
|
||||
entry.ID, entry.Category, entry.Title, entry.Link), tele.ModeMarkdown)
|
||||
}
|
||||
return c.Reply(fmt.Sprintf("✅ 已添加\n\nID: `%s`\n分类: %s\n标题: %s\n链接: %s",
|
||||
entry.ID, entry.Category, entry.Title, entry.MessageLink()), tele.ModeMarkdown)
|
||||
}
|
||||
|
||||
func buildMessageLinkFromReply(currentMsg, replyMsg *tele.Message) string {
|
||||
chat := currentMsg.Chat
|
||||
@@ -437,13 +440,13 @@ func (b *Bot) handleConfirmCallback(c tele.Context, userID int64, action string)
|
||||
return c.Respond(&tele.CallbackResponse{Text: "发送失败"})
|
||||
}
|
||||
|
||||
// 使用频道消息的链接
|
||||
link := b.buildChannelLink(channelMsg.ID)
|
||||
|
||||
entry, err := b.storage.CreateEntry(state.SelectedCat, title, link)
|
||||
if err != nil {
|
||||
c.Edit(fmt.Sprintf("❌ 保存失败: %v", err))
|
||||
return c.Respond(&tele.CallbackResponse{Text: "保存失败"})
|
||||
channelLink := b.buildChannelLink(channelMsg.ID)
|
||||
directLink := extractDirectLinkFromState(state)
|
||||
|
||||
entry, err := b.storage.CreateEntry(state.SelectedCat, title, channelLink, directLink)
|
||||
if err != nil {
|
||||
c.Edit(fmt.Sprintf("❌ 保存失败: %v", err))
|
||||
return c.Respond(&tele.CallbackResponse{Text: "保存失败"})
|
||||
}
|
||||
|
||||
b.toc.TriggerUpdate()
|
||||
@@ -499,7 +502,7 @@ func (b *Bot) buildChannelLink(msgID int) string {
|
||||
return fmt.Sprintf("https://t.me/c/%d/%d", chatID, msgID)
|
||||
}
|
||||
|
||||
func extractTitle(msg *tele.Message) string {
|
||||
func extractTitle(msg *tele.Message) string {
|
||||
text := msg.Text
|
||||
if text == "" {
|
||||
text = msg.Caption
|
||||
@@ -516,8 +519,58 @@ func extractTitle(msg *tele.Message) string {
|
||||
title = "无标题"
|
||||
}
|
||||
|
||||
return title
|
||||
}
|
||||
return title
|
||||
}
|
||||
|
||||
func extractDirectLinkFromMessages(msgs []*tele.Message) string {
|
||||
for _, msg := range msgs {
|
||||
if msg == nil {
|
||||
continue
|
||||
}
|
||||
if link := extractDirectLink(msg); link != "" {
|
||||
return link
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func extractDirectLinkFromState(state *PostState) string {
|
||||
if state == nil {
|
||||
return ""
|
||||
}
|
||||
if link := extractDirectLink(state.ForwardedMsg); link != "" {
|
||||
return link
|
||||
}
|
||||
return extractDirectLinkFromMessages(state.ForwardedMsgs)
|
||||
}
|
||||
|
||||
func extractDirectLink(msg *tele.Message) string {
|
||||
if msg == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
for _, entity := range msg.Entities {
|
||||
if entity.Type == tele.EntityTextLink && entity.URL != "" {
|
||||
return entity.URL
|
||||
}
|
||||
}
|
||||
for _, entity := range msg.CaptionEntities {
|
||||
if entity.Type == tele.EntityTextLink && entity.URL != "" {
|
||||
return entity.URL
|
||||
}
|
||||
}
|
||||
|
||||
for _, text := range []string{msg.Text, msg.Caption} {
|
||||
if text == "" {
|
||||
continue
|
||||
}
|
||||
if match := directURLPattern.FindString(text); match != "" {
|
||||
return match
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
func buildMessageLink(msg *tele.Message) string {
|
||||
if msg.OriginalChat == nil {
|
||||
|
||||
Reference in New Issue
Block a user