From 783df019217fc0641afd511439b6dcdb839d7985 Mon Sep 17 00:00:00 2001 From: ahiipsa Date: Tue, 26 Sep 2023 23:39:03 +0400 Subject: [PATCH] Added text to speech feature --- src/config.ts | 1 + src/modules/voice-translate/index.ts | 33 +++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/config.ts b/src/config.ts index b0f0eb51..ae760a61 100644 --- a/src/config.ts +++ b/src/config.ts @@ -148,5 +148,6 @@ export default { password: process.env.ES_PASSWORD ?? '', index: process.env.ES_INDEX }, + deepL: { apikey: process.env.DEEPL_API_KEY ?? '' }, gc: { credentials: process.env.GC_CREDENTIALS ?? '' } } diff --git a/src/modules/voice-translate/index.ts b/src/modules/voice-translate/index.ts index 38541331..74923791 100644 --- a/src/modules/voice-translate/index.ts +++ b/src/modules/voice-translate/index.ts @@ -26,16 +26,47 @@ export class VoiceTranslateBot implements PayableBot { public isSupportedEvent (ctx: OnMessageContext): boolean { const { voice, audio } = ctx.update.message - return (!!voice || !!audio) + return (!!voice || !!audio) || ctx.hasCommand('voice') } public getEstimatedPrice (ctx: OnMessageContext): number { return 0 } + public async onTextToSpeech (ctx: OnMessageContext, message: string): Promise { + if (!message) { + await ctx.reply('/voice command should contain text.') + return + } + + if (!ctx.chat?.id) { + throw new Error('Internal error') + } + + const progressMessage = await ctx.reply('Waite a moment...') + + const voiceResult = await textToSpeech(message) + + if (!voiceResult) { + await ctx.api.editMessageText(ctx.chat.id, progressMessage.message_id, 'An error occurred during the process of generating the message.') + return + } + + const inputFile = new InputFile(voiceResult) + + await ctx.api.deleteMessage(ctx.chat.id, progressMessage.message_id) + await ctx.replyWithVoice(inputFile) + } + public async onEvent (ctx: OnMessageContext): Promise { const { voice, audio } = ctx.update.message + if (ctx.hasCommand('voice')) { + const text = ctx.match.toString() + await this.onTextToSpeech(ctx, text) + return + } + if (!(!!voice || !!audio)) { return }