telegram

package
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 9, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyTdlibParameters

func ApplyTdlibParameters(tdjson *TDJSON, clientID int32, tdlibBaseDirectory string, apiID int32, apiHash string) error

func BuildIdentityLabelFromUser

func BuildIdentityLabelFromUser(user User) string

func ConfigureLogging

func ConfigureLogging(tdjson *TDJSON)

func CreatePrivateChat

func CreatePrivateChat(tdjson *TDJSON, clientID int32, userID int64) (int64, error)

func DownloadFile

func DownloadFile(tdjson *TDJSON, clientID int32, fileID int32, timeout time.Duration) (string, error)

DownloadFile downloads a file by id and blocks until it completes, returning the local path within TDLib's file cache. Uses a synchronous downloadFile so the reply only arrives once the download has finished.

func EnsureTdlibDirectories

func EnsureTdlibDirectories(tdlibBaseDirectory string) (string, string, error)

func FetchChatIdentifiers

func FetchChatIdentifiers(tdjson *TDJSON, clientID int32, limit int) ([]int64, error)

func FetchChatTitle

func FetchChatTitle(tdjson *TDJSON, clientID int32, chatID int64) (string, error)

func FetchUserDisplayName

func FetchUserDisplayName(tdjson *TDJSON, clientID int32, userID int64) (string, error)

func FetchUserHandle

func FetchUserHandle(tdjson *TDJSON, clientID int32, userID int64) (string, error)

FetchUserHandle returns a user's "@username" (the stable handle the agent tier keys allow-list, claims, and contacts on), or "" when the user has no public username. It tolerates both the legacy `username` field and the newer `usernames.active_usernames` shape.

func LogOutSession

func LogOutSession(tdjson *TDJSON, clientID int32) error

func MarkMessagesRead

func MarkMessagesRead(tdjson *TDJSON, clientID int32, chatID int64, messageIDs []int64) error

MarkMessagesRead marks the given messages read (and sends read receipts), so they won't be triaged again.

func OpenChat

func OpenChat(tdjson *TDJSON, clientID int32, chatID int64) error

func PersistIdentity

func PersistIdentity(configuration config.Config, configPath string, user User) (config.Config, error)

func ProvideAuthenticationPhoneNumber

func ProvideAuthenticationPhoneNumber(tdjson *TDJSON, clientID int32, phoneNumber string) error

func ReceiveUpdates

func ReceiveUpdates(tdjson *TDJSON) (string, error)

func RequireAuthenticatedSession

func RequireAuthenticatedSession() error

func ResolveChatIdentifierByUsername

func ResolveChatIdentifierByUsername(tdjson *TDJSON, clientID int32, username string) (int64, error)

func ResolveIdentityLabel

func ResolveIdentityLabel(configuration config.Config, configPath string) (string, config.Config, error)

func ResolveUserIdentifierByUsername

func ResolveUserIdentifierByUsername(tdjson *TDJSON, clientID int32, username string) (int64, error)

func SendLocalFileMessage

func SendLocalFileMessage(tdjson *TDJSON, clientID int32, chatID int64, path, caption string) (int64, string, error)

SendLocalFileMessage sends a local file to a chat as the appropriate media type with an optional caption. It returns the temporary message id (the upload runs asynchronously — use WaitForSendCompletion to block until done) and a human-readable label for the media kind.

func SendRequestAndWait

func SendRequestAndWait(tdjson *TDJSON, clientID int32, requestJSON string, extraTag string, timeout time.Duration) (string, error)

SendRequestAndWait sends a request and blocks until the matching reply (by @extra) arrives, the timeout elapses, or the client is closed. The extraTag is suffixed with a per-client sequence number so concurrent callers reusing the same logical tag (e.g. "get-user") never collide on the pending map.

func SendTextMessage

func SendTextMessage(tdjson *TDJSON, clientID int32, chatID int64, text string) (int64, error)

func SubmitAuthenticationCode

func SubmitAuthenticationCode(tdjson *TDJSON, clientID int32, code string) error

func SubmitAuthenticationPassword

func SubmitAuthenticationPassword(tdjson *TDJSON, clientID int32, password string) error

func WaitForSendCompletion

func WaitForSendCompletion(tdjson *TDJSON, clientID int32, temporaryMessageID int64, timeout time.Duration) error

WaitForSendCompletion blocks until the message with the given temporary id finishes uploading/sending (or fails). Needed for one-shot commands so the process doesn't exit and cancel the upload before it completes.

func WaitForUpdateType

func WaitForUpdateType(tdjson *TDJSON, clientID int32, wantedType string, timeout time.Duration) (string, error)

Types

type ApplyTdlibParametersRequest

type ApplyTdlibParametersRequest struct {
	Type                   string `json:"@type"`
	DatabaseDirectory      string `json:"database_directory"`
	FilesDirectory         string `json:"files_directory"`
	UseFileDatabase        bool   `json:"use_file_database"`
	UseChatInfoDatabase    bool   `json:"use_chat_info_database"`
	UseMessageDatabase     bool   `json:"use_message_database"`
	UseSecretChats         bool   `json:"use_secret_chats"`
	ApiID                  int32  `json:"api_id"`
	ApiHash                string `json:"api_hash"`
	SystemLanguageCode     string `json:"system_language_code"`
	DeviceModel            string `json:"device_model"`
	SystemVersion          string `json:"system_version"`
	ApplicationVersion     string `json:"application_version"`
	EnableStorageOptimizer bool   `json:"enable_storage_optimizer"`
}

type AuthorizationState

type AuthorizationState string
const (
	AuthStateUnknown             AuthorizationState = "unknown"
	AuthStateWaitTdlibParameters AuthorizationState = "authorizationStateWaitTdlibParameters"
	AuthStateWaitPhoneNumber     AuthorizationState = "authorizationStateWaitPhoneNumber"
	AuthStateWaitCode            AuthorizationState = "authorizationStateWaitCode"
	AuthStateWaitPassword        AuthorizationState = "authorizationStateWaitPassword"
	AuthStateReady               AuthorizationState = "authorizationStateReady"
	AuthStateLoggingOut          AuthorizationState = "authorizationStateLoggingOut"
	AuthStateClosing             AuthorizationState = "authorizationStateClosing"
	AuthStateClosed              AuthorizationState = "authorizationStateClosed"
)

func FetchAuthorizationState

func FetchAuthorizationState(tdjson *TDJSON, clientID int32) (AuthorizationState, error)

func ParseUpdateAuthorizationState

func ParseUpdateAuthorizationState(updateJSON string) (AuthorizationState, bool)

ParseUpdateAuthorizationState returns the new authorization state from an updateAuthorizationState event (false for any other update), so a long-running client can react when the session is logged out or closed remotely.

type ChatHistory

type ChatHistory struct {
	Messages []Message `json:"messages"`
}

type ChatInfo

type ChatInfo struct {
	ID                     int64
	Title                  string
	TypeName               string // "private" | "group" | "other"
	UserID                 int64  // for private chats: the other user
	UnreadCount            int
	LastReadInboxMessageID int64
}

ChatInfo carries the unread state we need to triage a chat.

func FetchChatInfo

func FetchChatInfo(tdjson *TDJSON, clientID int32, chatID int64) (ChatInfo, error)

FetchChatInfo returns a chat's unread state (from the local cache populated by getChats — fast, no network).

type Content

type Content struct {
	Type string `json:"@type"`
	Text struct {
		Text string `json:"text"`
	} `json:"text"`
	Caption struct {
		Text string `json:"text"`
	} `json:"caption"`

	Photo *struct {
		Sizes []PhotoSize `json:"sizes"`
	} `json:"photo"`
	Video *struct {
		FileName string `json:"file_name"`
		Video    File   `json:"video"`
	} `json:"video"`
	Audio *struct {
		FileName string `json:"file_name"`
		Audio    File   `json:"audio"`
	} `json:"audio"`
	Document *struct {
		FileName string `json:"file_name"`
		Document File   `json:"document"`
	} `json:"document"`
	Animation *struct {
		FileName  string `json:"file_name"`
		Animation File   `json:"animation"`
	} `json:"animation"`
	VoiceNote *struct {
		Voice File `json:"voice"`
	} `json:"voice_note"`
	VideoNote *struct {
		Video File `json:"video"`
	} `json:"video_note"`
	Sticker *struct {
		Sticker File `json:"sticker"`
	} `json:"sticker"`
}

func (Content) CaptionOrText

func (c Content) CaptionOrText() string

CaptionOrText returns the caption (for media) or text (for text messages).

func (Content) MediaFile

func (c Content) MediaFile() (file File, fileName string, label string, ok bool)

MediaFile returns the best downloadable file for a media message, a suggested filename, and a short human-readable label ("photo", "video", ...). ok is false when the content carries no downloadable file (e.g. plain text).

type File

type File struct {
	ID    int32 `json:"id"`
	Size  int64 `json:"size"`
	Local struct {
		Path                   string `json:"path"`
		IsDownloadingCompleted bool   `json:"is_downloading_completed"`
	} `json:"local"`
	Remote struct {
		UniqueID string `json:"unique_id"`
	} `json:"remote"`
}

File mirrors the TDLib "file" object (only the fields we use).

type FormattedText

type FormattedText struct {
	Type     string `json:"@type"`
	Text     string `json:"text"`
	Entities []any  `json:"entities,omitempty"`
}

func FormatOutgoingText

func FormatOutgoingText(tdjson *TDJSON, clientID int32, text string) FormattedText

func FormatOutgoingTextBestEffort

func FormatOutgoingTextBestEffort(tdjson *TDJSON, clientID int32, text string) FormattedText

func ParseMarkdownText

func ParseMarkdownText(tdjson *TDJSON, clientID int32, text string, version int) (FormattedText, error)

func ParseMarkdownV1Text

func ParseMarkdownV1Text(tdjson *TDJSON, clientID int32, text string) (FormattedText, error)

func ParseMarkdownV2Text

func ParseMarkdownV2Text(tdjson *TDJSON, clientID int32, text string) (FormattedText, error)

func ParseOutgoingMarkdownText

func ParseOutgoingMarkdownText(tdjson *TDJSON, clientID int32, text string) (FormattedText, error)

func PlainFormattedText

func PlainFormattedText(text string) FormattedText

type Message

type Message struct {
	ID         int64    `json:"id"`
	ChatID     int64    `json:"chat_id"`
	Date       int64    `json:"date"`
	IsOutgoing bool     `json:"is_outgoing"`
	SenderID   SenderID `json:"sender_id"`
	Content    Content  `json:"content"`
}

func FetchChatHistory

func FetchChatHistory(tdjson *TDJSON, clientID int32, chatID int64, limit int) ([]Message, error)

func FetchChatHistorySnapshot

func FetchChatHistorySnapshot(tdjson *TDJSON, clientID int32, chatID int64, limit int) ([]Message, error)

func FetchUnreadIncoming

func FetchUnreadIncoming(tdjson *TDJSON, clientID int32, chatID, lastReadInboxMessageID int64) ([]Message, error)

FetchUnreadIncoming returns the unread incoming messages in a chat (those with id > lastReadInboxMessageID), paginating back past any newer outgoing messages (e.g. an auto-reply) so they aren't missed. Newest first.

type NewMessageUpdate

type NewMessageUpdate struct {
	Type    string `json:"@type"`
	Message struct {
		ID      int64 `json:"id"`
		ChatID  int64 `json:"chat_id"`
		Content struct {
			Type string `json:"@type"`
			Text struct {
				Text string `json:"text"`
			} `json:"text"`
		} `json:"content"`
	} `json:"message"`
}

func ParseNewMessage

func ParseNewMessage(updateJSON string) (*NewMessageUpdate, bool)

type PhotoSize

type PhotoSize struct {
	Type   string `json:"type"`
	Photo  File   `json:"photo"`
	Width  int    `json:"width"`
	Height int    `json:"height"`
}

PhotoSize mirrors the TDLib "photoSize" object.

type SenderID

type SenderID struct {
	Type   string `json:"@type"`
	UserID int64  `json:"user_id,omitempty"`
	ChatID int64  `json:"chat_id,omitempty"`
}

type TDJSON

type TDJSON struct{}

func LoadTDJSON

func LoadTDJSON() (*TDJSON, error)

func (*TDJSON) Close

func (t *TDJSON) Close() error

func (*TDJSON) CreateClientID

func (t *TDJSON) CreateClientID() int32

func (*TDJSON) Execute

func (t *TDJSON) Execute(requestJSON string) (string, error)

func (*TDJSON) Receive

func (t *TDJSON) Receive(timeoutSeconds float64) (string, error)

func (*TDJSON) Send

func (t *TDJSON) Send(clientID int32, requestJSON string) error

type UpdateNewMessage

type UpdateNewMessage struct {
	Type    string  `json:"@type"`
	Message Message `json:"message"`
}

func ParseUpdateNewMessage

func ParseUpdateNewMessage(updateJSON string) (*UpdateNewMessage, bool)

type User

type User struct {
	ID          int64  `json:"id"`
	FirstName   string `json:"first_name"`
	LastName    string `json:"last_name"`
	Username    string `json:"username"`
	PhoneNumber string `json:"phone_number"`
}

func FetchCurrentUser

func FetchCurrentUser(tdjson *TDJSON, clientID int32) (User, error)

func FetchUser

func FetchUser(tdjson *TDJSON, clientID int32, userID int64) (User, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL