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