Объединение в PyTelegramBotAPI

Рейтинг: 0Ответов: 0Опубликовано: 27.04.2023

В коде есть две функции обработчика сообщений с типом "text", которые обе реагируют на любой текст, поэтому вторая из этих функций перезаписывает первую, что приводит к тому, что обработчик команды "/timer" не работает.

Как это исправить?

Вот код

import random
import time
from telebot import types

TOKEN = "Тут токен"
bot = telebot.TeleBot(TOKEN)

@bot.message_handler(commands=["start"])
def start(message):
    bot.send_message(message.chat.id, "Бот успешно работает")


@bot.message_handler(commands=["help"])
def help(message):
    bot.send_message(message.chat.id, " /game - игра /timer - таймер на 5 мин. /send_file - бот вам скинет фото/видео/гиифку/стикер")


@bot.message_handler(commands=["game"])
def game(message):
    keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True)

    keyboard.row("камень")
    keyboard.row("ножницы")
    keyboard.row("бумага")
    
    bot.send_message(message.chat.id, "Выберите камень, ножницы, бумага",reply_markup=keyboard)

    
@bot.message_handler(content_types=["text"])
def text(message):
    
    player = message.text

    if message.text in ["камень", "ножницы", "бумага"]:
        bot.reply_to(message,"Вы выбрали свой ход")

    botnumber = random.randint(1,3)

    if botnumber == 1:
       botnumber = 'камень'
    elif botnumber == 2:
       botnumber = 'ножницы'
    elif botnumber == 3:
        botnumber = 'бумага'

    if player == botnumber:
        bot.send_message(message.chat.id, "Ничья")
    elif (player == "камень" and botnumber == "ножницы") or (player == "ножницы" and botnumber == "бумага") or (player == "бумага" and botnumber == "камень"):
        bot.send_message(message.chat.id,"Ты выиграл")
    else:
        bot.send_message(message.chat.id,"Бот выиграл")


@bot.message_handler(commands=["timer"])
def timer(message):
    keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True)
    keyboard.add("Таймер")

    bot.send_message(message.chat.id, "чтоб запустить таймер на 5 мин, нажмите на кнопку:", reply_markup=keyboard)

@bot.message_handler(content_types=["text"])
def timer(message):
        bot.send_message(message.chat.id, "Запущен таймер на 5 минут!")
        for minute in range(1, 5):
            time.sleep(60)
            bot.send_message(message.chat.id, f"Осталось {5 - minute} минут...")
        else:
            time.sleep(60)
            bot.send_message(message.chat.id, "Время вышло!")

@bot.message_handler(commands=["send_file"])
def send_file(message):
    keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True)
    keyboard.add("Фото", "Видео")
    keyboard.add("Гифка", "Стикер")

    bot.send_message(message.chat.id, "Что мне надо прислать?:", reply_markup=keyboard)


@bot.message_handler(content_types=["text"])
def text(message):
    txt = message.text
    if txt == "Фото":
        photo = random.randint(1,3)
        if photo == 1:
            bot.send_photo(message.chat.id, "https://randomwordgenerator.com/img/picture-generator/54e5d2444253af14f1dc8460962e33791c3ad6e04e50744172287ad1954ec7_640.jpg")
        if photo == 2:
            bot.send_photo(message.chat.id, "https://images.unsplash.com/photo-1681375020549-b6c39cb7cd51?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwxODY2Nzh8MHwxfHJhbmRvbXx8fHx8fHx8fDE2ODI2MDA5NjI&ixlib=rb-4.0.3&q=80&w=1080")
        if photo == 3:
            bot.send_photo(message.chat.id, "https://images.unsplash.com/photo-1680540790093-2640b5cd2e66?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwxODY2Nzh8MHwxfHJhbmRvbXx8fHx8fHx8fDE2ODI2MDExMzQ&ixlib=rb-4.0.3&q=80&w=1080")
    if txt == "Видео":
        video = random.randint(1,3)
        if video == 1:
            bot.send_message(message.chat.id, "https://www.youtube.com/watch?v=EyrKPyEy1EY&ab_channel=pokernews")
        if video == 2:
            bot.send_message(message.chat.id, "https://www.youtube.com/watch?v=gVYO1eMwk9s")
        if video == 3:
            bot.send_message(message.chat.id, "https://www.youtube.com/watch?v=RL21JIWp4VM")
    if txt == "Гифка":
        gif = random.randint(1,3)
        if gif == 1:
            bot.send_animation(message.chat.id, "https://media1.giphy.com/media/IcifS1qG3YFlS/giphy.gif")
        if gif == 2:
            bot.send_animation(message.chat.id, "https://media2.giphy.com/media/KZeKpFsTB3uGntA3f5/giphy.webp")
        if gif == 3:
            bot.send_animation(message.chat.id, "https://media1.giphy.com/media/L1k7riupZqEoveEv6c/giphy.gif?cid=ecf05e4769d7ops7c93xomqz3ff1rc3hakodl2seswuglebl&ep=v1_gifs_search&rid=giphy.gif&ct=g")
    if txt == "Стикер":
        sticker = random.randint(1,3)
        if sticker == 1:
            bot.send_sticker(message.chat.id, "CAACAgIAAxkBAAEF0MJjH1NHpjSYXrEQCV_5oerUz5zmCwACTgEAAmWiAyxAShoLjRN4jikE")
        if sticker == 2:
            bot.send_sticker(message.chat.id, "CAACAgIAAxkBAAEIv-lkSnqM9UoY130cd3-c9fXME7m52gACngsAAhLrSErR-dIaq3X2tC8E")
        if sticker == 3:
            bot.send_sticker(message.chat.id, "CAACAgIAAxkBAAEIv-tkSnt7C5vYyh8RqdX5_GEebruDPQACGhQAAnWRSUp13pGoL32-5S8E")



bot.polling(non_stop=True)

Ответы

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