fix: error on extra fields in TelegramClient#call + related fixes

This commit is contained in:
alina 🌸 2023-07-24 00:12:17 +03:00
parent 6221a8716f
commit 6a2c5d90b7
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI
11 changed files with 55 additions and 50 deletions

View file

@ -3092,12 +3092,6 @@ export interface TelegramClient extends BaseTelegramClient {
*/ */
schedule?: Date | number schedule?: Date | number
/**
* For bots: inline or reply markup or an instruction
* to hide a reply keyboard or to force a reply.
*/
replyMarkup?: ReplyMarkup
/** /**
* Function that will be called after some part has been uploaded. * Function that will be called after some part has been uploaded.
* Only used when a file that requires uploading is passed, * Only used when a file that requires uploading is passed,
@ -3437,13 +3431,11 @@ export interface TelegramClient extends BaseTelegramClient {
* @param chatId Chat or user ID * @param chatId Chat or user ID
* @param messageId Identifier of the message to translate * @param messageId Identifier of the message to translate
* @param toLanguage Target language (two-letter ISO 639-1 language code) * @param toLanguage Target language (two-letter ISO 639-1 language code)
* @param fromLanguage Source language (two-letter ISO 639-1 language code, by default auto-detected)
*/ */
translateMessage( translateMessage(
chatId: InputPeerLike, chatId: InputPeerLike,
messageId: number, messageId: number,
toLanguage: string, toLanguage: string
fromLanguage?: string
): Promise<[string, MessageEntity[]] | null> ): Promise<[string, MessageEntity[]] | null>
/** /**
* Translate text to a given language. * Translate text to a given language.
@ -3454,13 +3446,8 @@ export interface TelegramClient extends BaseTelegramClient {
* *
* @param text Text to translate * @param text Text to translate
* @param toLanguage Target language (two-letter ISO 639-1 language code) * @param toLanguage Target language (two-letter ISO 639-1 language code)
* @param fromLanguage Source language (two-letter ISO 639-1 language code, by default auto-detected)
*/ */
translateText( translateText(text: string, toLanguage: string): Promise<string | null>
text: string,
toLanguage: string,
fromLanguage?: string
): Promise<string | null>
/** /**
* Unpin all pinned messages in a chat. * Unpin all pinned messages in a chat.
* *

View file

@ -49,7 +49,6 @@ export async function addChatMembers(
users as InputPeerLike[], users as InputPeerLike[],
normalizeToInputUser, normalizeToInputUser,
), ),
fwdLimit: forwardCount,
}) })
this._handleUpdate(updates) this._handleUpdate(updates)
} else throw new MtInvalidPeerTypeError(chatId, 'chat or channel') } else throw new MtInvalidPeerTypeError(chatId, 'chat or channel')

View file

@ -24,7 +24,6 @@ export async function hideAllJoinRequests(
_: 'messages.hideAllChatJoinRequests', _: 'messages.hideAllChatJoinRequests',
approved: action === 'approve', approved: action === 'approve',
peer: await this.resolvePeer(peer), peer: await this.resolvePeer(peer),
userId,
link, link,
}) })
} }

View file

@ -78,12 +78,6 @@ export async function sendMediaGroup(
*/ */
schedule?: Date | number schedule?: Date | number
/**
* For bots: inline or reply markup or an instruction
* to hide a reply keyboard or to force a reply.
*/
replyMarkup?: ReplyMarkup
/** /**
* Function that will be called after some part has been uploaded. * Function that will be called after some part has been uploaded.
* Only used when a file that requires uploading is passed, * Only used when a file that requires uploading is passed,
@ -123,7 +117,6 @@ export async function sendMediaGroup(
if (!params) params = {} if (!params) params = {}
let peer = await this.resolvePeer(chatId) let peer = await this.resolvePeer(chatId)
const replyMarkup = BotKeyboard._convertToTl(params.replyMarkup)
let replyTo = normalizeMessageId(params.replyTo) let replyTo = normalizeMessageId(params.replyTo)
@ -193,10 +186,13 @@ export async function sendMediaGroup(
peer, peer,
multiMedia, multiMedia,
silent: params.silent, silent: params.silent,
replyToMsgId: replyTo, replyTo: replyTo ?
randomId: randomLong(), {
_: 'inputReplyToMessage',
replyToMsgId: replyTo,
} :
undefined,
scheduleDate: normalizeDate(params.schedule), scheduleDate: normalizeDate(params.schedule),
replyMarkup,
clearDraft: params.clearDraft, clearDraft: params.clearDraft,
noforwards: params.forbidForwards, noforwards: params.forbidForwards,
sendAs: params.sendAs ? sendAs: params.sendAs ?
@ -211,7 +207,12 @@ export async function sendMediaGroup(
const msgs = res.updates const msgs = res.updates
.filter( .filter(
(u): u is tl.RawUpdateNewMessage | tl.RawUpdateNewChannelMessage | tl.RawUpdateNewScheduledMessage => (
u,
): u is
| tl.RawUpdateNewMessage
| tl.RawUpdateNewChannelMessage
| tl.RawUpdateNewScheduledMessage =>
u._ === 'updateNewMessage' || u._ === 'updateNewMessage' ||
u._ === 'updateNewChannelMessage' || u._ === 'updateNewChannelMessage' ||
u._ === 'updateNewScheduledMessage', u._ === 'updateNewScheduledMessage',

View file

@ -145,9 +145,11 @@ export async function sendMedia(
// some types dont have `caption` field, and ts warns us, // some types dont have `caption` field, and ts warns us,
// but since it's JS, they'll just be `undefined` and properly // but since it's JS, they'll just be `undefined` and properly
// handled by _parseEntities method // handled by _parseEntities method
params.caption || (media as Extract<typeof media, { caption?: unknown }>).caption, params.caption ||
(media as Extract<typeof media, { caption?: unknown }>).caption,
params.parseMode, params.parseMode,
params.entities || (media as Extract<typeof media, { entities?: unknown }>).entities, params.entities ||
(media as Extract<typeof media, { entities?: unknown }>).entities,
) )
let peer = await this.resolvePeer(chatId) let peer = await this.resolvePeer(chatId)
@ -179,7 +181,12 @@ export async function sendMedia(
peer, peer,
media: inputMedia, media: inputMedia,
silent: params.silent, silent: params.silent,
replyToMsgId: replyTo, replyTo: replyTo ?
{
_: 'inputReplyToMessage',
replyToMsgId: replyTo,
} :
undefined,
randomId: randomLong(), randomId: randomLong(),
scheduleDate: normalizeDate(params.schedule), scheduleDate: normalizeDate(params.schedule),
replyMarkup, replyMarkup,

View file

@ -153,7 +153,12 @@ export async function sendText(
peer, peer,
noWebpage: params.disableWebPreview, noWebpage: params.disableWebPreview,
silent: params.silent, silent: params.silent,
replyToMsgId: replyTo, replyTo: replyTo ?
{
_: 'inputReplyToMessage',
replyToMsgId: replyTo,
} :
undefined,
randomId: randomLong(), randomId: randomLong(),
scheduleDate: normalizeDate(params.schedule), scheduleDate: normalizeDate(params.schedule),
replyMarkup, replyMarkup,

View file

@ -13,7 +13,6 @@ import { InputPeerLike, MessageEntity } from '../../types'
* @param chatId Chat or user ID * @param chatId Chat or user ID
* @param messageId Identifier of the message to translate * @param messageId Identifier of the message to translate
* @param toLanguage Target language (two-letter ISO 639-1 language code) * @param toLanguage Target language (two-letter ISO 639-1 language code)
* @param fromLanguage Source language (two-letter ISO 639-1 language code, by default auto-detected)
* @internal * @internal
*/ */
export async function translateMessage( export async function translateMessage(
@ -21,13 +20,11 @@ export async function translateMessage(
chatId: InputPeerLike, chatId: InputPeerLike,
messageId: number, messageId: number,
toLanguage: string, toLanguage: string,
fromLanguage?: string,
): Promise<[string, MessageEntity[]] | null> { ): Promise<[string, MessageEntity[]] | null> {
const res = await this.call({ const res = await this.call({
_: 'messages.translateText', _: 'messages.translateText',
peer: await this.resolvePeer(chatId), peer: await this.resolvePeer(chatId),
id: [messageId], id: [messageId],
fromLang: fromLanguage,
toLang: toLanguage, toLang: toLanguage,
}) })

View file

@ -9,14 +9,12 @@ import { TelegramClient } from '../../client'
* *
* @param text Text to translate * @param text Text to translate
* @param toLanguage Target language (two-letter ISO 639-1 language code) * @param toLanguage Target language (two-letter ISO 639-1 language code)
* @param fromLanguage Source language (two-letter ISO 639-1 language code, by default auto-detected)
* @internal * @internal
*/ */
export async function translateText( export async function translateText(
this: TelegramClient, this: TelegramClient,
text: string, text: string,
toLanguage: string, toLanguage: string,
fromLanguage?: string,
): Promise<string | null> { ): Promise<string | null> {
const res = await this.call({ const res = await this.call({
_: 'messages.translateText', _: 'messages.translateText',
@ -27,7 +25,6 @@ export async function translateText(
entities: [], entities: [],
}, },
], ],
fromLang: fromLanguage,
toLang: toLanguage, toLang: toLanguage,
}) })

View file

@ -1,3 +1,4 @@
import { MustEqual } from '@mtcute/core'
import { tl } from '@mtcute/tl' import { tl } from '@mtcute/tl'
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'
@ -29,7 +30,7 @@ export class TakeoutSession {
* @param params Additional call parameters * @param params Additional call parameters
*/ */
async call<T extends tl.RpcMethod>( async call<T extends tl.RpcMethod>(
message: T, message: MustEqual<T, tl.RpcMethod>,
params?: { params?: {
throwFlood: boolean throwFlood: boolean
}, },
@ -39,7 +40,8 @@ export class TakeoutSession {
_: 'invokeWithTakeout', _: 'invokeWithTakeout',
takeoutId: this.id, takeoutId: this.id,
query: message, query: message,
}, // eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any,
params, params,
) )
} }

View file

@ -5,10 +5,7 @@ import Long from 'long'
import { tl } from '@mtcute/tl' import { tl } from '@mtcute/tl'
import defaultReaderMap from '@mtcute/tl/binary/reader' import defaultReaderMap from '@mtcute/tl/binary/reader'
import defaultWriterMap from '@mtcute/tl/binary/writer' import defaultWriterMap from '@mtcute/tl/binary/writer'
import { import { TlReaderMap, TlWriterMap } from '@mtcute/tl-runtime'
TlReaderMap,
TlWriterMap,
} from '@mtcute/tl-runtime'
import { import {
defaultReconnectionStrategy, defaultReconnectionStrategy,
@ -19,6 +16,7 @@ import {
} from './network' } from './network'
import { PersistentConnectionParams } from './network/persistent-connection' import { PersistentConnectionParams } from './network/persistent-connection'
import { ITelegramStorage, MemoryStorage } from './storage' import { ITelegramStorage, MemoryStorage } from './storage'
import { MustEqual } from './types'
import { import {
ControllablePromise, ControllablePromise,
createControllablePromise, createControllablePromise,
@ -288,7 +286,9 @@ export class BaseTelegramClient extends EventEmitter {
const apiId = const apiId =
typeof opts.apiId === 'string' ? parseInt(opts.apiId) : opts.apiId typeof opts.apiId === 'string' ? parseInt(opts.apiId) : opts.apiId
if (isNaN(apiId)) { throw new Error('apiId must be a number or a numeric string!') } if (isNaN(apiId)) {
throw new Error('apiId must be a number or a numeric string!')
}
this._transportFactory = opts.transport ?? defaultTransportFactory this._transportFactory = opts.transport ?? defaultTransportFactory
this._crypto = (opts.crypto ?? defaultCryptoProviderFactory)() this._crypto = (opts.crypto ?? defaultCryptoProviderFactory)()
@ -303,7 +303,9 @@ export class BaseTelegramClient extends EventEmitter {
if (this._testMode) { if (this._testMode) {
dc = this._useIpv6 ? defaultTestIpv6Dc : defaultTestDc dc = this._useIpv6 ? defaultTestIpv6Dc : defaultTestDc
} else { } else {
dc = this._useIpv6 ? defaultProductionIpv6Dc : defaultProductionDc dc = this._useIpv6 ?
defaultProductionIpv6Dc :
defaultProductionDc
} }
} }
@ -445,7 +447,10 @@ export class BaseTelegramClient extends EventEmitter {
await this.storage.getAuthKeyFor(this._primaryDc.id), await this.storage.getAuthKeyFor(this._primaryDc.id),
) )
if ((this._importForce || !this.primaryConnection.getAuthKey()) && this._importFrom) { if (
(this._importForce || !this.primaryConnection.getAuthKey()) &&
this._importFrom
) {
const data = readStringSession(this._readerMap, this._importFrom) const data = readStringSession(this._readerMap, this._importFrom)
if (data.testMode !== !this._testMode) { if (data.testMode !== !this._testMode) {
@ -609,7 +614,7 @@ export class BaseTelegramClient extends EventEmitter {
* @param params Additional call parameters * @param params Additional call parameters
*/ */
async call<T extends tl.RpcMethod>( async call<T extends tl.RpcMethod>(
message: T, message: MustEqual<T, tl.RpcMethod>,
params?: { params?: {
throwFlood?: boolean throwFlood?: boolean
connection?: SessionConnection connection?: SessionConnection
@ -700,7 +705,9 @@ export class BaseTelegramClient extends EventEmitter {
await this.changeDc(e.new_dc) await this.changeDc(e.new_dc)
continue continue
} }
} else if (e.constructor === tl.errors.AuthKeyUnregisteredError) { } else if (
e.constructor === tl.errors.AuthKeyUnregisteredError
) {
// we can try re-exporting auth from the primary connection // we can try re-exporting auth from the primary connection
this.log.warn('exported auth key error, re-exporting..') this.log.warn('exported auth key error, re-exporting..')
@ -968,7 +975,9 @@ export class BaseTelegramClient extends EventEmitter {
* > with [@BotFather](//t.me/botfather) * > with [@BotFather](//t.me/botfather)
*/ */
async exportSession(): Promise<string> { async exportSession(): Promise<string> {
if (!this.primaryConnection.getAuthKey()) { throw new Error('Auth key is not generated yet') } if (!this.primaryConnection.getAuthKey()) {
throw new Error('Auth key is not generated yet')
}
return writeStringSession(this._writerMap, { return writeStringSession(this._writerMap, {
version: 1, version: 1,

View file

@ -5,6 +5,8 @@ export type PartialOnly<T, K extends keyof T> = Partial<Pick<T, K>> & Omit<T, K>
export type MaybeArray<T> = T | T[] export type MaybeArray<T> = T | T[]
export type MustEqual<T, V> = T extends V ? (V extends T ? T : V) : V
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
export function assertNever(x: never): never { export function assertNever(x: never): never {
throw new Error('Illegal state') throw new Error('Illegal state')