Overview
This example shows how to retrieve all your dialogs (chats, groups, channels) with pagination and filtering options.
Code
package main
import (
"fmt"
"github.com/amarnathcjd/gogram/telegram"
)
func main() {
// Create a new Telegram client
client, _ := telegram.NewClient(telegram.ClientConfig{
AppID: 6,
AppHash: "YOUR_APP_HASH",
})
// Login with your phone number
client.Login("+1234567890")
// Get first 5 dialogs
dialogs, _ := client.GetDialogs(&telegram.DialogOptions{
Limit: 5, // Fetch only 5 dialogs
})
// Alternative: Use IterDialogs() for iterating through all dialogs
// diagsChan, errChan := client.IterDialogs()
// Print each dialog's ID
for _, dialog := range dialogs {
fmt.Printf("Dialog: %+v\n", dialog.GetID())
}
// Keep the client running
client.Idle()
}
How It Works
- Authentication: Connects using user account credentials
- Fetch Dialogs: Calls
GetDialogs() with limit option
- Display Results: Prints each dialog’s ID
- Pagination: Use DialogOptions to control fetching behavior
DialogOptions Parameters
Message ID offset for pagination
Date offset for pagination (Unix timestamp)
Peer offset for pagination
Maximum number of dialogs to fetch
Exclude pinned dialogs from results
Fetch dialogs from specific folder (0 = main, 1 = archived)
Hash for caching purposes
Sleep threshold in milliseconds for rate limiting
Practical Examples
Get All Dialogs
Iterate All Dialogs
Exclude Pinned
Archived Dialogs
Dialog Details
// Get all dialogs with detailed information
dialogs, _ := client.GetDialogs(&telegram.DialogOptions{
Limit: 100, // Fetch 100 dialogs at a time
})
for _, dialog := range dialogs {
fmt.Printf("ID: %d, Type: %s\n", dialog.GetID(), dialog.GetType())
}
Using IterDialogs
For large dialog lists, use IterDialogs() to avoid loading everything into memory:
diagsChan, errChan := client.IterDialogs()
count := 0
for {
select {
case dialog := <-diagsChan:
if dialog == nil {
goto complete
}
count++
fmt.Printf("%d. %s (ID: %d)\n", count, dialog.GetTitle(), dialog.GetID())
case err := <-errChan:
if err != nil {
fmt.Println("Error fetching dialogs:", err)
goto complete
}
}
}
complete:
fmt.Printf("\nTotal dialogs: %d\n", count)
Running the Example
- Replace
YOUR_APP_HASH with your actual app hash from my.telegram.org
- Replace
+1234567890 with your phone number
- Run the program
This requires a user account. Bots can only see dialogs where they are members.
Next Steps