import json
import time
import user_params
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup, CallbackQuery, ReplyKeyboardMarkup, \
KeyboardButton
from telegram.ext import CallbackContext, ContextTypes
from telegram.error import BadRequest
from user_data_storage import load_user_data, create_connection
def load_user_data(user_id):
conn = create_connection()
cursor = conn.cursor()
cursor.execute("SELECT data FROM user_data WHERE user_id=?", (user_id,))
data_json = cursor.fetchone()
conn.close()
if data_json:
return json.loads(data_json[0])
else:
return None
def update_callback_data(menu, callback_data_to_add):
updated_keyboard = []
if isinstance(menu, ReplyKeyboardMarkup):
for row in menu.keyboard:
updated_row = []
for button in row:
if isinstance(button, KeyboardButton):
updated_row.append(KeyboardButton(button.text + callback_data_to_add))
else:
updated_row.append(button + callback_data_to_add)
updated_keyboard.append(updated_row)
return ReplyKeyboardMarkup(updated_keyboard)
elif isinstance(menu, InlineKeyboardMarkup):
for row in menu.inline_keyboard:
updated_row = []
for button in row:
if hasattr(button, 'callback_data') and button.callback_data:
new_callback_data = button.callback_data + callback_data_to_add
else:
new_callback_data = None
updated_row.append(InlineKeyboardButton(button.text, callback_data=new_callback_data))
updated_keyboard.append(updated_row)
return InlineKeyboardMarkup(updated_keyboard)
else:
raise ValueError("Invalid menu type")
def check_subscription(chat_id: int, channel_username: str, bot, required_status: str = None) -> bool:
try:
member = bot.get_chat_member(chat_id=channel_username, user_id=chat_id)
allowed_statuses = ['member', 'creator', 'administrator']
if required_status:
allowed_statuses = [required_status]
if member.status in allowed_statuses:
return True
except:
pass
return False
def check_and_start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
global query
if update.callback_query:
query = update.callback_query
chat_id = query.message.chat_id
# Обработка callback_data "enter_data"
if query.data == "enter_data":
return user_params.user_params_start(update, context)
else:
chat_id = update.effective_chat.id
channel_username = "@name"
if check_subscription(chat_id, channel_username, context.bot):
if 'gender' not in context.user_data:
start(update, context, chat_id, query=query if update.callback_query else None)
else:
from menu_handlers import show_main_menu
show_main_menu(update, context)
if update.callback_query:
context.bot.delete_message(chat_id, query.message.message_id)
else:
if update.callback_query:
query.answer("Пожалуйста, сначала подпишитесь на канал.")
show_subscribe_message_start(update, context)
async def show_subscribe_message_start(update: Update, context: ContextTypes.DEFAULT_TYPE):
keyboard = [
[InlineKeyboardButton("Подписаться", url="https://t.me/name")],
[InlineKeyboardButton("Продолжить", callback_data="check_and_start")],
]
reply_markup = InlineKeyboardMarkup(keyboard)
message_text = "Пожалуйста, подпишитесь на наш канал @Name"
# Удаление нижнего меню перед показом сообщения о подписке
if "menu_message_id" in context.user_data:
try:
await context.bot.delete_message(chat_id=update.effective_chat.id,
message_id=context.user_data["menu_message_id"])
except BadRequest:
pass
if update.message:
message = await update.message.reply_text(message_text, reply_markup=reply_markup)
else:
current_text = update.callback_query.message.text
if current_text != message_text:
message = await update.callback_query.edit_message_text(message_text, reply_markup=reply_markup)
else:
await update.callback_query.answer("Вы уже подписаны на канал или сообщение не было изменено.")
return
context.user_data['subscribe_message_id'] = message.message_id
def start(update: Update, context: CallbackContext, chat_id: int = None, message_text: str = None, query: CallbackQuery = None) -> None:
if chat_id is None:
chat_id = update.effective_chat.id
user_data = load_user_data(chat_id)
# Если данные пользователя уже существуют, показываем сообщение и выходим из функции
if user_data:
update.message.reply_text("Вы уже начали использовать этот бот. Если хотите сбросить данные, используйте команду /restart.")
return
context.user_data['user_id'] = chat_id
if message_text is None:
message_text = "текст"
channel_username = "@Name"
if not check_subscription(chat_id, channel_username, context.bot):
show_subscribe_message_start(update, context)
return
keyboard = [
[InlineKeyboardButton("кнопка1", url="https://example.com")],
[InlineKeyboardButton("кнопка2", url="https://example.com")],
]
reply_markup = InlineKeyboardMarkup(keyboard)
keyboard = update_callback_data(InlineKeyboardMarkup(keyboard), "check_and_start")
if query:
sent_message = query.edit_message_text(text=message_text, reply_markup=reply_markup)
new_update = Update(update.update_id, message=sent_message)
else:
sent_message = context.bot.send_message(chat_id, message_text, reply_markup=reply_markup)
new_update = update
# Добавление задержки перед вызовом функции user_params_start
time.sleep(0.3)
return user_params.user_params_start(new_update, context)