Get history

This example shows how to get the full message history of a chat, starting from the latest message.

main.go
package main
 
import (
    "fmt"
    "github.com/amarnathcjd/gogram/telegram"
)
 
func main() {
    client, _ := telegram.NewClient(telegram.ClientConfig{ // Create a new client
        AppID:   6,
        AppHash: "app_hash",
    })
    client.Start()
 
    chatId := int64(-1001234567890) // chat id of the chat whose history is to be fetched
    messages, err := client.GetHistory(chatId, &telegram.HistoryOption{
		Limit: 1000,
		Offset: 0,
	})
 
    if err != nil {
        fmt.Println("Error:", err)
        return
    }
 
    for _, message := range messages {
        fmt.Println(message.Marshal()) // .Marshal() returns the message in JSON format
    }
 
    fmt.Println("Total messages:", len(messages))
}

Note: Can only be used by user bots.