Gogram supports multiple authentication methods to connect to Telegram. You can authenticate as a bot using a bot token or as a user using a phone number.
client, err := telegram.NewClient(telegram.ClientConfig{ AppID: 6, AppHash: "your_app_hash", SessionName: "my_session", // Custom session name Session: "session.dat", // Custom session filename or path to use // optional, the key used to encrypt the session file. SessionAESKey: "12345678901234567890123456789012"})
Your Telegram application ID obtained from my.telegram.org
Show AppHash
Your Telegram application hash obtained from my.telegram.org
Show SessionName
Custom name for the session. Used as a prefix for session files. Default is empty.
Show Session
Custom session filename or path. Default is “session.dat”
Show SessionAESKey
32-character key used to encrypt the session file. If not provided, session is stored unencrypted.
Show StringSession
Base64-encoded session string for session import. Alternative to file-based sessions.
session.dat is the default session file name
If you use more than one account or bot in the same program or directory, make sure their Session or SessionName fields are unique to prevent authentication data from being overwritten.
For advanced use cases, you can implement custom session storage using MongoDB, Redis, or any other database.The session is represented by the Session struct:
Copy
type Session struct { Key []byte // AUTH_KEY Hash []byte // AUTH_KEY_HASH (SHA1 of AUTH_KEY) Salt int64 // SERVER_SALT Hostname string // HOSTNAME (IP address of the DC) AppID int32 // APP_ID}
// Export as encoded string (base64)sessionString := client.ExportSession()// Store in databasedatabase.Save(userID, sessionString)// Later, retrieve and importsessionString := database.Load(userID)client.ImportSession([]byte(sessionString))
Custom session handlers are perfect for multi-tenant applications or when you need centralized session management across multiple instances.
If your account has 2FA enabled, provide the password:
Copy
// The library will prompt for the password if neededclient.Login("+1234567890")// Or handle it manuallyclient.Login("+1234567890", &tg.LoginOptions{ Password: "your_2fa_password",})