2023-05-26 09:43:51 +08:00
|
|
|
package telegram
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/usememos/memos/common/log"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// notice message send to telegram.
|
|
|
|
const (
|
|
|
|
workingMessage = "Working on send your memo..."
|
|
|
|
successMessage = "Success"
|
|
|
|
)
|
|
|
|
|
|
|
|
// handleSingleMessage handle a message not belongs to group.
|
2023-05-29 19:49:05 +08:00
|
|
|
func (b *Bot) handleSingleMessage(ctx context.Context, message Message) error {
|
|
|
|
reply, err := b.SendReplyMessage(ctx, message.Chat.ID, message.MessageID, workingMessage)
|
2023-05-26 09:43:51 +08:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("fail to SendReplyMessage: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var blobs map[string][]byte
|
|
|
|
|
|
|
|
// download blob if need
|
|
|
|
if len(message.Photo) > 0 {
|
2023-05-29 19:49:05 +08:00
|
|
|
filepath, blob, err := b.downloadFileID(ctx, message.GetMaxPhotoFileID())
|
2023-05-26 09:43:51 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Error("fail to downloadFileID", zap.Error(err))
|
2023-05-29 19:49:05 +08:00
|
|
|
_, err = b.EditMessage(ctx, message.Chat.ID, reply.MessageID, err.Error())
|
2023-05-26 09:43:51 +08:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("fail to EditMessage: %s", err)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("fail to downloadFileID: %s", err)
|
|
|
|
}
|
|
|
|
blobs = map[string][]byte{filepath: blob}
|
|
|
|
}
|
|
|
|
|
2023-05-29 19:49:05 +08:00
|
|
|
err = b.handler.MessageHandle(ctx, message, blobs)
|
2023-05-26 09:43:51 +08:00
|
|
|
if err != nil {
|
2023-05-29 19:49:05 +08:00
|
|
|
if _, err := b.EditMessage(ctx, message.Chat.ID, reply.MessageID, err.Error()); err != nil {
|
2023-05-26 09:43:51 +08:00
|
|
|
return fmt.Errorf("fail to EditMessage: %s", err)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("fail to MessageHandle: %s", err)
|
|
|
|
}
|
|
|
|
|
2023-05-29 19:49:05 +08:00
|
|
|
if _, err := b.EditMessage(ctx, message.Chat.ID, reply.MessageID, successMessage); err != nil {
|
2023-05-26 09:43:51 +08:00
|
|
|
return fmt.Errorf("fail to EditMessage: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleGroupMessages handle a message belongs to group.
|
2023-05-29 19:49:05 +08:00
|
|
|
func (b *Bot) handleGroupMessages(ctx context.Context, groupMessages []Message) error {
|
2023-05-26 09:43:51 +08:00
|
|
|
captions := make(map[string]string, len(groupMessages))
|
|
|
|
messages := make(map[string]Message, len(groupMessages))
|
|
|
|
blobs := make(map[string]map[string][]byte, len(groupMessages))
|
|
|
|
|
|
|
|
// Group all captions, blobs and messages
|
|
|
|
for _, message := range groupMessages {
|
|
|
|
groupID := *message.MediaGroupID
|
|
|
|
|
|
|
|
messages[groupID] = message
|
|
|
|
|
|
|
|
if message.Caption != nil {
|
|
|
|
captions[groupID] += *message.Caption
|
|
|
|
}
|
|
|
|
|
2023-05-29 19:49:05 +08:00
|
|
|
filepath, blob, err := b.downloadFileID(ctx, message.GetMaxPhotoFileID())
|
2023-05-26 09:43:51 +08:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("fail to downloadFileID")
|
|
|
|
}
|
|
|
|
if _, found := blobs[groupID]; !found {
|
|
|
|
blobs[groupID] = make(map[string][]byte)
|
|
|
|
}
|
|
|
|
blobs[groupID][filepath] = blob
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle each group message
|
|
|
|
for groupID, message := range messages {
|
2023-05-29 19:49:05 +08:00
|
|
|
reply, err := b.SendReplyMessage(ctx, message.Chat.ID, message.MessageID, workingMessage)
|
2023-05-26 09:43:51 +08:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("fail to SendReplyMessage: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// replace Caption with all Caption in the group
|
|
|
|
caption := captions[groupID]
|
|
|
|
message.Caption = &caption
|
2023-05-29 19:49:05 +08:00
|
|
|
if err := b.handler.MessageHandle(ctx, message, blobs[groupID]); err != nil {
|
|
|
|
if _, err = b.EditMessage(ctx, message.Chat.ID, reply.MessageID, err.Error()); err != nil {
|
2023-05-26 09:43:51 +08:00
|
|
|
return fmt.Errorf("fail to EditMessage: %s", err)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("fail to MessageHandle: %s", err)
|
|
|
|
}
|
|
|
|
|
2023-05-29 19:49:05 +08:00
|
|
|
if _, err := b.EditMessage(ctx, message.Chat.ID, reply.MessageID, successMessage); err != nil {
|
2023-05-26 09:43:51 +08:00
|
|
|
return fmt.Errorf("fail to EditMessage: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|