chore(client)!: improved translate* methods

breaking: both methods now return `TextWithEntities`
This commit is contained in:
alina 🌸 2023-12-11 21:52:48 +03:00
parent dba22e7946
commit f4ee1f599f
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI
4 changed files with 28 additions and 21 deletions

View file

@ -288,7 +288,6 @@ import {
InputText,
MaybeDynamic,
Message,
MessageEntity,
MessageMedia,
MessageReactions,
ParametersSkip2,
@ -315,6 +314,7 @@ import {
StoryViewer,
StoryViewersList,
TakeoutSession,
TextWithEntities,
TypingStatus,
UploadedFile,
UploadFileLike,
@ -3950,20 +3950,16 @@ export interface TelegramClient extends BaseTelegramClient {
/** Target language (two-letter ISO 639-1 language code) */
toLanguage: string
},
): Promise<[string, MessageEntity[]] | null>
): Promise<TextWithEntities>
/**
* Translate text to a given language.
*
* Returns `null` if it could not translate the message.
*
* > **Note**: For now doesn't seem to work, returns null for all messages.
*
* **Available**: 👤 users only
*
* @param text Text to translate
* @param toLanguage Target language (two-letter ISO 639-1 language code)
*/
translateText(text: string, toLanguage: string): Promise<string | null>
translateText(text: InputText, toLanguage: string): Promise<TextWithEntities>
/**
* Unpin all pinned messages in a chat.
*

View file

@ -57,7 +57,6 @@ import {
InputText,
MaybeDynamic,
Message,
MessageEntity,
MessageMedia,
MessageReactions,
ParametersSkip2,
@ -84,6 +83,7 @@ import {
StoryViewer,
StoryViewersList,
TakeoutSession,
TextWithEntities,
TypingStatus,
UploadedFile,
UploadFileLike,

View file

@ -1,6 +1,6 @@
import { BaseTelegramClient } from '@mtcute/core'
import { InputMessageId, MessageEntity, normalizeInputMessageId } from '../../types/index.js'
import { InputMessageId, normalizeInputMessageId, TextWithEntities } from '../../types/index.js'
import { resolvePeer } from '../users/resolve-peer.js'
/**
@ -14,7 +14,7 @@ export async function translateMessage(
/** Target language (two-letter ISO 639-1 language code) */
toLanguage: string
},
): Promise<[string, MessageEntity[]] | null> {
): Promise<TextWithEntities> {
const { toLanguage } = params
const { chatId, message } = normalizeInputMessageId(params)
@ -25,5 +25,8 @@ export async function translateMessage(
toLang: toLanguage,
})
return [res.result[0].text, res.result[0].entities.map((it) => new MessageEntity(it, res.result[0].text))]
return {
text: res.result[0].text,
entities: res.result[0].entities,
}
}

View file

@ -1,31 +1,39 @@
import { BaseTelegramClient } from '@mtcute/core'
import { BaseTelegramClient, MtTypeAssertionError } from '@mtcute/core'
import { InputText, TextWithEntities } from '../../types/misc/entities.js'
import { _normalizeInputText } from '../misc/normalize-text.js'
/**
* Translate text to a given language.
*
* Returns `null` if it could not translate the message.
*
* > **Note**: For now doesn't seem to work, returns null for all messages.
*
* @param text Text to translate
* @param toLanguage Target language (two-letter ISO 639-1 language code)
*/
export async function translateText(
client: BaseTelegramClient,
text: string,
text: InputText,
toLanguage: string,
): Promise<string | null> {
): Promise<TextWithEntities> {
const [message, entities] = await _normalizeInputText(client, text)
const res = await client.call({
_: 'messages.translateText',
text: [
{
_: 'textWithEntities',
text,
entities: [],
text: message,
entities: entities || [],
},
],
toLang: toLanguage,
})
return res.result[0].text
if (!res.result[0]) {
throw new MtTypeAssertionError('messages.translateResult#result', 'not empty', 'empty')
}
return {
text: res.result[0].text,
entities: res.result[0].entities,
}
}