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

x/mtkruto/client/4_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.1.0/client/4_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
handle
private
handleUpdateQueue: Queue
private
lastMsgId: bigint
private
lastPropagatedAuthorizationState: boolean | null
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
private
usernameResolver: UsernameResolver
protected
stateChangeHandler
readonly
appVersion: string
readonly
deviceModel: string
readonly
langCode: string
readonly
langPack: string
readonly
parseMode: ParseMode
readonly
systemLangCode: string
readonly
systemVersion: string

Methods

private
assertBot(source: string)
private
assertUser(source: string)
private
checkChannelGap(
channelId: bigint,
pts: number,
ptsCount: number,
assertNoGap: boolean,
)
private
checkGap(
pts: number,
ptsCount: number,
assertNoGap: boolean,
)
private
constructReplyMarkup(params?: Pick<SendMessagesParams, "replyMarkup">)
private
downloadInner(location: types.TypeInputFileLocation, dcId?: number)
private
fetchState(source: string)
private
getChannelAccessHash(channelId: bigint)
private
getLocalState()
private
getMessagesInner(chatId_: ChatID, messageIds: number[])
private
getUserAccessHash(userId: bigint)
private
handleUpdate(update: types.TypeUpdate)
private
parseText(text: string, params?: { parseMode?: ParseMode; entities?: MessageEntity[]; })
private
pingLoop()
private
processChats(chats: types.TypeChat[])
private
processUpdates(updates_: types.TypeUpdate | types.TypeUpdates, assertNoGap?)
private
processUsers(users: types.TypeUser[])
private
propagateAuthorizationState(authorized: boolean)
private
propagateConnectionState(connectionState: ConnectionState)
private
receiveLoop()
private
recoverChannelUpdateGap(channelId: bigint, source: string)
private
recoverUpdateGap(source: string)
private
resolveSendAs(params?: Pick<SendMessagesParams, "sendAs">)
private
setAuth(key: Uint8Array)
private
setUpdateStateDate(date: number)
private
updatesToMessages(chatId: ChatID, updates: types.TypeUpdates)

Answer a callback query. Bot-only.

answerInlineQuery(
id: string,
results: InlineQueryResult[],
params?: { cacheTime?: number; isPersonal?: boolean; nextOffset?: string; isGallery?: boolean; button: InlineQueryResultButton; },
)
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.
branch(
predicate: (upd: Update) => MaybePromise<boolean>,
trueHandler: Handler,
falseHandler: Handler,
)

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.

filter<D extends Update>(predicate: (update: Update) => update is D, handler: Handler<D>): void
filter(predicate: (update: Update) => MaybePromise<boolean>, handler: Handler): void
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.

getMyCommands(params?: { languageCode?: string; scope?: BotCommandScope; }): Promise<BotCommand[]>
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<T extends keyof Update, F extends keyof NonNullable<Update[T]>>(filter: T extends FilterableUpdates ? T | [T, F, ...F[]] : T, handler: Handler<FilterUpdate<Update, T, F>>)
send<T extends (functions.Function<unknown> | types.Type) = functions.Function<unknown>>(function_: T)

Alias for invoke with its second parameter being true.

sendChatAction(
chatId: ChatID,
action_: ChatAction,
messageThreadId?: number,
)

Send a chat action.

sendMessage(
chatId: ChatID,
text: string,
)

Send a text message.

sendPoll(
chatId: ChatID,
question: string,
options: [string, string, ...string[]],
params?: SendPollParams,
)

Send a poll.

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.

setMyCommands(commands: BotCommand[], params?: { languageCode?: string; scope?: BotCommandScope; })
start(params?: string | types.AuthExportedAuthorization | AuthorizeUserParams)

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

use(handler: 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>
[getMessageWithReply](chatId: ChatID, messageId: number): Promise<Message | null>
[getStickerSetName](inputStickerSet: types.InputStickerSetID, hash?)

Static Methods

private
assertMsgHas<K extends keyof Message>(message: Message, key: K): With<Message, K>