How to build a Shazam-like Telegram Bot using Python

When you listen to music outside, like in a bar or a shop, and you really like it, don't you wonder what the song's title and the singer's name are?
It happened to me a lot of times and there is only one solution. Shazam. Download it and tap the Pop-Up button to identify the music.
In this article, we are going to build a similar app to discover the song and the singer. A quick and easy way to develop this Data Science project is to create a Telegram Bot with Python.
While you listen to the music, you record the voice. This bot will transcribe the audio into text and will find the title of the song and the singer for you. Let's get started!
Table of contents:
Part 1: Create Telegram Bot with Python

This tutorial will be split into two parts. In the first part, we are going to focus on creating the telegram bot that transcribes the registration and discovers the information behind the song. Later, we'll deploy the telegram bot to fly.io, which constitutes one of the many platforms that enable to building, running and scaling of applications in the cloud.
- Requirements
- Install libraries
- First steps to create Telegram Bot
- Transcribe the song's audio
- Discover the information about the song
Requirements:
There are a few steps to steps to follow before starting Programming with Python.
- Go to Telegram, search for @botfather and click on the first result.

- Press the Start button to begin a conversation with BotFather.

- Write
/newbot
and follow the instructions suggested in the screenshot. After creating the bot, you will receive the token by BotFather, allowing you access to Telegram API.

- Type the name of your telegram bot.

Install libraries
First of all, install a Python library, that allows creating a telegram bot with few lines of code. It's called pyTelegramBotAPI.
pip install pyTelegramBotAPI
To transcribe the audio, I am going to use Steamship‘s API. Before installing the package, you need to install nodej. In Windows, you need to install it from here and add the path on the PATH environment variable, while on ubuntu you need two lines of code:
sudo apt install nodejs
sudo apt install npm
After you can finally install Steamship CLI in your Python virtual environment.
pip install steamship
npm install -g @steamship/cli
ship login
Once it's installed, we are going to pass to the third API that permits us to discover the song from the audio. This is possible by getting access to Google search results through this API, called SerpApi.
pip install google-search-results
First steps to creating the telegram bot
Once you have obtained the API token of Telegram and created the Telegram Bot, which should appear in your Telegram application, we can start to explore pyTelegramBotAPI, which provides Python implementations to create a Telegram Bot with few lines of code.
There are many other different python libraries that allow building Telegram Bots, but since I found many tutorials that used this library and the documentation was well done, I chose it for my little project.
import os
import json
...
from steamship import Steamship, TaskState
import telebot
from serpapi import GoogleSearch
f = open("cred.json", "rb")
params = json.load(f)
BOT_TOKEN = params['BOT_TOKEN']
bot = telebot.TeleBot(BOT_TOKEN)
We have a file cred.json
that contains the bot token and the API keys for Steamship and SerpApi:
{
"BOT_TOKEN":,
"API_KEY":,
"STEAM_API_KEY":
}
In line 11, we simply create an instance of the TeleBot
, which is a class that provides functions to handle messages in Telegram. For example, let's define a message handler, that returns the message "Insert the audio of your song" if you write on the chat /start
or /hello
.
@bot.message_handler(commands=['start', 'hello'])
def send_welcome(message):
bot.reply_to(message, "Insert the audio of your song!")
bot.infinity_polling()
At the end of the code, we launch the bot using bot.infinity_polling()

You should have a similar result, like in the screenshot above, after running python bot.py
on the terminal.
After the function send_welcome
, we can define another function, called telegram_bot
, which will deal with audio files of the type "voice".
@bot.message_handler(content_types=['voice'])
def telegram_bot(message,bot_token=params["BOT_TOKEN"]):
# insert audio
file_info = bot.get_file(message.voice.file_id)
# extract telegram's url of the audio
audio_url = 'https://api.telegram.org/file/bot{}/{}'.format(bot_token,file_info.file_path)
bot.infinity_polling()
We are interested in obtaining the public URL of your audio file sent to the bot. To check if the URL is right, copy the following path, by substituting the bot token and the file path, in your browser. If it works, it should download the audio to your local PC.
https://api.telegram.org/file/bot/
The last operation we need to do is the convertion of the audio from OGA to MP3 format. This is an important step since the OGA file is not supported for transcription.
To convert the audio to an mp3 file, we are going to use the ffmpeg wrapper, which allows converting various audio files using command lines. Before using it, you need installing. If you work in windows, check this guide that shows all the steps, while in Linux you need only the command line sudo apt install ffmpeg
.
After the convertion reached in line 4 into the function convert_oga_to_mp3
, we need to extract again the URL of the new audio file, which will have a different format at the end.
Transcribe the audio's song
Now, we have reached the most interesting part of the tutorial which makes us reach the goals of the telegram bot. Before going further, log in here to obtain the API key. First, we want to transcribe the audio using Steamship API. The function transcribe_audio
takes as input the audio URL retrieved before and the instance of Steamship
, which is a class that provides functions and methods for many AI applications. In this case, it's focused on audio transcription.
The code can be summarized by a few lines of code:
- In line 3, we specify that we are going to use the
audio_markdown
package to transcribe audio and, then, generate the Markdown output. - In the next line, we invoke the method
transcribe_url
by callinginvoke
. We also need to pass our URL of the audio. - In the middle of the function, we invoke the method
get_markdown
, which will allow us to receive the transcription of the audio. The number of retries is limited to 100 to avoid infinite loops in case it doesn't succeed. - The function returns the transcription at the end and we get the resulting text on the telegram bot.
Discover the information about the song
After obtaining the transcription, we pass to the next step, which consists in discovering the title and the singer of the song. This is possible using SerpApi, which is an API that permits getting access to results of Google and other websites.
We need to pass a dictionary to GoogleSearch
with the parameters, like the song's words, the API key of SerpApi and we are going to obtain the results from Google. It returns a JSON output, which can be converted into a Python dictionary to get access to the information. After, a text message will be sent back to the sender, which can be the missed/achieved discovery.

Part 2: Deploy Telegram Bot to Fly.io
Until now, we could make work the bot by running the following command line:
python bot.py
But it means that the bot will work only when you run this code and it's not practical. It's preferable to have the telegram bot always available to make people try it.
For this reason, we need to deploy the Telegram Bot. At first, I wanted to use Heroku, which is another closed-source platform to deploy applications, but it doesn't offer a free tier anymore.
So, I chose Fly.io as a cloud service because it provides a free plan and it was well-documented.
- Create requirements.txt and Dockerfile
- Connect to Fly.io
- Launch App
- Deploy App
1. Create requirements.txt and Dockerfile
To put it into production, we need the file requirements.txt
, that contains all the python dependencies. It can be automatically created by running the command line [pipreqs](https://pypi.org/project/pipreqs/)
in your terminal. You must install pipreqs to make the command line work. These are the following python packages required by the project:
google_search_results==2.4.1
pyTelegramBotAPI==4.9.0
python-dotenv==0.21.1
requests==2.28.1
steamship==2.3.5
In addition to this file, we also need another file, called Dockerfile
, which should be like this:
FROM python:3.10.2
WORKDIR /bot
COPY requirements.txt /bot/
RUN pip install -r requirements.txt
RUN apt-get update
RUN apt-get install ffmpeg -y
RUN apt-get install nodejs -y
RUN apt-get install npm -y
COPY . /bot
CMD python bot.py
2. Connect to Fly.io
The third requirement is the installation of flyctl
, a command line that will allow deploying our application. You can find the instructions here, which can be different depending on your operating system.
If you didn't create a Fly.io account yet, you need to create one by running the command line:
flyctl auth signup
In case you already have the account, you only need to sign in by copying on the terminal:
fly auth login
3. Launch App
To get started with this project, we can type the command line:
flyctl launch
It will ask you for the following information:
- Write the app name.
- Select the region for deployment.
- If you want to set up a Postgresql database. In this case, the answer is no.
- If you want to set up an Upstash Redis Database now. In this case, the answer is no.
- If we want to deploy now. The answer is no for now. We'll deploy it later.
Before deploying the app, I also decided to exploit a functionality provided by fly.io, the secrets. As you may know, the credentials are sensitive and you may prefer to not give your secrets to anyone except yourself. This is possible by tying the following command lines:
flyctl secrets set BOT_TOKEN=
flyctl secrets set STEAM_TOKEN=
flyctl secrets set API_KEY=
After running these command lines, the python code needs to be modified. We don't use anymore JSON file with the credentials, which are stored directly on fly.io platform. For example, we can get access to the token of the Telegram bot in this way:
from dotenv import load_dotenv
load_dotenv()
bot = telebot.TeleBot(os.getenv("BOT_TOKEN"))
You need to do the same for the other API keys. You can check the list of the command lines with the command line:
flyctl secrets list
4. Deploy App
Finally, we have reached the top of Everest! Just another command line and it's done:
flyctl deploy
In this way, our docker container with telegram bot is built and deployed. The code will be launched on the cloud, not on the local PC anymore. Now the telegram bot will work
Final Thoughts:
I hope you appreciated this data science project. It can give you ideas for other possible applications. There are a lot of resources available online that reach everything you want. You only need creativity, patience and hard work. If you have any of these ingredients, there are no limits to what you can do. The GitHub code is here. Thanks for reading. Have a nice day!