Skip to content

Commit

Permalink
Added text to speech feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ahiipsa committed Sep 26, 2023
1 parent 9216a8c commit 783df01
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? '' }
}
33 changes: 32 additions & 1 deletion src/modules/voice-translate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<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 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> {
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
}
Expand Down

0 comments on commit 783df01

Please sign in to comment.