feat(client): support takeout session
This commit is contained in:
parent
77ab185493
commit
64d860cc4c
6 changed files with 129 additions and 1 deletions
|
@ -68,6 +68,7 @@ import { sendMedia } from './methods/messages/send-media'
|
||||||
import { sendPhoto } from './methods/messages/send-photo'
|
import { sendPhoto } from './methods/messages/send-photo'
|
||||||
import { sendText } from './methods/messages/send-text'
|
import { sendText } from './methods/messages/send-text'
|
||||||
import { unpinMessage } from './methods/messages/unpin-message'
|
import { unpinMessage } from './methods/messages/unpin-message'
|
||||||
|
import { initTakeoutSession } from './methods/misc/init-takeout-session'
|
||||||
import {
|
import {
|
||||||
getParseMode,
|
getParseMode,
|
||||||
registerParseMode,
|
registerParseMode,
|
||||||
|
@ -104,6 +105,7 @@ import {
|
||||||
PartialExcept,
|
PartialExcept,
|
||||||
ReplyMarkup,
|
ReplyMarkup,
|
||||||
SentCode,
|
SentCode,
|
||||||
|
TakeoutSession,
|
||||||
TermsOfService,
|
TermsOfService,
|
||||||
UploadFileLike,
|
UploadFileLike,
|
||||||
UploadedFile,
|
UploadedFile,
|
||||||
|
@ -1031,6 +1033,7 @@ export interface TelegramClient extends BaseTelegramClient {
|
||||||
): Promise<Message>
|
): Promise<Message>
|
||||||
/**
|
/**
|
||||||
* Forward one or more messages, optionally including a caption message.
|
* Forward one or more messages, optionally including a caption message.
|
||||||
|
* You can forward no more than 100 messages at once.
|
||||||
*
|
*
|
||||||
* If a caption message was sent, it will be the first message in the resulting array.
|
* If a caption message was sent, it will be the first message in the resulting array.
|
||||||
*
|
*
|
||||||
|
@ -1630,6 +1633,14 @@ export interface TelegramClient extends BaseTelegramClient {
|
||||||
* @param messageId Message ID
|
* @param messageId Message ID
|
||||||
*/
|
*/
|
||||||
unpinMessage(chatId: InputPeerLike, messageId: number): Promise<void>
|
unpinMessage(chatId: InputPeerLike, messageId: number): Promise<void>
|
||||||
|
/**
|
||||||
|
* Create a new takeout session
|
||||||
|
*
|
||||||
|
* @param params Takeout session parameters
|
||||||
|
*/
|
||||||
|
initTakeoutSession(
|
||||||
|
params: Omit<tl.account.RawInitTakeoutSessionRequest, '_'>
|
||||||
|
): Promise<TakeoutSession>
|
||||||
/**
|
/**
|
||||||
* Register a given {@link IMessageEntityParser} as a parse mode
|
* Register a given {@link IMessageEntityParser} as a parse mode
|
||||||
* for messages. When this method is first called, given parse
|
* for messages. When this method is first called, given parse
|
||||||
|
@ -1830,6 +1841,7 @@ export class TelegramClient extends BaseTelegramClient {
|
||||||
sendPhoto = sendPhoto
|
sendPhoto = sendPhoto
|
||||||
sendText = sendText
|
sendText = sendText
|
||||||
unpinMessage = unpinMessage
|
unpinMessage = unpinMessage
|
||||||
|
initTakeoutSession = initTakeoutSession
|
||||||
registerParseMode = registerParseMode
|
registerParseMode = registerParseMode
|
||||||
unregisterParseMode = unregisterParseMode
|
unregisterParseMode = unregisterParseMode
|
||||||
getParseMode = getParseMode
|
getParseMode = getParseMode
|
||||||
|
|
|
@ -25,7 +25,8 @@ import {
|
||||||
FileDownloadParameters,
|
FileDownloadParameters,
|
||||||
Message,
|
Message,
|
||||||
ReplyMarkup,
|
ReplyMarkup,
|
||||||
InputMediaLike
|
InputMediaLike,
|
||||||
|
TakeoutSession
|
||||||
} from '../types'
|
} from '../types'
|
||||||
|
|
||||||
// @copy
|
// @copy
|
||||||
|
|
19
packages/client/src/methods/misc/init-takeout-session.ts
Normal file
19
packages/client/src/methods/misc/init-takeout-session.ts
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
import { TelegramClient } from '../../client'
|
||||||
|
import { tl } from '@mtcute/tl'
|
||||||
|
import { TakeoutSession } from '../../types'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new takeout session
|
||||||
|
*
|
||||||
|
* @param params Takeout session parameters
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
export async function initTakeoutSession(
|
||||||
|
this: TelegramClient,
|
||||||
|
params: Omit<tl.account.RawInitTakeoutSessionRequest, '_'>
|
||||||
|
): Promise<TakeoutSession> {
|
||||||
|
return new TakeoutSession(this, await this.call({
|
||||||
|
_: 'account.initTakeoutSession',
|
||||||
|
...params
|
||||||
|
}))
|
||||||
|
}
|
|
@ -4,6 +4,7 @@ export * from './files'
|
||||||
export * from './media'
|
export * from './media'
|
||||||
export * from './messages'
|
export * from './messages'
|
||||||
export * from './peers'
|
export * from './peers'
|
||||||
|
export * from './misc'
|
||||||
|
|
||||||
export * from './errors'
|
export * from './errors'
|
||||||
export { MaybeDynamic } from './utils'
|
export { MaybeDynamic } from './utils'
|
||||||
|
|
1
packages/client/src/types/misc/index.ts
Normal file
1
packages/client/src/types/misc/index.ts
Normal file
|
@ -0,0 +1 @@
|
||||||
|
export * from './takeout-session'
|
94
packages/client/src/types/misc/takeout-session.ts
Normal file
94
packages/client/src/types/misc/takeout-session.ts
Normal file
|
@ -0,0 +1,94 @@
|
||||||
|
import { tl } from 'packages/tl'
|
||||||
|
import { TelegramClient } from '../../client'
|
||||||
|
import { makeInspectable } from '../utils'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Account takeout session
|
||||||
|
*/
|
||||||
|
export class TakeoutSession {
|
||||||
|
private client: TelegramClient
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Takeout session id
|
||||||
|
*/
|
||||||
|
readonly id: tl.Long
|
||||||
|
|
||||||
|
constructor(client: TelegramClient, session: tl.account.RawTakeout) {
|
||||||
|
this.client = client
|
||||||
|
this.id = session.id
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make an API call using this takeout session
|
||||||
|
*
|
||||||
|
* This method just wraps the query into `invokeWithTakeout`
|
||||||
|
* and passes the control down to {@link TelegramClient.call}.
|
||||||
|
*
|
||||||
|
* @param message RPC method to call
|
||||||
|
* @param params Additional call parameters
|
||||||
|
*/
|
||||||
|
async call<T extends tl.RpcMethod>(
|
||||||
|
message: T,
|
||||||
|
params?: {
|
||||||
|
throwFlood: boolean
|
||||||
|
}
|
||||||
|
): Promise<tl.RpcCallReturn[T['_']]> {
|
||||||
|
return this.client.call(
|
||||||
|
{
|
||||||
|
_: 'invokeWithTakeout',
|
||||||
|
takeoutId: this.id,
|
||||||
|
query: message,
|
||||||
|
},
|
||||||
|
params
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a proxy over {@link TelegramClient}
|
||||||
|
* that will use this takeout session to call methods.
|
||||||
|
*
|
||||||
|
* You can optionally provide a function to check if some
|
||||||
|
* RPC method should be called via a takeout session or directly,
|
||||||
|
* otherwise all methods are called through the takeout session.
|
||||||
|
*
|
||||||
|
* > **Note**: This will return a `Proxy` object that
|
||||||
|
* > overrides `call` method. Using this method requires
|
||||||
|
* > that your target environment supports `Proxy` and `Reflect` APIs
|
||||||
|
*
|
||||||
|
* @param predicate
|
||||||
|
* Function that given the RPC call should determine whether
|
||||||
|
* that call should be called via takeout session or not.
|
||||||
|
* Returning `true` will use takeout session, `false` will not.
|
||||||
|
*/
|
||||||
|
createProxy(predicate?: (obj: tl.TlObject) => boolean): TelegramClient {
|
||||||
|
const boundCall: TakeoutSession['call'] = predicate
|
||||||
|
? (obj, params) => {
|
||||||
|
if (predicate(obj)) {
|
||||||
|
return this.call(obj, params)
|
||||||
|
}
|
||||||
|
return this.client.call(obj, params)
|
||||||
|
}
|
||||||
|
: this.call.bind(this)
|
||||||
|
|
||||||
|
return new Proxy(this.client, {
|
||||||
|
get(target, prop, receiver) {
|
||||||
|
if (prop === 'call') return boundCall
|
||||||
|
return Reflect.get(target, prop, receiver)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finish account takeout session
|
||||||
|
*
|
||||||
|
* @param success Whether the data was successfully exported
|
||||||
|
*/
|
||||||
|
async finish(success = true): Promise<void> {
|
||||||
|
await this.call({
|
||||||
|
_: 'account.finishTakeoutSession',
|
||||||
|
success,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
makeInspectable(TakeoutSession)
|
Loading…
Reference in a new issue