Documentation
¶
Overview ¶
Package tgconv converts Telegram session strings between library formats.
A session string encapsulates the data needed to connect to Telegram without re-authenticating: the data center ID, server address, port, and the 256-byte authorization key (plus optional fields like user ID and API ID).
Supported formats:
- Telethon
- Pyrogram
- GramJS
- mtcute
- MTKruto
- gogram
- gotgproto (gotd/td)
SQLite session files from Telethon and Pyrogram are also supported via ReadSQLite.
Index ¶
- Variables
- func Convert(str string, to Format) (string, error)
- func ConvertFrom(str string, from, to Format) (string, error)
- func Decode(str string) (*Session, Format, error)
- func Encode(s *Session, format Format) (string, error)
- func EncodeGogram(s *Session) (string, error)
- func EncodeGotgproto(s *Session) (string, error)
- func EncodeGramJS(s *Session) (string, error)
- func EncodeMTKruto(s *Session) (string, error)
- func EncodeMtcute(s *Session) (string, error)
- func EncodePyrogram(s *Session) (string, error)
- func EncodeTelethon(s *Session) (string, error)
- type Format
- type Session
- func DecodeFormat(str string, format Format) (*Session, error)
- func DecodeGogram(str string) (*Session, error)
- func DecodeGotgproto(str string) (*Session, error)
- func DecodeGramJS(str string) (*Session, error)
- func DecodeMTKruto(str string) (*Session, error)
- func DecodeMtcute(str string) (*Session, error)
- func DecodePyrogram(str string) (*Session, error)
- func DecodeTelethon(str string) (*Session, error)
Constants ¶
This section is empty.
Variables ¶
var AllFormats = []Format{ FormatTelethon, FormatPyrogram, FormatGramJS, FormatMtcute, FormatMTKruto, FormatGogram, FormatGotgproto, }
AllFormats lists every supported format.
Functions ¶
func ConvertFrom ¶
ConvertFrom decodes from a known format and re-encodes.
func EncodeGogram ¶
EncodeGogram encodes a session in gogram string format.
Modern: "1BvE" + base64RawURL(json({key, hash, dc_id, ip_addr, app_id}))
func EncodeGotgproto ¶
EncodeGotgproto encodes a session in gotgproto string format.
The format is base64std(json(gotgprotoSession)), where gotgprotoSession wraps gotd's session data:
{
"Version": 1,
"Data": base64(json({Version: 1, Data: {DC, Addr, AuthKey, AuthKeyID, Salt, Config}}))
}
func EncodeGramJS ¶
EncodeGramJS encodes a session in GramJS string format.
"1" + base64std(dc[1] + addr_len[2 BE] + addr_bytes[variable] + port[2 BE] + authkey[256])
func EncodeMTKruto ¶
EncodeMTKruto encodes a session in MTKruto string format.
base64url(rleEncode(TL_string(dc) + TL_bytes(authkey) + int32(apiId) LE + byte(isBot) + int64(userId) LE))
The dc string is formatted as "<dcID>" or "<dcID>-test" for test mode.
func EncodeMtcute ¶
EncodeMtcute encodes a session in mtcute string format.
base64url(version[1]=3 + flags[4 LE] + dc_option(TL) + [media_dc(TL)] + user_id[8 LE] + is_bot(TL bool) + authkey(TL bytes))
func EncodePyrogram ¶
EncodePyrogram encodes a session in Pyrogram string format.
base64url(dc[1] + api_id[4 BE] + test_mode[1] + authkey[256] + user_id[8 BE] + is_bot[1]) No version prefix. Total payload: 271 bytes.
func EncodeTelethon ¶
EncodeTelethon encodes a session in Telethon string format.
Telethon's StringSession has a single version (prefix "1") and stores only what's needed to connect — it never serializes api_id:
"1" + base64url(dc[1] + ip[4|16] + port[2 BE] + authkey[256])
Mirrors Telethon v1 sessions/string.py (struct ">B{}sH256s", CURRENT_VERSION = "1"). AppID is dropped from the wire format on purpose.
Types ¶
type Format ¶
type Format string
Format identifies a session string encoding format.
func DetectFormat ¶
DetectFormat attempts to identify the format without fully decoding.
type Session ¶
type Session struct {
DCID int // data center ID (1-5)
ServerAddress string // IP address or hostname
Port int // server port
AuthKey []byte // 256-byte MTProto authorization key
AppID int32 // API ID from my.telegram.org
TestMode bool // connected to test servers
UserID int64 // authenticated user/bot ID
IsBot bool // whether the account is a bot
}
Session holds the common fields extracted from any session format. Not every field is populated by every format — the auth key and DC ID are always present; the rest depend on the source.
func DecodeFormat ¶
DecodeFormat decodes a session string in the specified format.
func DecodeGogram ¶
DecodeGogram decodes a gogram session string (modern and legacy).
func DecodeGotgproto ¶
DecodeGotgproto decodes a gotgproto session string.
Handles both the current format (Version + Data wrapping gotd JSON) and older formats where the JSON directly contains DCID, AuthKey, etc.
func DecodeGramJS ¶
DecodeGramJS decodes a GramJS session string.
func DecodeMTKruto ¶
DecodeMTKruto decodes an MTKruto session string.
func DecodeMtcute ¶
DecodeMtcute decodes an mtcute session string.
func DecodePyrogram ¶
DecodePyrogram decodes a Pyrogram session string.
Format: base64url(dc[1] + api_id[4 BE] + test_mode[1] + authkey[256] + user_id[8 BE] + is_bot[1]) Total payload: 271 bytes. No version prefix, no legacy variants.
func DecodeTelethon ¶
DecodeTelethon decodes a Telethon session string. Telethon has a single version (prefix "1"), storing dc + ip + port + auth_key. No api_id.
func (*Session) FillDefaults ¶ added in v0.5.0
func (s *Session) FillDefaults()
FillDefaults populates ServerAddress and Port from DC defaults if they are zero. This is called after decoding formats that omit these fields, and should be called after constructing a Session manually (e.g., from SQLite).