Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Telegram bot for searching and downloading songs

Crystaliq

New Coder
You need to create a bot that searches for a song in Spotify, then it should send the finished audio file to the user, and not a link. I roughly understand how to find the file, but to send the audio file it’s not very good, here’s the code I got:
Python:
import logging
from telegram import Update
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
from spotipy import Spotify
from spotipy.oauth2 import SpotifyClientCredentials
 
# лог
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
 
#Spotify API
SPOTIFY_CLIENT_ID = 'client_id'
SPOTIFY_CLIENT_SECRET = 'client_secret'
 
 
sp = Spotify(client_credentials_manager=SpotifyClientCredentials(
    client_id=SPOTIFY_CLIENT_ID,
    client_secret=SPOTIFY_CLIENT_SECRET
))
 
# Поиск
def search_song(query):
    results = sp.search(q=query, type='track', limit=1)
    if results['tracks']['items']:
        track = results['tracks']['items'][0]
        song_name = track['name']
        artist = ', '.join(artist['name'] for artist in track['artists'])
        url = track['external_urls']['spotify']
        return f"������ {song_name} - {artist}\n������ {url}"
    else:
        return "Ненайдено."
 
# /start
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    await update.message.reply_text("Введите название песни.")
 
 
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    user_message = update.message.text
    reply = search_song(user_message)
    await update.message.reply_text(reply)
 
def main():
    # Токен Telegram бота
    TELEGRAM_TOKEN = 'token'
 
 
    application = Application.builder().token(TELEGRAM_TOKEN).build()
 
    # Обробники    application.add_handler(CommandHandler("start", start))
    application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
 
    # Запуск бота
    application.run_polling()
 
if __name__ == '__main__':
    main()
 
Last edited by a moderator:
yk spotify doesnt really allow you to directly download music from their site perhaps thats the reason it isnt working, so you can either provide a link or stream it since you would need licensing for downloading it
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom