apps/docs/content/guides/functions/examples/elevenlabs-transcribe-speech.mdx
In this tutorial you will learn how to build a Telegram bot that transcribes audio and video messages in 99 languages using TypeScript and the ElevenLabs Scribe model via the speech to text API.
To check out what the end result will look like, you can test out the t.me/ElevenLabsScribeBot
<Admonition type="tip">Find the example project on GitHub.
</Admonition>Use the BotFather to create a new Telegram bot. Run the /newbot command and follow the instructions to create a new bot. At the end, you will receive your secret bot token. Note it down securely for the next step.
After installing the Supabase CLI, run the following command to create a new Supabase project locally:
supabase init
Next, create a new database table to log the transcription results:
supabase migrations new init
This will create a new migration file in the supabase/migrations directory. Open the file and add the following SQL:
CREATE TABLE IF NOT EXISTS transcription_logs (
id BIGSERIAL PRIMARY KEY,
file_type VARCHAR NOT NULL,
duration INTEGER NOT NULL,
chat_id BIGINT NOT NULL,
message_id BIGINT NOT NULL,
username VARCHAR,
transcript TEXT,
language_code VARCHAR,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
error TEXT
);
ALTER TABLE transcription_logs ENABLE ROW LEVEL SECURITY;
Next, create a new Edge Function to handle Telegram webhook requests:
supabase functions new scribe-bot
If you're using VS Code or Cursor, select y when the CLI prompts "Generate VS Code settings for Deno? [y/N]"!
Within the supabase/functions directory, create a new .env file and add the following variables:
# Find / create an API key at https://elevenlabs.io/app/settings/api-keys
ELEVENLABS_API_KEY=your_api_key
# The bot token you received from the BotFather.
TELEGRAM_BOT_TOKEN=your_bot_token
# A random secret chosen by you to secure the function.
FUNCTION_SECRET=random_secret
The project uses a couple of dependencies:
Since Supabase Edge Function uses the Deno runtime, you don't need to install the dependencies, rather you can import them via the npm: prefix.
In your newly created scribe-bot/index.ts file, add the following code:
import { Bot, webhookCallback } from 'https://deno.land/x/[email protected]/mod.ts'
import 'jsr:@supabase/functions-js/edge-runtime.d.ts'
import { createClient } from 'npm:@supabase/supabase-js@2'
import { ElevenLabsClient } from 'npm:[email protected]'
console.log(`Function "elevenlabs-scribe-bot" up and running!`)
const elevenLabsClient = new ElevenLabsClient({
apiKey: Deno.env.get('ELEVENLABS_API_KEY') || '',
})
const supabase = createClient(
Deno.env.get('SUPABASE_URL') || '',
Deno.env.get('SUPABASE_SERVICE_ROLE_KEY') || ''
)
async function scribe({
fileURL,
fileType,
duration,
chatId,
messageId,
username,
}: {
fileURL: string
fileType: string
duration: number
chatId: number
messageId: number
username: string
}) {
let transcript: string | null = null
let languageCode: string | null = null
let errorMsg: string | null = null
try {
const sourceFileArrayBuffer = await fetch(fileURL).then((res) => res.arrayBuffer())
const sourceBlob = new Blob([sourceFileArrayBuffer], {
type: fileType,
})
const scribeResult = await elevenLabsClient.speechToText.convert({
file: sourceBlob,
model_id: 'scribe_v1',
tag_audio_events: false,
})
transcript = scribeResult.text
languageCode = scribeResult.language_code
// Reply to the user with the transcript
await bot.api.sendMessage(chatId, transcript, {
reply_parameters: { message_id: messageId },
})
} catch (error) {
errorMsg = error.message
console.log(errorMsg)
await bot.api.sendMessage(chatId, 'Sorry, there was an error. Please try again.', {
reply_parameters: { message_id: messageId },
})
}
// Write log to Supabase.
const logLine = {
file_type: fileType,
duration,
chat_id: chatId,
message_id: messageId,
username,
language_code: languageCode,
error: errorMsg,
}
console.log({ logLine })
await supabase.from('transcription_logs').insert({ ...logLine, transcript })
}
const telegramBotToken = Deno.env.get('TELEGRAM_BOT_TOKEN')
const bot = new Bot(telegramBotToken || '')
const startMessage = `Welcome to the ElevenLabs Scribe Bot\\! I can transcribe speech in 99 languages with super high accuracy\\!
\nTry it out by sending or forwarding me a voice message, video, or audio file\\!
\n[Learn more about Scribe](https://elevenlabs.io/speech-to-text) or [build your own bot](https://elevenlabs.io/docs/cookbooks/speech-to-text/telegram-bot)\\!
`
bot.command('start', (ctx) => ctx.reply(startMessage.trim(), { parse_mode: 'MarkdownV2' }))
bot.on([':voice', ':audio', ':video'], async (ctx) => {
try {
const file = await ctx.getFile()
const fileURL = `https://api.telegram.org/file/bot${telegramBotToken}/${file.file_path}`
const fileMeta = ctx.message?.video ?? ctx.message?.voice ?? ctx.message?.audio
if (!fileMeta) {
return ctx.reply('No video|audio|voice metadata found. Please try again.')
}
// Run the transcription in the background.
EdgeRuntime.waitUntil(
scribe({
fileURL,
fileType: fileMeta.mime_type!,
duration: fileMeta.duration,
chatId: ctx.chat.id,
messageId: ctx.message?.message_id!,
username: ctx.from?.username || '',
})
)
// Reply to the user immediately to let them know we received their file.
return ctx.reply('Received. Scribing...')
} catch (error) {
console.error(error)
return ctx.reply(
'Sorry, there was an error getting the file. Please try again with a smaller file!'
)
}
})
const handleUpdate = webhookCallback(bot, 'std/http')
Deno.serve(async (req) => {
try {
const url = new URL(req.url)
if (url.searchParams.get('secret') !== Deno.env.get('FUNCTION_SECRET')) {
return new Response('not allowed', { status: 405 })
}
return await handleUpdate(req)
} catch (err) {
console.error(err)
}
})
If you haven't already, create a new Supabase account at database.new and link the local project to your Supabase account:
supabase link
Run the following command to apply the database migrations from the supabase/migrations directory:
supabase db push
Navigate to the table editor in your Supabase dashboard and you should see and empty transcription_logs table.
Lastly, run the following command to deploy the Edge Function:
supabase functions deploy --no-verify-jwt scribe-bot
Navigate to the Edge Functions view in your Supabase dashboard and you should see the scribe-bot function deployed. Make a note of the function URL as you'll need it later, it should look something like https://<project-ref>.functions.supabase.co/scribe-bot.
Set your bot's webhook URL to https://<PROJECT_REFERENCE>.functions.supabase.co/telegram-bot (Replacing <...> with respective values). In order to do that, run a GET request to the following URL (in your browser, for example):
https://api.telegram.org/bot<TELEGRAM_BOT_TOKEN>/setWebhook?url=https://<PROJECT_REFERENCE>.supabase.co/functions/v1/scribe-bot?secret=<FUNCTION_SECRET>
Note that the FUNCTION_SECRET is the secret you set in your .env file.
Now that you have all your secrets set locally, you can run the following command to set the secrets in your Supabase project:
supabase secrets set --env-file supabase/functions/.env
Finally you can test the bot by sending it a voice message, audio or video file.
After you see the transcript as a reply, navigate back to your table editor in the Supabase dashboard and you should see a new row in your transcription_logs table.