Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added /transcribe requested in issue #35 #53

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
.env
*.log
dist
dist
package-lock.json
HeySreelal marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions locales/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ oops: "Ooopsie! I haven't implemented this feature yet. But be rest assured: I'v
timecodes_true: Nice! Now *Voicy* will add timecodes to the recognized text.
timecodes_false: Nice! Now *Voicy* will not add timecodes to the recognized text.
url: 'If you want to bypass the Telegram limit of 20Mb per file, you can use voicybot.com directly. Cheers!'
reply_to_voice: 'Please, reply to a voice message to use this command.'
3 changes: 2 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import countMessage from '@/middlewares/countMessage'
import disallowPrivate from '@/middlewares/disallowPrivate'
import engines from '@/engines'
import handleAddPromoException from '@/commands/handleAddPromoException'
import handleAudio from '@/handlers/handleAudio'
import handleAudio, { transcribeRequestHandler } from '@/handlers/handleAudio'
import handleDisableGoogle from '@/commands/handleDisableGoogle'
import handleEnableGoogle from '@/commands/handleEnableGoogle'
import handleEngine from '@/commands/handleEngine'
Expand Down Expand Up @@ -86,6 +86,7 @@ async function runApp() {
bot.command('l', checkAdminLock, handleL)
bot.command('addPromoException', checkSuperAdmin, handleAddPromoException)
bot.command('viewPromoExceptions', checkSuperAdmin, handleViewPromoExceptions)
bot.command('transcribe', checkAdminLock, transcribeRequestHandler)
// Callabcks
bot.callbackQuery(Object.keys(engines), handleSetEngine)
bot.callbackQuery(/li.+/, handleSetLanguage)
Expand Down
51 changes: 44 additions & 7 deletions src/handlers/handleAudio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import urlToText from '@/helpers/urlToText'
export default async function handleAudio(ctx: Context) {
try {
const message = ctx.msg

if (message.chat.type == 'group' || message.chat.type == 'supergroup') {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be the default behaviour, instead we should add a flag on the chat like transcribeAllAudio which is true by default

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit confused here. So, do we have to add a new command to turn off and on the transcribeAllAudio flag and store it in DB?

return
}

const voice =
message.voice || message.document || message.audio || message.video_note
// Check size
Expand All @@ -23,7 +28,8 @@ export default async function handleAudio(ctx: Context) {
}
// Get full url to the voice message
const fileData = await ctx.getFile()
const voiceUrl = await fileUrl(fileData.file_path)
const voiceUrl = fileUrl(fileData.file_path)

// Send action or transcription depending on whether chat is silent
await sendTranscription(ctx, voiceUrl, voice.file_id)
} catch (error) {
Expand Down Expand Up @@ -132,8 +138,8 @@ async function sendTranscription(ctx: Context, url: string, fileId: string) {
parse_mode: 'Markdown',
}
)
} catch (error) {
report(error, { ctx, location: 'updateMessagewithError' })
} catch (err) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't shorten the error variable name :) there is literally no need for that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, indeed no need, but just to ignore a warning that they were already defined in upper scope.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Can we call them sendTranscriptionError in the upper scope then? To avoid confusion

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for sure! I'll change that 🎯

report(err, { ctx, location: 'updateMessagewithError' })
}
}
try {
Expand All @@ -148,8 +154,8 @@ async function sendTranscription(ctx: Context, url: string, fileId: string) {
await ctx.dbchat.save()
}
}
} catch (error) {
report(error, { ctx, location: 'removeBadWitToken' })
} catch (err) {
HeySreelal marked this conversation as resolved.
Show resolved Hide resolved
report(err, { ctx, location: 'removeBadWitToken' })
}
report(error, { ctx, location: 'sendTranscription' })
} finally {
Expand All @@ -161,9 +167,40 @@ async function sendTranscription(ctx: Context, url: string, fileId: string) {
}
}

export async function transcribeRequestHandler(ctx: Context) {
try {
if (!ctx.msg.reply_to_message) {
return ctx.reply(ctx.i18n.t('reply_to_voice'), {
parse_mode: 'Markdown',
})
}
const message = ctx.msg.reply_to_message
const voice =
message.voice || message.document || message.audio || message.video_note

if (!voice) {
return ctx.reply(ctx.i18n.t('reply_to_voice'), {
parse_mode: 'Markdown',
})
}

if (voice.file_size && voice.file_size >= 19 * 1024 * 1024) {
if (!ctx.dbchat.silent) {
await sendLargeFileError(ctx)
}
return
}

const voiceUrl = fileUrl(voice.file_id)

await sendTranscription(ctx, voiceUrl, voice.file_id)
} catch (err) {
report(err, { ctx, location: 'transcribeRequestHandler' })
}
}

function splitText(text: string): string[] {
const chunks = text.match(/[\s\S]{1,4000}/g)
return chunks
return text.match(/[\s\S]{1,4000}/g)
}

function sanitizeChat(chat: Chat): Partial<Chat> {
Expand Down
Loading