Overview
Album represents a group of media messages sent together (media group/album). Telegram allows sending up to 10 media items as a single grouped message.
Structure
Copy
type Album struct {
Client *Client
GroupedID int64
Messages []*NewMessage
}
Properties
Unique identifier for the media group
Copy
groupID := album.GroupedID
Array of messages in the album (sorted by ID)
Copy
for i, msg := range album.Messages {
fmt.Printf("Item %d: %s\n", i+1, msg.MediaType())
}
Client instance for making API calls
Copy
client := album.Client
Basic Usage
Handling Albums
Copy
client.On("album", func(a *telegram.Album) error {
fmt.Printf("Received album with %d items\n", len(a.Messages))
for i, msg := range a.Messages {
fmt.Printf("%d. %s\n", i+1, msg.MediaType())
}
a.Messages[0].Reply("Received your album!")
return nil
})
Accessing Individual Messages
Copy
client.On("album", func(a *telegram.Album) error {
// First message
first := a.Messages[0]
// Last message
last := a.Messages[len(a.Messages)-1]
// Iterate all
for idx, msg := range a.Messages {
fmt.Printf("Message %d: ID=%d, Type=%s\n",
idx, msg.ID, msg.MediaType())
}
return nil
})
Response Methods
All response methods work through the first message in the album.Text Responses
Copy
// Reply to the album
a.Reply("Great photos!")
// With options
a.Reply("Nice album!", telegram.SendOptions{
ParseMode: "HTML",
})
Media Responses
Copy
// Reply with single media
a.ReplyMedia(&telegram.InputMediaPhoto{
File: "response.jpg",
}, telegram.MediaOptions{
Caption: "Here's a response photo",
})
Album Actions
Downloading Album
Copy
// Download all media in album
paths, err := a.Download()
if err == nil {
for i, path := range paths {
fmt.Printf("Downloaded %d: %s\n", i+1, path)
}
}
Deleting Album
Copy
// Delete entire album
affected, err := a.Delete()
if err == nil {
fmt.Printf("Deleted %d messages\n", affected.PtsCount)
}
// Delete works on all messages in the album
Forwarding Album
Copy
// Forward entire album to another chat
messages, err := a.ForwardTo(targetChatID)
if err == nil {
fmt.Printf("Forwarded %d items\n", len(messages))
}
// Forward with options
messages, err := a.ForwardTo(targetChatID, &telegram.ForwardOptions{
Silent: true,
NoCaption: true,
})
Editing Album
Copy
// Edit caption of first message
a.Edit("Updated album caption")
// Edit with options
a.Edit("New caption", telegram.SendOptions{
ParseMode: "Markdown",
})
// Note: This only edits the first message's caption
Album Checks
Message Type Checks
- Reply
- Forward
Copy
if a.IsReply() {
replyMsg, _ := a.GetReplyMessage()
fmt.Println("Album is reply to:", replyMsg.Text())
}
Media Type Analysis
Copy
client.On("album", func(a *telegram.Album) error {
var photos, videos, docs int
for _, msg := range a.Messages {
switch msg.MediaType() {
case "photo":
photos++
case "video":
videos++
case "document":
docs++
}
}
a.Reply(fmt.Sprintf("Album contains:\n"+
"Photos: %d\nVideos: %d\nDocuments: %d",
photos, videos, docs))
return nil
})
Utilities
Mark as Read
Copy
// Mark album as read
err := a.MarkRead()
Pin/Unpin Album
Copy
// Pin first message of album
a.Pin()
// Pin silently
a.Pin(&telegram.PinOptions{
Silent: true,
})
Practical Examples
Download Only Photos
Copy
client.On("album", func(a *telegram.Album) error {
var photoPaths []string
for i, msg := range a.Messages {
if msg.Photo() != nil {
path, err := msg.Download(&telegram.DownloadOptions{
Output: fmt.Sprintf("photo_%d.jpg", i),
})
if err == nil {
photoPaths = append(photoPaths, path)
}
}
}
a.Reply(fmt.Sprintf("Downloaded %d photos", len(photoPaths)))
return nil
})
Process Album with Captions
Copy
client.On("album", func(a *telegram.Album) error {
var captions []string
for i, msg := range a.Messages {
text := msg.Text()
if text != "" {
captions = append(captions,
fmt.Sprintf("%d. %s", i+1, text))
}
}
if len(captions) > 0 {
a.Reply("Captions:\n" + strings.Join(captions, "\n"))
}
return nil
})
Create Album Response
Copy
client.On("album", func(a *telegram.Album) error {
// Process album
a.Reply("Processing your album...")
// Create response album
responseMedia := []telegram.InputMedia{
&telegram.InputMediaPhoto{
File: "processed1.jpg",
Caption: "Processed #1",
},
&telegram.InputMediaPhoto{
File: "processed2.jpg",
Caption: "Processed #2",
},
}
// Send as reply
_, err := client.SendAlbum(a.Messages[0].ChatID(), responseMedia,
&telegram.MediaOptions{
ReplyID: a.Messages[0].ID,
})
return err
})
Album Size Validation
Copy
client.On("album", func(a *telegram.Album) error {
var totalSize int64
maxSize := int64(50 * 1024 * 1024) // 50 MB
for _, msg := range a.Messages {
if doc := msg.Document(); doc != nil {
totalSize += doc.Size
}
if photo := msg.Photo(); photo != nil {
// Estimate photo size from largest size
if len(photo.Sizes) > 0 {
if size, ok := photo.Sizes[len(photo.Sizes)-1].(*telegram.PhotoSizeObj); ok {
totalSize += int64(size.Size)
}
}
}
}
if totalSize > maxSize {
a.Reply("Album too large! Maximum 50 MB allowed.")
return nil
}
paths, _ := a.Download()
a.Reply(fmt.Sprintf("Downloaded %d files", len(paths)))
return nil
})
Forward Album Selectively
Copy
client.On("album", func(a *telegram.Album) error {
var photoIDs []int32
// Collect only photo IDs
for _, msg := range a.Messages {
if msg.Photo() != nil {
photoIDs = append(photoIDs, msg.ID)
}
}
if len(photoIDs) > 0 {
// Forward only photos
_, err := client.Forward(
targetChatID,
a.Messages[0].ChatID(),
photoIDs,
)
if err == nil {
a.Reply(fmt.Sprintf("Forwarded %d photos", len(photoIDs)))
}
}
return nil
})
Complete Example
Copy
package main
import (
"fmt"
"github.com/amarnathcjd/gogram/telegram"
)
func main() {
client, _ := telegram.NewClient(telegram.ClientConfig{
AppID: 6, AppHash: "app_hash",
})
client.LoginBot("bot_token")
client.On("album", func(a *telegram.Album) error {
count := len(a.Messages)
fmt.Printf("Album received: %d items, GroupID: %d\n",
count, a.GroupedID)
// Analyze album contents
var photos, videos, others int
var totalSize int64
for i, msg := range a.Messages {
fmt.Printf("%d. ID: %d, Type: %s\n",
i+1, msg.ID, msg.MediaType())
switch msg.MediaType() {
case "photo":
photos++
case "video":
videos++
default:
others++
}
// Get size
if doc := msg.Document(); doc != nil {
totalSize += doc.Size
}
}
// Send summary
summary := fmt.Sprintf(
"📦 Album Summary\n\n"+
"📸 Photos: %d\n"+
"🎥 Videos: %d\n"+
"📄 Other: %d\n"+
"📊 Total: %d items\n"+
"💾 Size: %.2f MB",
photos, videos, others, count,
float64(totalSize)/(1024*1024),
)
a.Reply(summary)
// Download if small enough
if totalSize < 10*1024*1024 { // 10 MB
paths, err := a.Download(&telegram.DownloadOptions{
Output: "albums/",
})
if err == nil {
a.Reply(fmt.Sprintf("✅ Downloaded %d files", len(paths)))
}
} else {
a.Reply("⚠️ Album too large to download automatically")
}
return nil
})
client.Idle()
}