mirror of
https://github.com/usememos/memos.git
synced 2024-12-29 22:33:39 +03:00
c5a1f4c839
* feat: format message from telegram and download documents * fix: remove bool in expression * refactor: convert to markdown * refactor: resolve remarks and add support new message types * refactor: resolve remarks * feat: add test for mime type --------- Co-authored-by: Александр Тумайкин <AATumaykin@tsum.ru>
69 lines
1.6 KiB
Go
69 lines
1.6 KiB
Go
package telegram
|
|
|
|
import (
|
|
"context"
|
|
)
|
|
|
|
// handleSingleMessages handle single messages not belongs to group.
|
|
func (b *Bot) handleSingleMessages(ctx context.Context, messages []Message) error {
|
|
var attachments []Attachment
|
|
|
|
for _, message := range messages {
|
|
attachment, err := b.downloadAttachment(ctx, &message)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if attachment != nil {
|
|
attachments = append(attachments, *attachment)
|
|
}
|
|
|
|
err = b.handler.MessageHandle(ctx, b, message, attachments)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// handleGroupMessages handle a message belongs to group.
|
|
func (b *Bot) handleGroupMessages(ctx context.Context, groupMessages []Message) error {
|
|
captions := make(map[string]string, len(groupMessages))
|
|
messages := make(map[string]Message, len(groupMessages))
|
|
attachments := make(map[string][]Attachment, 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
|
|
}
|
|
|
|
attachment, err := b.downloadAttachment(ctx, &message)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if attachment != nil {
|
|
attachments[groupID] = append(attachments[groupID], *attachment)
|
|
}
|
|
}
|
|
|
|
// Handle each group message
|
|
for groupID, message := range messages {
|
|
// replace Caption with all Caption in the group
|
|
caption := captions[groupID]
|
|
message.Caption = &caption
|
|
err := b.handler.MessageHandle(ctx, b, message, attachments[groupID])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|