Transcription Telegram Bot
Build a Telegram bot that transcribes audio and video messages in 99 languages using TypeScript with Deno in Supabase Edge Functions.
Introduction
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
Prefer to jump straight to the code?
Find the example project on GitHub.
Requirements
- An ElevenLabs account with an API key.
- A Supabase account (you can sign up for a free account via database.new).
- The Supabase CLI installed on your machine.
- The Deno runtime installed on your machine and optionally setup in your facourite IDE.
- A Telegram account.
Setup
Register a Telegram bot
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.
Create a Supabase project locally
After installing the Supabase CLI, run the following command to create a new Supabase project locally:
Create a database table to log the transcription results
Next, create a new database table to log the transcription results:
This will create a new migration file in the supabase/migrations
directory. Open the file and add the following SQL:
Create a Supabase Edge Function to handle Telegram webhook requests
Next, create a new Edge Function to handle Telegram webhook requests:
If you’re using VS Code or Cursor, select y
when the CLI prompts “Generate VS Code settings for Deno? [y/N]“!
Set up the environment variables
Within the supabase/functions
directory, create a new .env
file and add the following variables:
Dependencies
The project uses a couple of dependencies:
- The open-source grammY Framework to handle the Telegram webhook requests.
- The @supabase/supabase-js library to interact with the Supabase database.
- The ElevenLabs JavaScript SDK to interact with the speech-to-text API.
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.
Code the Telegram Bot
In your newly created scribe-bot/index.ts
file, add the following code:
Code deep dive
There’s a couple of things worth noting about the code. Let’s step through it step by step.
Handling the incoming request
To handle the incoming request, use the Deno.serve
handler. The handler checks whether the request has the correct secret and then passes the request to the handleUpdate
function.
Handle voice, audio, and video messages
The grammY frameworks provides a convenient way to filter for specific message types. In this case, the bot is listening for voice, audio, and video messages.
Using the request context, the bot extracts the file metadata and then uses Supabase Background Tasks EdgeRuntime.waitUntil
to run the transcription in the background.
This way you can provide an immediate response to the user and handle the transcription of the file in the background.
Transcription with the ElevenLabs API
Finally, in the background worker, the bot uses the ElevenLabs JavaScript SDK to transcribe the file. Once the transcription is complete, the bot replies to the user with the transcript and writes a log entry to the Supabase database using supabase-js.
Deploy to Supabase
If you haven’t already, create a new Supabase account at database.new and link the local project to your Supabase account:
Apply the database migrations
Run the following command to apply the database migrations from the supabase/migrations
directory:
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:
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 up the webhook
Set your bot’s webhook url to https://<PROJECT_REFERENCE>.functions.supabase.co/telegram-bot
(Replacing <...>
with respective values). In order to do that, simply run a GET request to the following url (in your browser, for example):
Note that the FUNCTION_SECRET
is the secret you set in your .env
file.
Set the function secrets
Now that you have all your secrets set locally, you can run the following command to set the secrets in your Supabase project:
Test the bot
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.