-
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.
ixed ADC auth
- Loading branch information
Showing
8 changed files
with
516 additions
and
17 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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
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,17 @@ | ||
import GcTextToSpeech from '@google-cloud/text-to-speech' | ||
import config from '../../config' | ||
|
||
const credentials = JSON.parse(Buffer.from(config.gc.credentials, 'base64').toString('utf-8')) | ||
const client = new GcTextToSpeech.TextToSpeechClient({ credentials }) | ||
|
||
export async function textToSpeech (text: string): Promise<string | Uint8Array | null | undefined> { | ||
const ssml = `<speak>${text}</speak>` | ||
|
||
const [response] = await client.synthesizeSpeech({ | ||
input: { ssml }, | ||
voice: { languageCode: 'en-US', ssmlGender: 'MALE' }, | ||
audioConfig: { audioEncoding: 'MP3' } | ||
}) | ||
|
||
return response.audioContent | ||
} |
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,77 @@ | ||
import fs from 'fs' | ||
import pino from 'pino' | ||
import { InputFile } from 'grammy' | ||
import type { Logger } from 'pino' | ||
import { textToSpeech } from './client' | ||
import type { BotPayments } from '../payment' | ||
import { speechToText } from '../open-ai/api/openAi' | ||
import type { OnMessageContext, PayableBot } from '../types' | ||
|
||
export class VoiceTranslateBot implements PayableBot { | ||
private readonly payments: BotPayments | ||
|
||
private readonly logger: Logger | ||
|
||
constructor (payments: BotPayments) { | ||
this.payments = payments | ||
this.logger = pino({ | ||
name: 'VoiceTranslate', | ||
transport: { | ||
target: 'pino-pretty', | ||
options: { colorize: true } | ||
} | ||
}) | ||
} | ||
|
||
public isSupportedEvent (ctx: OnMessageContext): boolean { | ||
const { voice, audio } = ctx.update.message | ||
|
||
return (!!voice || !!audio) | ||
} | ||
|
||
public getEstimatedPrice (ctx: OnMessageContext): number { | ||
return 0 | ||
} | ||
|
||
public async onEvent (ctx: OnMessageContext): Promise<void> { | ||
const { voice, audio } = ctx.update.message | ||
|
||
if (!(!!voice || !!audio)) { | ||
return | ||
} | ||
|
||
const message = await ctx.reply('Waite a moment...') | ||
|
||
if (!ctx.chat?.id) { | ||
throw Error('chat id is undefined') | ||
} | ||
|
||
const file = await ctx.getFile() | ||
const path = await file.download() | ||
|
||
let ext = 'ogg' | ||
|
||
if (file.file_path) { | ||
ext = file.file_path.split('.').pop() ?? ext | ||
} | ||
|
||
const filename = path + '.' + ext | ||
fs.renameSync(path, filename) | ||
|
||
const resultText = await speechToText(fs.createReadStream(filename)) | ||
fs.rmSync(filename) | ||
|
||
const voiceResult = await textToSpeech(resultText) | ||
|
||
if (!voiceResult) { | ||
await ctx.reply('voice generation error') | ||
return | ||
} | ||
|
||
await ctx.api.editMessageText(ctx.chat.id, message.message_id, resultText) | ||
|
||
const inputFile = new InputFile(voiceResult) | ||
|
||
await ctx.replyWithVoice(inputFile) | ||
} | ||
} |