Documentation
¶
Overview ¶
Package mtp talks to Telegram as a user account over MTProto.
Index ¶
- Constants
- func Classify(method string) string
- func CodeHint(via, next string, timeout int) string
- func Describe(name string) (map[string]any, error)
- func FilterNames() []string
- func Home(getenv func(string) string) (string, error)
- func Layer() string
- func MethodNames(prefix string) []string
- func ParseParams(raw []byte) (map[string]any, error)
- func ParseSessionBundle(s string) (apiID int, data []byte, err error)
- func PlaceholderResolver(kind, ref string) (map[string]any, error)
- func PublicID(typ string, id int64) int64
- type Config
- type Conn
- func (c *Conn) Delete(ctx context.Context, p PeerEntry, ids []int, revoke bool) error
- func (c *Conn) Dialogs(ctx context.Context, o DialogsOpts) ([]Dialog, error)
- func (c *Conn) Download(ctx context.Context, p PeerEntry, id int, out string) (string, error)
- func (c *Conn) Edit(ctx context.Context, p PeerEntry, id int, text, parse string) (*Sent, error)
- func (c *Conn) Forward(ctx context.Context, from, to PeerEntry, ids []int) error
- func (c *Conn) History(ctx context.Context, p PeerEntry, o HistoryOpts) (*History, error)
- func (c *Conn) Inbox(ctx context.Context, o InboxOpts) (*Inbox, error)
- func (c *Conn) Info(ctx context.Context, p PeerEntry) (map[string]any, error)
- func (c *Conn) Join(ctx context.Context, ref string) (*Who, error)
- func (c *Conn) Leave(ctx context.Context, p PeerEntry) error
- func (c *Conn) MarkRead(ctx context.Context, p PeerEntry, maxID int) error
- func (c *Conn) Poll(ctx context.Context, o PollOpts) (*PollResult, error)
- func (c *Conn) Raw(ctx context.Context, method string, params map[string]any) (any, error)
- func (c *Conn) Resolve(ctx context.Context, ref string) (PeerEntry, error)
- func (c *Conn) Search(ctx context.Context, o SearchOpts) (*SearchResult, error)
- func (c *Conn) Send(ctx context.Context, p PeerEntry, o SendOpts) (*Sent, error)
- func (c *Conn) Status(ctx context.Context) (map[string]any, error)
- type Dialog
- type DialogsOpts
- type Error
- type History
- type HistoryOpts
- type Inbox
- type InboxChat
- type InboxOpts
- type Kind
- type LoginOptions
- type LoginResult
- type Media
- type Message
- type Options
- type PeerEntry
- type Peers
- type PollOpts
- type PollResult
- type Profile
- func (p *Profile) CanWrite() bool
- func (p *Profile) ConfigPath() string
- func (p *Profile) Exists() bool
- func (p *Profile) ExportSession() (string, error)
- func (p *Profile) Load(getenv func(string) string) error
- func (p *Profile) LockPath() string
- func (p *Profile) Login(ctx context.Context, o LoginOptions) (*LoginResult, error)
- func (p *Profile) LoginState() string
- func (p *Profile) Logout(ctx context.Context, o Options, local bool) (revoked bool, err error)
- func (p *Profile) Open(ctx context.Context, o Options, fn func(ctx context.Context, c *Conn) error) error
- func (p *Profile) PeersPath() string
- func (p *Profile) RequireWrite(what string) error
- func (p *Profile) ResetLogin(ctx context.Context, lockTimeout time.Duration) error
- func (p *Profile) Save() error
- func (p *Profile) SessionPath() string
- func (p *Profile) Watch(ctx context.Context, o Options, w WatchOpts, emit func(Message) error) error
- type RawPlan
- type SearchOpts
- type SearchResult
- type SendOpts
- type Sent
- type TLDecl
- type TLField
- type WatchOpts
- type Who
Constants ¶
const ( AccessRead = "read" AccessWrite = "write" )
const ( ClassRead = "read" ClassWrite = "write" ClassBlocked = "blocked" )
const ( TransportAuto = "auto" TransportTCP = "tcp" TransportWebsocket = "websocket" )
Variables ¶
This section is empty.
Functions ¶
func Classify ¶
Classify decides whether a method only reads, changes the account, or is never allowed.
func Describe ¶
Describe returns a compact description of a method or a TL type. Field types end in "!" when required and "?" when optional.
func MethodNames ¶
MethodNames lists MTProto methods, optionally filtered by prefix.
func ParseParams ¶
ParseParams reads a JSON object, keeping numbers exact.
func ParseSessionBundle ¶
ParseSessionBundle unpacks the output of ExportSession.
func PlaceholderResolver ¶
PlaceholderResolver stands in for real lookups when only validating (--dry-run).
Types ¶
type Config ¶
type Config struct {
APIID int `json:"api_id"`
APIHash string `json:"api_hash"`
Access string `json:"access"`
Transport string `json:"transport,omitempty"` // set to websocket once the direct protocol proved blocked
UserID int64 `json:"user_id,omitempty"`
Username string `json:"username,omitempty"`
Name string `json:"name,omitempty"`
CreatedAt time.Time `json:"created_at"`
}
Config is stored next to the session. It holds no Telegram login secrets, but api_hash should still be kept private.
type Conn ¶
type Conn struct {
API *tg.Client
Self *tg.User
Profile *Profile
Peers *Peers
Client *telegram.Client
}
Conn is a connected, authorized account.
func (*Conn) Download ¶
Download saves the media of a message and returns the file path. out may be a directory or a file path; empty means the current directory.
func (*Conn) Inbox ¶
Inbox returns unread messages using Telegram's own read markers, so it needs no local state: whatever the user has not read in any client shows up here.
func (*Conn) Poll ¶
Poll returns messages that arrived since the previous poll. Cursors (the last seen message id per chat) live in the profile directory, so no daemon is needed: call it from a cron job or an agent loop.
func (*Conn) Raw ¶
Raw runs any MTProto method. Reads are always allowed; writes need an access-write profile; a small set of dangerous methods is never allowed.
func (*Conn) Resolve ¶
Resolve turns a user-supplied reference into an entry.
Accepted: me, @username, username, t.me/username, t.me/c/<channel>/<msg>, a numeric id as printed by this tool, +phone, or a chat title.
func (*Conn) Search ¶
func (c *Conn) Search(ctx context.Context, o SearchOpts) (*SearchResult, error)
type Dialog ¶
type Dialog struct {
ID int64 `json:"id"`
Type string `json:"type"`
Title string `json:"title"`
Username string `json:"username,omitempty"`
Unread int `json:"unread,omitempty"`
UnreadMentions int `json:"unread_mentions,omitempty"`
Muted bool `json:"muted,omitempty"`
Pinned bool `json:"pinned,omitempty"`
Archived bool `json:"archived,omitempty"`
LastMessage *Message `json:"last_message,omitempty"`
// contains filtered or unexported fields
}
Dialog is one chat in the chat list.
type HistoryOpts ¶
type LoginOptions ¶
type LoginOptions struct {
Options
APIID int
APIHash string
Phone string
QR bool
TelethonSession string
TgbotSession string // bundle made by `tgbot user session export`
Code string // non-interactive step 2
Resend bool // ask Telegram to deliver the pending code another way (SMS or call)
Password string // 2FA password, only for this call
Prompt func(label string, secret bool) (string, error)
ShowQR func(url string, expires time.Time)
}
LoginOptions drive an interactive login. The CLI supplies the prompts, so this package never touches the terminal.
type LoginResult ¶
type Message ¶
type Message struct {
ID int `json:"id"`
Date string `json:"date"`
Chat *Who `json:"chat,omitempty"`
From *Who `json:"from,omitempty"`
Text string `json:"text,omitempty"`
Out bool `json:"out,omitempty"`
ReplyTo int `json:"reply_to,omitempty"`
FwdFrom string `json:"forwarded_from,omitempty"`
Media *Media `json:"media,omitempty"`
Links []string `json:"links,omitempty"`
Edited string `json:"edited,omitempty"`
Views int `json:"views,omitempty"`
Pinned bool `json:"pinned,omitempty"`
Service string `json:"service,omitempty"`
AlbumID int64 `json:"album_id,omitempty"`
}
Message is the compact view of a message that agents see.
type Options ¶
type Options struct {
Version string
Stderr io.Writer
MaxFloodWait time.Duration // longest FLOOD_WAIT that is waited out instead of reported
LockTimeout time.Duration // how long to queue behind another tgbot process
Transport string // auto (default), tcp or websocket
Proxy string // socks5://host:port, or http://host:port with websocket
ConnectTimeout time.Duration // how long one transport may take to connect
}
Options tune one connected run.
type PeerEntry ¶
type PeerEntry struct {
Type string `json:"type"` // user, chat, channel
ID int64 `json:"id"`
AccessHash int64 `json:"access_hash,omitempty"`
Title string `json:"title,omitempty"`
Username string `json:"username,omitempty"`
Phone string `json:"phone,omitempty"`
}
PeerEntry is what the cache remembers about a user, group or channel.
func (PeerEntry) Input ¶
func (e PeerEntry) Input() tg.InputPeerClass
func (PeerEntry) InputChannel ¶
func (e PeerEntry) InputChannel() tg.InputChannelClass
func (PeerEntry) InputUser ¶
func (e PeerEntry) InputUser() tg.InputUserClass
type Peers ¶
type Peers struct {
// contains filtered or unexported fields
}
Peers caches access hashes. Telegram only accepts a numeric user or channel id together with the access hash it handed out earlier, so ids from a previous run are resolvable only if we kept them.
type PollResult ¶
type Profile ¶
Profile is one logged-in account: a directory with the session, config and peer cache.
func NewProfile ¶
NewProfile points at a profile directory without requiring it to exist.
func (*Profile) ConfigPath ¶
func (*Profile) ExportSession ¶
ExportSession packs the stored session so it can be moved to another machine. Whoever holds the string has full access to the account.
func (*Profile) Load ¶
Load reads the config. It fails with KindAuth when the profile has never logged in.
func (*Profile) Login ¶
func (p *Profile) Login(ctx context.Context, o LoginOptions) (*LoginResult, error)
Login signs in and stores the session. If the profile already has a working session it does nothing: a new login is never needed while the key is valid.
func (*Profile) LoginState ¶
LoginState is "awaiting_2fa" while a QR scan was accepted but the password is missing, else "".
func (*Profile) Logout ¶
Logout terminates the session on Telegram's side (unless local is set) and deletes the local files.
func (*Profile) Open ¶
func (p *Profile) Open(ctx context.Context, o Options, fn func(ctx context.Context, c *Conn) error) error
Open connects with the stored session and runs fn. It fails with KindAuth when the session is missing or no longer valid.
func (*Profile) RequireWrite ¶
RequireWrite is the single gate every state-changing command passes through.
func (*Profile) ResetLogin ¶
ResetLogin throws away an unfinished login (a pending code, or a QR scan that still waits for the 2FA password) so the next login starts clean. A finished session is never touched.
func (*Profile) SessionPath ¶
type RawPlan ¶
type RawPlan struct {
Method string
Class string
Request *tlRequest
Result string
Params map[string]any
}
RawPlan is a validated request that has not been sent.
type SearchOpts ¶
type SearchResult ¶
type TLField ¶
type TLField struct {
Name string
Type string // type without the flags.N? prefix, e.g. "InputPeer", "Vector<int>"
Optional bool // only sent when the flag bit is set
Flags bool // this field is a flags word (flags:# or flags2:#)
FlagName string // for optional fields: which flags word holds the bit
Bit int
}
TLField is one parameter of a TL constructor or method.