Skip to content

MTGo Client & Configuration

Creating a Client

go
client, err := tg.NewClient(apiID, apiHash, &tg.Config{...})

NewClient accepts your Telegram API ID, API hash, and a Config pointer. The config controls authentication, session persistence, transport, and client behavior.

Configuration

All fields have sensible defaults from tg.DefaultConfig. Override only what you need:

go
cfg := tg.DefaultConfig
cfg.SessionName = "my_bot"
cfg.BotToken = "123456:ABC-DEF"
cfg.InMemory = true
cfg.DeviceModel = "MyApp"

client, err := tg.NewClient(apiID, apiHash, &cfg)

Key Fields

FieldTypeDescription
BotTokenstringBot API token for bot authentication
PhoneNumberstringPhone number for userbot authentication
SessionNamestringLabel for session file persistence
SessionStringstringExported session string for resuming
InMemoryboolKeep session data in memory only
WorkDirstringDirectory for session files
WebSocketboolUse WebSocket transport instead of TCP
WebSocketTLSboolEnable TLS on WebSocket connection
NoUpdatesboolDisable the update receiving loop
SkipUpdatesboolDiscard stale updates on reconnection (default: true)
ParseModeintDefault text parsing mode (Markdown/HTML)
WorkersintNumber of update processing goroutines
HandlerTimeouttime.DurationMax time a handler may run
MaxConcurrentTransintMax parallel file transfers (default: 1)
FetchRepliesboolResolve reply-to references (default: true)
FetchTopicsboolLoad forum topic metadata (default: true)
Proxy*ProxyProxy configuration for routing traffic

Device Info

These fields identify your client to Telegram and appear in "Active Sessions":

go
cfg := &tg.Config{
    DeviceModel:   "My Server",
    SystemVersion: "Linux 6.1",
    AppVersion:    "1.0.0",
    LangCode:      "en",
    LangPack:      "tdesktop",
}

Connection Lifecycle

go
// Connect and start update loop
err := client.Connect(30 * time.Second)

// Check if connected
connected := client.IsConnected()

// Graceful disconnect
client.Disconnect()

// Disconnect and clean up all resources
client.Stop()

// Block main goroutine until interrupted
client.Idle()

Proxy

Route MTProto traffic through a proxy:

go
cfg := &tg.Config{
    Proxy: &tg.Proxy{
        Addr:     "proxy.example.com:1080",
        Username: "user",
        Password: "pass",
    },
}

Logging

Configure logging for debugging:

go
cfg := &tg.Config{
    Log: tg.LogConfig{
        Level: tg.LogLevelDebug,
        File:  "mtgo.log",
    },
}

Log levels: LogLevelNone, LogLevelError, LogLevelWarn, LogLevelInfo, LogLevelDebug.

Released under the Apache-2.0 License.