fix: error on extra fields in TelegramClient#call + related fixes
This commit is contained in:
parent
6221a8716f
commit
6a2c5d90b7
11 changed files with 55 additions and 50 deletions
|
@ -3092,12 +3092,6 @@ export interface TelegramClient extends BaseTelegramClient {
|
|||
*/
|
||||
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.
|
||||
* 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 messageId Identifier of the message to translate
|
||||
* @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(
|
||||
chatId: InputPeerLike,
|
||||
messageId: number,
|
||||
toLanguage: string,
|
||||
fromLanguage?: string
|
||||
toLanguage: string
|
||||
): Promise<[string, MessageEntity[]] | null>
|
||||
/**
|
||||
* Translate text to a given language.
|
||||
|
@ -3454,13 +3446,8 @@ export interface TelegramClient extends BaseTelegramClient {
|
|||
*
|
||||
* @param text Text to translate
|
||||
* @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(
|
||||
text: string,
|
||||
toLanguage: string,
|
||||
fromLanguage?: string
|
||||
): Promise<string | null>
|
||||
translateText(text: string, toLanguage: string): Promise<string | null>
|
||||
/**
|
||||
* Unpin all pinned messages in a chat.
|
||||
*
|
||||
|
|
|
@ -49,7 +49,6 @@ export async function addChatMembers(
|
|||
users as InputPeerLike[],
|
||||
normalizeToInputUser,
|
||||
),
|
||||
fwdLimit: forwardCount,
|
||||
})
|
||||
this._handleUpdate(updates)
|
||||
} else throw new MtInvalidPeerTypeError(chatId, 'chat or channel')
|
||||
|
|
|
@ -24,7 +24,6 @@ export async function hideAllJoinRequests(
|
|||
_: 'messages.hideAllChatJoinRequests',
|
||||
approved: action === 'approve',
|
||||
peer: await this.resolvePeer(peer),
|
||||
userId,
|
||||
link,
|
||||
})
|
||||
}
|
||||
|
|
|
@ -78,12 +78,6 @@ export async function sendMediaGroup(
|
|||
*/
|
||||
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.
|
||||
* Only used when a file that requires uploading is passed,
|
||||
|
@ -123,7 +117,6 @@ export async function sendMediaGroup(
|
|||
if (!params) params = {}
|
||||
|
||||
let peer = await this.resolvePeer(chatId)
|
||||
const replyMarkup = BotKeyboard._convertToTl(params.replyMarkup)
|
||||
|
||||
let replyTo = normalizeMessageId(params.replyTo)
|
||||
|
||||
|
@ -193,10 +186,13 @@ export async function sendMediaGroup(
|
|||
peer,
|
||||
multiMedia,
|
||||
silent: params.silent,
|
||||
replyToMsgId: replyTo,
|
||||
randomId: randomLong(),
|
||||
replyTo: replyTo ?
|
||||
{
|
||||
_: 'inputReplyToMessage',
|
||||
replyToMsgId: replyTo,
|
||||
} :
|
||||
undefined,
|
||||
scheduleDate: normalizeDate(params.schedule),
|
||||
replyMarkup,
|
||||
clearDraft: params.clearDraft,
|
||||
noforwards: params.forbidForwards,
|
||||
sendAs: params.sendAs ?
|
||||
|
@ -211,7 +207,12 @@ export async function sendMediaGroup(
|
|||
|
||||
const msgs = res.updates
|
||||
.filter(
|
||||
(u): u is tl.RawUpdateNewMessage | tl.RawUpdateNewChannelMessage | tl.RawUpdateNewScheduledMessage =>
|
||||
(
|
||||
u,
|
||||
): u is
|
||||
| tl.RawUpdateNewMessage
|
||||
| tl.RawUpdateNewChannelMessage
|
||||
| tl.RawUpdateNewScheduledMessage =>
|
||||
u._ === 'updateNewMessage' ||
|
||||
u._ === 'updateNewChannelMessage' ||
|
||||
u._ === 'updateNewScheduledMessage',
|
||||
|
|
|
@ -145,9 +145,11 @@ export async function sendMedia(
|
|||
// some types dont have `caption` field, and ts warns us,
|
||||
// but since it's JS, they'll just be `undefined` and properly
|
||||
// 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.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)
|
||||
|
@ -179,7 +181,12 @@ export async function sendMedia(
|
|||
peer,
|
||||
media: inputMedia,
|
||||
silent: params.silent,
|
||||
replyToMsgId: replyTo,
|
||||
replyTo: replyTo ?
|
||||
{
|
||||
_: 'inputReplyToMessage',
|
||||
replyToMsgId: replyTo,
|
||||
} :
|
||||
undefined,
|
||||
randomId: randomLong(),
|
||||
scheduleDate: normalizeDate(params.schedule),
|
||||
replyMarkup,
|
||||
|
|
|
@ -153,7 +153,12 @@ export async function sendText(
|
|||
peer,
|
||||
noWebpage: params.disableWebPreview,
|
||||
silent: params.silent,
|
||||
replyToMsgId: replyTo,
|
||||
replyTo: replyTo ?
|
||||
{
|
||||
_: 'inputReplyToMessage',
|
||||
replyToMsgId: replyTo,
|
||||
} :
|
||||
undefined,
|
||||
randomId: randomLong(),
|
||||
scheduleDate: normalizeDate(params.schedule),
|
||||
replyMarkup,
|
||||
|
|
|
@ -13,7 +13,6 @@ import { InputPeerLike, MessageEntity } from '../../types'
|
|||
* @param chatId Chat or user ID
|
||||
* @param messageId Identifier of the message to translate
|
||||
* @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
|
||||
*/
|
||||
export async function translateMessage(
|
||||
|
@ -21,13 +20,11 @@ export async function translateMessage(
|
|||
chatId: InputPeerLike,
|
||||
messageId: number,
|
||||
toLanguage: string,
|
||||
fromLanguage?: string,
|
||||
): Promise<[string, MessageEntity[]] | null> {
|
||||
const res = await this.call({
|
||||
_: 'messages.translateText',
|
||||
peer: await this.resolvePeer(chatId),
|
||||
id: [messageId],
|
||||
fromLang: fromLanguage,
|
||||
toLang: toLanguage,
|
||||
})
|
||||
|
||||
|
|
|
@ -9,14 +9,12 @@ import { TelegramClient } from '../../client'
|
|||
*
|
||||
* @param text Text to translate
|
||||
* @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
|
||||
*/
|
||||
export async function translateText(
|
||||
this: TelegramClient,
|
||||
text: string,
|
||||
toLanguage: string,
|
||||
fromLanguage?: string,
|
||||
): Promise<string | null> {
|
||||
const res = await this.call({
|
||||
_: 'messages.translateText',
|
||||
|
@ -27,7 +25,6 @@ export async function translateText(
|
|||
entities: [],
|
||||
},
|
||||
],
|
||||
fromLang: fromLanguage,
|
||||
toLang: toLanguage,
|
||||
})
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import { MustEqual } from '@mtcute/core'
|
||||
import { tl } from '@mtcute/tl'
|
||||
|
||||
import { TelegramClient } from '../../client'
|
||||
|
@ -29,7 +30,7 @@ export class TakeoutSession {
|
|||
* @param params Additional call parameters
|
||||
*/
|
||||
async call<T extends tl.RpcMethod>(
|
||||
message: T,
|
||||
message: MustEqual<T, tl.RpcMethod>,
|
||||
params?: {
|
||||
throwFlood: boolean
|
||||
},
|
||||
|
@ -39,7 +40,8 @@ export class TakeoutSession {
|
|||
_: 'invokeWithTakeout',
|
||||
takeoutId: this.id,
|
||||
query: message,
|
||||
},
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
} as any,
|
||||
params,
|
||||
)
|
||||
}
|
||||
|
|
|
@ -5,10 +5,7 @@ import Long from 'long'
|
|||
import { tl } from '@mtcute/tl'
|
||||
import defaultReaderMap from '@mtcute/tl/binary/reader'
|
||||
import defaultWriterMap from '@mtcute/tl/binary/writer'
|
||||
import {
|
||||
TlReaderMap,
|
||||
TlWriterMap,
|
||||
} from '@mtcute/tl-runtime'
|
||||
import { TlReaderMap, TlWriterMap } from '@mtcute/tl-runtime'
|
||||
|
||||
import {
|
||||
defaultReconnectionStrategy,
|
||||
|
@ -19,6 +16,7 @@ import {
|
|||
} from './network'
|
||||
import { PersistentConnectionParams } from './network/persistent-connection'
|
||||
import { ITelegramStorage, MemoryStorage } from './storage'
|
||||
import { MustEqual } from './types'
|
||||
import {
|
||||
ControllablePromise,
|
||||
createControllablePromise,
|
||||
|
@ -288,7 +286,9 @@ export class BaseTelegramClient extends EventEmitter {
|
|||
const 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._crypto = (opts.crypto ?? defaultCryptoProviderFactory)()
|
||||
|
@ -303,7 +303,9 @@ export class BaseTelegramClient extends EventEmitter {
|
|||
if (this._testMode) {
|
||||
dc = this._useIpv6 ? defaultTestIpv6Dc : defaultTestDc
|
||||
} 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),
|
||||
)
|
||||
|
||||
if ((this._importForce || !this.primaryConnection.getAuthKey()) && this._importFrom) {
|
||||
if (
|
||||
(this._importForce || !this.primaryConnection.getAuthKey()) &&
|
||||
this._importFrom
|
||||
) {
|
||||
const data = readStringSession(this._readerMap, this._importFrom)
|
||||
|
||||
if (data.testMode !== !this._testMode) {
|
||||
|
@ -609,7 +614,7 @@ export class BaseTelegramClient extends EventEmitter {
|
|||
* @param params Additional call parameters
|
||||
*/
|
||||
async call<T extends tl.RpcMethod>(
|
||||
message: T,
|
||||
message: MustEqual<T, tl.RpcMethod>,
|
||||
params?: {
|
||||
throwFlood?: boolean
|
||||
connection?: SessionConnection
|
||||
|
@ -700,7 +705,9 @@ export class BaseTelegramClient extends EventEmitter {
|
|||
await this.changeDc(e.new_dc)
|
||||
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
|
||||
this.log.warn('exported auth key error, re-exporting..')
|
||||
|
||||
|
@ -968,7 +975,9 @@ export class BaseTelegramClient extends EventEmitter {
|
|||
* > with [@BotFather](//t.me/botfather)
|
||||
*/
|
||||
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, {
|
||||
version: 1,
|
||||
|
|
|
@ -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 MustEqual<T, V> = T extends V ? (V extends T ? T : V) : V
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
export function assertNever(x: never): never {
|
||||
throw new Error('Illegal state')
|
||||
|
|
Loading…
Reference in a new issue