@Benjiamin01

Почему бот телеграмма завершается сам по себе?

Код бота
import telebot
# Create a bot object
bot = telebot.TeleBot('<TELEGRAM TOKEN>')
# Define a function to greet the user
def greet_user(message):
    # Get the user's name
    name = message.from_user.first_name
    # Send a message to the user
    bot.send_message(message.chat_id, f"Hello {name}!")
# Define a function to get the user's VK ID
def get_vk_id(message):
    # Get the user's message
    text = message.text
    # Extract the VK ID from the message
    vk_id = text.split("/")[-1]
    # Return the VK ID
    return vk_id
# Define a function to send a list of songs to the user
def send_song_list(message, vk_id):
    # Get the user's songs
    songs = get_songs_from_vk(vk_id)
    # Create a message with the list of songs
    message = f"Here is a list of your songs:\n"
    for song in songs:
        message += f"* {song}\n"
    # Send the message to the user
    bot.send_message(message.chat_id, message)
# Define a function to download a song from VK
def download_song(message, song_name):
    # Get the song file from VK
    song_file = get_song_file_from_vk(song_name)
    # Save the song file to the user's device
    save_song_file(song_file, message.chat_id)
# Define a function to get the song file from VK
def get_song_file_from_vk(song_name):
    # Get the song URL from VK
    song_url = get_song_url_from_vk(song__from_vk(song_name))
    # Download the song file
    response = requests.get(song_url)
    song_file = response.content
    # Return the song file
    return song_file
# Define a function to get the song URL from VK
def get_song_url_from_vk(song_name):
    # Get the user's VK ID
    vk_id = get_user_vk_id(message)
    # Get the song URL from VK
    url = f"https://api.vk.com/method/audio.get?owner_id={vk_id}&q={song_name}&access_token=<VK_ACCESS_TOKEN>"
    response = requests.get(url)
    data = response.json()
    song_url = data["response"]["items"][0]["url"]
    # Return the song URL
    return song_url
# Define a function to get the user's VK ID
def get_user_vk_id(message):
    # Get the user's ID from the message
    user_id = message.from_user.id
    # Get the user's VK ID from the database
    vk_id = get_vk_id_from_database(user_id)
    # Return the VK ID
    return vk_id
# Define a function to get the user's songs from VK
def get_songs_from_vk(vk_id):
    # Get the user's songs from VK
    url = f"https://api.vk.com/method/audio.get?owner_id={vk_id}&access_token=<Token VK>"
    response = requests.get(url)
    data = response.json()
    songs = [song["title"] for song in data["response"]["items"]]
    # Return the list of songs
    return songs
# Define a function to save a song file to the user's device
def save_song_file(song_file, chat_id):
    # Save the song file to the user's device
    with open(f"song_{chat_id}.mp3", "wb") as file:
        file.write(song_file)
    # Send the song file to the user
    bot.send_audio(chat_id, open(f"song_{chat_id}.mp3", "rb"))
# Define a function to handlenction to handle the "/start" command
@bot.message_handler(commands=["start"])
def start_command(message):
    # Greet the user
    greet_user(message)
    # Send a message about the bot's functionality
    bot.send_message(message.chat_id, "Welcome to the VK music bot! I can send you your own music from VK. Just send me your VK ID in the format 'https://vk.com/durov/' and I will send you your music.")
    # Add a button to the message
    markup = telebot.types.InlineKeyboardMarkup()
    markup.add(telebot.types.InlineKeyboardButton(text="Send VK ID", callback_data="vk_id"))
    bot.send_message(message.chat_id, "Click the button below to send your VK ID.", reply_markup=markup)
# Define a function to handle the "vk_id" button click
@bot.callback_query_handler(func=lambda call: call.data == "vk_id")
def vk_id_button_click(call):
    # Ask the user to send his VK ID
    bot.send_message(call.message.chat.id, "Please send me your VK ID in the format 'https://vk.com/durov/'")
    # Add a message handler to the user's message
    bot.register_next_step_handler(call.message, get_vk_id_step_2)
# Define a function to handle the user's message after sending his VK ID
def get_vk_id_step_2(message):
    # Get the user's VK ID
    vk_id = get_vk_id(message)
    # Save the user's VK ID to the database
    save_vk_id_to_database(message.from_user.id, vk_id)
    # Send the list of songs to the user
    send_song_list(message, vk_id)
# Define a function to save the user's VK ID to the database
def save_vk_id_to_database(user_id, vk_id):
    # Save the user's VK ID to the database
    # ...


# Start the bot
    bot.polling(none_stop=True)
  • Вопрос задан
  • 81 просмотр
Пригласить эксперта
Ответы на вопрос 3
@sakair
Код странный, но можешь попробовать написать вместо bot.polling() это bot.infinity_polling(), иногда у меня возникает такая проблема. Если не поможет, то, кто то другой поможет.
Ответ написан
Комментировать
Vindicar
@Vindicar
RTFM!
У тебя bot.polling() почему-то с отступом. Как будто он внутри save_vk_id_to_database().
Проверь код ещё раз. Отступы в питоне критичны!
Ответ написан
Комментировать
RimMirK
@RimMirK
Вроде человек. Вроде учусь. Вроде пайтону
ни одного декоратора не вижу. как телебот будет работать без ничего?
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы