Как сделать что бы работал запрос от пользователя, и возвращался ответ // PyTelegramBotApi

Рейтинг: 0Ответов: 0Опубликовано: 25.04.2023
    type_to_name = {
    'track': 'трек',
    'artist': 'исполнитель',
}

@bot.message_handler(commands=['search'])
def search(message):
    chat = message.chat.id
    def send_search_request_and_print_result(query):
        search_result = client.search(query)

        text = [bot.send_message(chat, f'Результаты по запросу "{query}":', '')]

        best_result_text = ''
        if search_result.best:
            type_ = search_result.best.type
            best = search_result.best.result

            text.append(bot.send_message(chat, f'❗️Лучший результат: {type_to_name.get(type_)}'))

            if type_ in ['track', 'podcast_episode']:
                artists = ''
                if best.artists:
                    artists = ' - ' + ', '.join(artist.name for artist in best.artists)
                best_result_text = best.title + artists
            elif type_ == 'artist':
                best_result_text = best.name
            elif type_ in ['album', 'podcast']:
                best_result_text = best.title
            elif type_ == 'playlist':
                best_result_text = best.title
            elif type_ == 'video':
                best_result_text = f'{best.title} {best.text}'

            text.append(bot.send_message(chat, f'Содержимое лучшего результата: {best_result_text}\n'))

        if search_result.artists:
            text.append(bot.send_message(chat, f'Исполнителей: {search_result.artists.total}'))
        if search_result.albums:
            text.append(bot.send_message(chat, f'Альбомов: {search_result.albums.total}'))
        if search_result.tracks:
            text.append(bot.send_message(chat, f'Треков: {search_result.tracks.total}'))
        if search_result.playlists:
            text.append(bot.send_message(chat, f'Плейлистов: {search_result.playlists.total}'))
        if search_result.videos:
            text.append(bot.send_message(chat, f'Видео: {search_result.videos.total}'))

    if __name__ == '__main__':
        while True:
            input_query = bot.send_message(chat, 'Введите поисковой запрос: ')
        bot.register_next_step_handler(message, send_search_request_and_print_result)

Ответы

Ответов пока нет.