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
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 favourite 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:
_10supabase init
Create a database table to log the transcription results
Next, create a new database table to log the transcription results:
_10supabase migrations new init
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:
_10supabase 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]"!
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:
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:
_10supabase link
Apply the database migrations
Run the following command to apply the database migrations from the supabase/migrations
directory:
_10supabase 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:
_10supabase 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 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, run a GET request to the following URL (in your browser, for example):
_10https://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.
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:
_10supabase secrets set --env-file supabase/functions/.env
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.