更新封面和readme
This commit is contained in:
@@ -12,10 +12,11 @@ import (
|
||||
)
|
||||
|
||||
type Manager struct {
|
||||
storage *storage.Storage
|
||||
bot *tele.Bot
|
||||
chanID int64
|
||||
debounce time.Duration
|
||||
storage *storage.Storage
|
||||
bot *tele.Bot
|
||||
chanID int64
|
||||
debounce time.Duration
|
||||
coverImage string
|
||||
|
||||
mu sync.Mutex
|
||||
pending bool
|
||||
@@ -23,13 +24,14 @@ type Manager struct {
|
||||
stopCh chan struct{}
|
||||
}
|
||||
|
||||
func NewManager(store *storage.Storage, bot *tele.Bot, chanID int64, debounce time.Duration) *Manager {
|
||||
func NewManager(store *storage.Storage, bot *tele.Bot, chanID int64, debounce time.Duration, coverImage string) *Manager {
|
||||
return &Manager{
|
||||
storage: store,
|
||||
bot: bot,
|
||||
chanID: chanID,
|
||||
debounce: debounce,
|
||||
stopCh: make(chan struct{}),
|
||||
storage: store,
|
||||
bot: bot,
|
||||
chanID: chanID,
|
||||
debounce: debounce,
|
||||
coverImage: coverImage,
|
||||
stopCh: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,6 +74,12 @@ func (m *Manager) updateMessage(content string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// 带封面图片模式
|
||||
if m.coverImage != "" {
|
||||
return m.updateWithPhoto(chat, msgID, content)
|
||||
}
|
||||
|
||||
// 纯文本模式
|
||||
if msgID == 0 {
|
||||
msg, err := m.bot.Send(chat, content, tele.ModeMarkdown, tele.NoPreview)
|
||||
if err != nil {
|
||||
@@ -105,6 +113,47 @@ func (m *Manager) updateMessage(content string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Manager) updateWithPhoto(chat *tele.Chat, msgID int, content string) error {
|
||||
photo := &tele.Photo{
|
||||
File: tele.FromDisk(m.coverImage),
|
||||
Caption: content,
|
||||
}
|
||||
|
||||
if msgID == 0 {
|
||||
msg, err := m.bot.Send(chat, photo, tele.ModeMarkdown)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return m.storage.SetTocMsgID(msg.ID)
|
||||
}
|
||||
|
||||
existingMsg := &tele.Message{
|
||||
ID: msgID,
|
||||
Chat: chat,
|
||||
}
|
||||
|
||||
// 编辑图片消息的 caption
|
||||
_, err := m.bot.EditCaption(existingMsg, content, tele.ModeMarkdown)
|
||||
if err != nil {
|
||||
errMsg := err.Error()
|
||||
if err == tele.ErrMessageNotModified || strings.Contains(errMsg, "message is not modified") {
|
||||
return nil
|
||||
}
|
||||
// 旧消息不是图片或找不到,重新发送
|
||||
if strings.Contains(errMsg, "message to edit not found") ||
|
||||
strings.Contains(errMsg, "no caption") {
|
||||
msg, err := m.bot.Send(chat, photo, tele.ModeMarkdown)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return m.storage.SetTocMsgID(msg.ID)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Manager) Stop() {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
@@ -45,7 +45,6 @@ func (m *Manager) Render() (string, error) {
|
||||
}
|
||||
|
||||
sb.WriteString("━━━━━━━━━━━━━━━\n")
|
||||
sb.WriteString("_自动生成_")
|
||||
|
||||
return sb.String(), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user