Gogram Client

Client

The Client struct is the main struct in Gogram. It incorporates the underlying mtproto connection, the session store, and the update handlers.

client exposes high-level methods to interact with the Telegram API.

Creating a Client

To create a new client, use the NewClient function:

main.go
client, err := telegram.NewClient(telegram.ClientConfig{})

Client Configuration

The following fields are available in the ClientConfig struct:

  • AppID (int): The Telegram API ID.
  • AppHash (string): The Telegram API hash.
  • DeviceConfig (DeviceConfig): The device configuration.
  • Session (string): file path to the session file.
  • StringSession (string): the session string.
  • LangCode (string): the language code. Default is en.
  • ParseMode (string): the algorithm used to parse the message text. Default is html. [html|markdown|none]
  • MemorySession (bool): whether to store the session in memory. Default is false.
  • DataCenter (int): explicitly set the data center. Default is 4. (Deprecated)
  • IpAddr (string): the IP address of the custom data center.
  • PublicKeys ([]*rsa.PublicKey): the public keys of telegram.
  • NoUpdates (bool): whether to disable updates. Default is false.
  • DisableCache (bool): whether to disable caching. Default is false.
  • TestMode (bool): whether to enable test mode. connect to test servers. Default is false.
  • LogLevel (telegram.LogLevel): the log level. Default is telegram.LogInfo.
  • Proxy (*url.URL): the proxy URL.

DeviceConfig

The DeviceConfig struct is used to configure the device information for the client:

  • DeviceModel (string): The device model.
  • SystemVersion (string): The system version.
  • AppVersion (string): The app version.

DataCenters

Telegram has 5 data centers.

  • - 1 - USA
  • - 2 - Europe
  • - 3 - USA
  • - 4 - Europe (netherlands) (default)
  • - 5 - Asia (singapore)

Apart from these, there are test servers available for 1-3 data centers. each user / bot is associated with a specific data center and is redirected to the appropriate data center by the library.

PublicKeys

The public keys of Telegram are used to verify the authenticity of the server. normaly, the default public keys are used. but you can provide your own public keys if you want.

Session

The session is used to store the user authentication information securely. There are three types of sessions available:

MemorySession

If MemorySession is set to true, the session will be stored in memory instead of a file.

ParseMode

  • html: Parse the message text as HTML. (eg. <b>bold</b>)
  • markdown: Parse the message text as Markdown. (eg. *bold*)
  • none: Do not parse the message text.
client.SetParseMode(telegram.HTML)

LogLevel

Available log levels:

  • telegram.LogDebug
  • telegram.LogInfo
  • telegram.LogWarn
  • telegram.LogError
  • telegram.LogDisable
client.SetLogLevel(telegram.LogDebug)

Proxy

http, https, socks4, socks4a, socks5 proxies are supported.

TODO: mtproxy support


Example

main.go
client, err := telegram.NewClient(telegram.ClientConfig{
    AppID:   6,
    AppHash: "app_hash",
    DeviceConfig: telegram.DeviceConfig{
        DeviceModel:    "iPhone 12",
        SystemVersion:  "iOS 15.0",
        AppVersion:     "8.0.1",
    },
    Session: "session.dat",
    LangCode: "en",
    ParseMode: "html",
    MemorySession: false,
    DataCenter: 4,
    NoUpdates: false,
    DisableCache: false,
    TestMode: false,
    LogLevel: telegram.LogInfo,
    Proxy: &url.URL{
        Scheme: "socks5",
        Host:   "127.0.0.1:1080",
        User:   url.UserPassword("username", "password"),
    },
})

NOTE: The AppID and AppHash can be obtained by creating a new application on the Telegram website (opens in a new tab).