2023-05-26 04:43:51 +03:00
|
|
|
package telegram
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
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 {
|
|
|
|
for _, message := range messages {
|
|
|
|
var blobs map[string][]byte
|
2023-05-26 04:43:51 +03:00
|
|
|
|
2023-06-14 17:10:01 +03:00
|
|
|
// download blob if provided
|
|
|
|
if len(message.Photo) > 0 {
|
|
|
|
filepath, blob, err := b.downloadFileID(ctx, message.GetMaxPhotoFileID())
|
2023-05-26 04:43:51 +03:00
|
|
|
if err != nil {
|
2023-06-14 17:10:01 +03:00
|
|
|
return err
|
2023-05-26 04:43:51 +03:00
|
|
|
}
|
2023-06-14 17:10:01 +03:00
|
|
|
blobs = map[string][]byte{filepath: blob}
|
2023-05-26 04:43:51 +03:00
|
|
|
}
|
|
|
|
|
2023-06-14 17:10:01 +03:00
|
|
|
err := b.handler.MessageHandle(ctx, b, message, blobs)
|
|
|
|
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))
|
|
|
|
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 14:49:05 +03:00
|
|
|
filepath, blob, err := b.downloadFileID(ctx, message.GetMaxPhotoFileID())
|
2023-05-26 04:43:51 +03: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 {
|
|
|
|
// replace Caption with all Caption in the group
|
|
|
|
caption := captions[groupID]
|
|
|
|
message.Caption = &caption
|
2023-06-14 17:10:01 +03:00
|
|
|
err := b.handler.MessageHandle(ctx, b, message, blobs[groupID])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-05-26 04:43:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|