36 lines
925 B
TypeScript
36 lines
925 B
TypeScript
import { fetch } from 'undici';
|
|
import { logger } from './logger';
|
|
|
|
interface BalabolaResponse {
|
|
bad_query: number,
|
|
query: string,
|
|
text: string,
|
|
error: number,
|
|
is_cached: number,
|
|
empty_zeliboba: number,
|
|
intro: number,
|
|
signature: string;
|
|
}
|
|
|
|
export class BalabolaError extends Error { }
|
|
export class BalabolaEmptyText extends Error { }
|
|
|
|
const BASE_URL = 'https://yandex.ru/lab/api/yalm/text3';
|
|
|
|
export async function balabola(query: string, intro: number): Promise<string> {
|
|
logger.debug({ query, intro });
|
|
|
|
const result = await fetch(BASE_URL, {
|
|
method: 'POST',
|
|
body: JSON.stringify({
|
|
filter: 1,
|
|
intro,
|
|
query
|
|
})
|
|
}).then(res => res.json()) as BalabolaResponse;
|
|
|
|
if (result.bad_query === 1) throw new BalabolaError('bad query');
|
|
if (!result.text) throw new BalabolaEmptyText('empty text');
|
|
|
|
return result.text;
|
|
} |