-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added voice translate and text to speech
- Loading branch information
Showing
6 changed files
with
112 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import GcTextToSpeech, { type TextToSpeechClient } from '@google-cloud/text-to-speech' | ||
import config from '../config' | ||
import type { CredentialBody } from 'google-auth-library/build/src/auth/credentials' | ||
|
||
class GcTextToSpeechClient { | ||
private readonly _client: TextToSpeechClient | ||
constructor (credentials: CredentialBody) { | ||
this._client = new GcTextToSpeech.TextToSpeechClient({ credentials }) | ||
} | ||
|
||
async textToSpeech (text: string): Promise<string | Uint8Array | null | undefined> { | ||
const ssml = `<speak>${text}</speak>` | ||
|
||
const [response] = await this._client.synthesizeSpeech({ | ||
input: { ssml }, | ||
voice: { languageCode: 'en-US', ssmlGender: 'MALE' }, | ||
audioConfig: { audioEncoding: 'OGG_OPUS' } | ||
}) | ||
|
||
return response.audioContent | ||
} | ||
} | ||
|
||
const credentials = JSON.parse(Buffer.from(config.gc.credentials, 'base64').toString('utf-8')) | ||
|
||
export const gcTextToSpeedClient = new GcTextToSpeechClient(credentials) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import pino from 'pino' | ||
import { InputFile } from 'grammy' | ||
import type { Logger } from 'pino' | ||
import type { BotPayments } from '../payment' | ||
import type { OnMessageContext, PayableBot } from '../types' | ||
import { gcTextToSpeedClient } from '../../google-cloud/gcTextToSpeechClient' | ||
|
||
export class TextToSpeechBot implements PayableBot { | ||
private readonly payments: BotPayments | ||
|
||
private readonly logger: Logger | ||
|
||
constructor (payments: BotPayments) { | ||
this.payments = payments | ||
this.logger = pino({ | ||
name: 'TextToSpeech', | ||
transport: { | ||
target: 'pino-pretty', | ||
options: { colorize: true } | ||
} | ||
}) | ||
} | ||
|
||
public isSupportedEvent (ctx: OnMessageContext): boolean { | ||
return ctx.hasCommand('voice') | ||
} | ||
|
||
public getEstimatedPrice (ctx: OnMessageContext): number { | ||
const str = ctx.match?.toString() ?? '' | ||
|
||
return str.length * 0.0001 | ||
} | ||
|
||
public async onTextToSpeech (ctx: OnMessageContext, message: string): Promise<void> { | ||
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 gcTextToSpeedClient.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<void> { | ||
if (ctx.hasCommand('voice')) { | ||
const text = ctx.match.toString() | ||
await this.onTextToSpeech(ctx, text) | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters