2023-05-26 04:43:51 +03:00
|
|
|
package telegram
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
)
|
|
|
|
|
2023-06-14 17:10:01 +03:00
|
|
|
// handleSingleMessages handle single messages not belongs to group.
|
|
|
|
func (b *Bot) handleSingleMessages(ctx context.Context, messages []Message) error {
|
2023-07-13 19:18:44 +03:00
|
|
|
var attachments []Attachment
|
|
|
|
|
2023-06-14 17:10:01 +03:00
|
|
|
for _, message := range messages {
|
2023-07-13 19:18:44 +03:00
|
|
|
attachment, err := b.downloadAttachment(ctx, &message)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-26 04:43:51 +03:00
|
|
|
|
2023-07-13 19:18:44 +03:00
|
|
|
if attachment != nil {
|
|
|
|
attachments = append(attachments, *attachment)
|
2023-05-26 04:43:51 +03:00
|
|
|
}
|
|
|
|
|
2023-07-13 19:18:44 +03:00
|
|
|
err = b.handler.MessageHandle(ctx, b, message, attachments)
|
2023-06-14 17:10:01 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-05-26 04:43:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleGroupMessages handle a message belongs to group.
|
2023-05-29 14:49:05 +03:00
|
|
|
func (b *Bot) handleGroupMessages(ctx context.Context, groupMessages []Message) error {
|
2023-05-26 04:43:51 +03:00
|
|
|
captions := make(map[string]string, len(groupMessages))
|
|
|
|
messages := make(map[string]Message, len(groupMessages))
|
2023-07-13 19:18:44 +03:00
|
|
|
attachments := make(map[string][]Attachment, len(groupMessages))
|
2023-05-26 04:43:51 +03:00
|
|
|
|
|
|
|
// 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-07-13 19:18:44 +03:00
|
|
|
attachment, err := b.downloadAttachment(ctx, &message)
|
2023-05-26 04:43:51 +03:00
|
|
|
if err != nil {
|
2023-07-13 19:18:44 +03:00
|
|
|
return err
|
2023-05-26 04:43:51 +03:00
|
|
|
}
|
2023-07-13 19:18:44 +03:00
|
|
|
|
|
|
|
if attachment != nil {
|
|
|
|
attachments[groupID] = append(attachments[groupID], *attachment)
|
2023-05-26 04:43:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle each group message
|
|
|
|
for groupID, message := range messages {
|
|
|
|
// replace Caption with all Caption in the group
|
|
|
|
caption := captions[groupID]
|
|
|
|
message.Caption = &caption
|
2023-07-13 19:18:44 +03:00
|
|
|
err := b.handler.MessageHandle(ctx, b, message, attachments[groupID])
|
2023-06-14 17:10:01 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-05-26 04:43:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|