Skip to main content

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

  1. Authentication: Connects using user account credentials
  2. Fetch Dialogs: Calls GetDialogs() with limit option
  3. Display Results: Prints each dialog’s ID
  4. Pagination: Use DialogOptions to control fetching behavior

DialogOptions Parameters

OffsetID
int32
Message ID offset for pagination
OffsetDate
int32
Date offset for pagination (Unix timestamp)
OffsetPeer
InputPeer
Peer offset for pagination
Limit
int32
Maximum number of dialogs to fetch
ExcludePinned
bool
Exclude pinned dialogs from results
FolderID
int32
Fetch dialogs from specific folder (0 = main, 1 = archived)
Hash
int64
Hash for caching purposes
SleepThresholdMs
int32
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

  1. Replace YOUR_APP_HASH with your actual app hash from my.telegram.org
  2. Replace +1234567890 with your phone number
  3. Run the program
This requires a user account. Bots can only see dialogs where they are members.

Next Steps