Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/mtkruto/client/3_client.ts>Client

[WIP] Cross-runtime JavaScript library for building Telegram clients
Go to Latest
class Client
import { Client } from "https://deno.land/x/mtkruto@0.0.989/client/3_client.ts";

Constructors

new
Client(
storage?: Storage,
apiId?: number | null,
apiHash?: string | null,
params?: ClientParams,
)

Constructs the client.

Properties

private
auth: { key: Uint8Array; id: bigint; } | null
private
readonly
autoStart: boolean
private
autoStarted: boolean
private
connectionInited: boolean
private
connectMutex: Mutex
private
handleUpdateQueue: Queue
private
lastMsgId: bigint
private
lastPropagatedConnectionState: ConnectionState | null
private
processUpdatesQueue: Queue
private
promises: Map<bigint, { resolve: (obj: ReadObject) => void; reject: (err: ReadObject) => void; }>
private
readonly
optional
publicKeys: PublicKeys
private
sessionId
private
state: { salt: bigint; seqNo: number; }
private
storageInited: boolean
private
toAcknowledge: Set<bigint>
private
optional
updateState: types.UpdatesState
protected
stateChangeHandler
readonly
appVersion: string
readonly
deviceModel: string
handler: Handler
readonly
langCode: string
readonly
langPack: string
readonly
parseMode: ParseMode
readonly
systemLangCode: string
readonly
systemVersion: string

Methods

private
checkChannelGap(
channelId: bigint,
pts: number,
ptsCount: number,
assertNoGap: boolean,
)
private
checkGap(
pts: number,
ptsCount: number,
assertNoGap: boolean,
)
private
downloadInner(location: types.TypeInputFileLocation, dcId?: number)
private
fetchState(source: string)
private
getLocalState()
private
handleUpdate(update: types.TypeUpdate)
private
pingLoop()
private
processChats(chats: types.TypeChat[])
private
processUpdates(updates_: types.TypeUpdate | types.TypeUpdates, assertNoGap?)
private
processUsers(users: types.TypeUser[])
private
propagateConnectionState(connectionState: ConnectionState)
private
receiveLoop()
private
recoverChannelUpdateGap(channelId: bigint, source: string)
private
recoverUpdateGap(source: string)
private
setAuth(key: Uint8Array)
private
setUpdateStateDate(date: number)
private
updatesToMessages(chatId: ChatID, updates: types.TypeUpdates)
authorize(params?: string | types.AuthExportedAuthorization | AuthorizeUserParams)

Calls initConnection and authorizes the client with one of the following:

  • Bot token (string)
  • Exported authorization (types.AuthExportedAuthorization)
  • User authorization handlers (AuthorizeUserParams)

if the current auth key doesn't throw AUTH_KEY_UNREGISTERED when calling updates.getState.

Notes:

  1. Requires the apiId and apiHash paramters to be passed when constructing the client.
  2. Reconnects the client to the appropriate DC in case of MIGRATE_X errors.
  3. The parameters passed to the initConnection call can be configured with the last parameter of the constructor.

Loads the session if setDc was not called, initializes and connnects a ClientPlain to generate auth key if there was none, and connects the client. Before establishing the connection, the session is saved.

download(fileId: string)

Download a file.

editMessageText(
chatId: ChatID,
messageId: number,
text: string,
)

Edit a message's text.

forwardMessage(
from: ChatID,
to: ChatID,
messageId: number,
)

Forward a single message.

forwardMessages(
from: ChatID,
to: ChatID,
messageIds: number[],
)

Forward multiple messages.

Get information on the currently authorized user.

getMessage(chatId: ChatID, messageId: number): Promise<Omit<Message, "replyToMessage"> | null>

Retrieve a single message.

getMessages(chatId_: ChatID, messageIds: number[])

Retrieve multiple messages.

invoke<T extends (functions.Function<unknown> | types.Type) = functions.Function<unknown>>(function_: T): Promise<T extends functions.Function<unknown> ? T["__R"] : void>

Invokes a function waiting and returning its reply if the second parameter is not true. Requires the client to be connected.

invoke<T extends (functions.Function<unknown> | types.Type) = functions.Function<unknown>>(function_: T, noWait: true): Promise<void>
on<U extends keyof Update, K extends keyof Update[U]>(filter: Update[U] extends string ? U : Update[U] extends Array<any> ? U : U | [U, ...K[]], handler: Handler<Pick<[P in U]: With<Update[U], K>, U>>)
send<T extends (functions.Function<unknown> | types.Type) = functions.Function<unknown>>(function_: T)

Alias for invoke with its second parameter being true.

sendMessage(
chatId: ChatID,
text: string,
)

Send a text message.

setDc(dc: DC)

Sets the DC and resets the auth key stored in the session provider if the stored DC was not the same as the dc parameter.

start(params?: string | types.AuthExportedAuthorization | AuthorizeUserParams)

Same as calling .connect() followed by .authorize(params).

use(middleware: Handler)
[getEntity](peer: types.PeerUser): Promise<types.User | null>
[getEntity](peer: types.PeerChat): Promise<types.Chat | null>
[getEntity](peer: types.PeerChannel): Promise<types.Channel | null>
[getStickerSetName](inputStickerSet: types.InputStickerSetID, hash?)
[handleMigrationError](err: types.RPCError)