DEV Community

Hasindu Nagolla
Hasindu Nagolla

Posted on

Building a Lag-Free Telegram Music Bot for Concurrent Streams in Python

Hey DEV community! 👋

Building a simple Telegram bot is fun, but building a music streaming bot that streams audio to many voice chats at the same time is a completely different challenge.

For the past year, I have been developing HasiiMusicBot, an open-source Telegram music player that focuses on performance and scalability. I build it to solve many of the problems that existing music bots face.

🏗️ Tech Stack

  • Framework: Pyrogram (MTProto API)
  • Voice Streaming: PyTgCalls
  • Media Processing: FFmpeg & yt-dlp
  • Database: MongoDB with Motor
  • Deployment: Docker & Docker Compose

🧗‍♂️ Challenges I Solve

1. Handling Multiple Streams Without Lag

Many Python music bots have a problem with blocking tasks like downloading audio or generating thumbnails. These tasks can freeze the event loop and cause audio interruptions in all active voice chats.

I redesigned the bot to use non-blocking asynchronous tasks. This keeps the event loop free and allows the bot to handle concurrent streams smoothly with no audio drops.

2. Handling yt-dlp Anti-Bot Restrictions

Many developers now face the "Sign in to confirm you're not a bot" error when using yt-dlp with YouTube.

To reduce this issue, I improved the metadata extraction process and add secure cookie handling. This helps the bot fetch high-quality audio streams more reliably.

🤝 Open Source and Contributions

The project is now available under the GPL-3.0 License and is completely open source.

If you are interested in Python, audio streaming, or Telegram bots, please check out the project.

🔗 GitHub Repository: https://github.com/hasindu-nagolla/HasiiMusicBot

I welcome feedback, code reviews, and contributions. I also added some good first issue labels for beginners who want to make their first open-source contribution.

If you like the project, please give it a ⭐ and let me know your thoughts in the comments!

Hasii Music Bot

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.