From 2909ce8585575eb46e080a6d8e28754b6479c061 Mon Sep 17 00:00:00 2001 From: Redsandyg Date: Sun, 15 Jun 2025 17:03:38 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A3=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=D1=8B=20?= =?UTF-8?q?=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B8=20=D0=B8=20=D1=81?= =?UTF-8?q?=D0=BE=D1=81=D1=82=D0=BE=D1=8F=D0=BD=D0=B8=D1=8F,=20=D1=81?= =?UTF-8?q?=D0=B2=D1=8F=D0=B7=D0=B0=D0=BD=D0=BD=D1=8B=D0=B5=20=D1=81=20?= =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=BC=D0=BE=D0=BA=D0=BE=D0=B4=D0=B0=D0=BC?= =?UTF-8?q?=D0=B8,=20=D0=B0=20=D1=82=D0=B0=D0=BA=D0=B6=D0=B5=20=D0=BE?= =?UTF-8?q?=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5=D0=BD=20=D0=B8=D0=BD=D1=82?= =?UTF-8?q?=D0=B5=D1=80=D1=84=D0=B5=D0=B9=D1=81=20=D0=B4=D0=BB=D1=8F=20?= =?UTF-8?q?=D0=BE=D1=82=D0=BE=D0=B1=D1=80=D0=B0=D0=B6=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=20=D1=81=D1=81=D1=8B=D0=BB=D0=BE=D0=BA=20=D1=81=20=D1=83?= =?UTF-8?q?=D1=87=D0=B5=D1=82=D0=BE=D0=BC=20=D0=BF=D1=80=D0=BE=D0=BC=D0=BE?= =?UTF-8?q?=D0=BA=D0=BE=D0=B4=D0=BE=D0=B2.=20=D0=98=D1=81=D0=BF=D1=80?= =?UTF-8?q?=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D1=8B=20=D1=84=D0=BE=D1=80=D0=BC?= =?UTF-8?q?=D0=B0=D1=82=D1=8B=20=D0=B2=D1=8B=D0=B2=D0=BE=D0=B4=D0=B0=20?= =?UTF-8?q?=D0=B8=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D1=8B?= =?UTF-8?q?=20=D0=BD=D0=BE=D0=B2=D1=8B=D0=B5=20=D0=BF=D0=BE=D0=BB=D1=8F=20?= =?UTF-8?q?=D0=B2=20=D1=81=D0=BE=D0=BE=D0=B1=D1=89=D0=B5=D0=BD=D0=B8=D1=8F?= =?UTF-8?q?=20=D0=BE=D0=B1=20=D1=83=D1=81=D0=BF=D0=B5=D1=88=D0=BD=D0=BE?= =?UTF-8?q?=D0=BC=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8=D0=B8=20?= =?UTF-8?q?=D1=81=D1=81=D1=8B=D0=BB=D0=BE=D0=BA.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 133 +++----------------------------------------------------- 1 file changed, 7 insertions(+), 126 deletions(-) diff --git a/main.py b/main.py index 00772a7..b7e6bd5 100644 --- a/main.py +++ b/main.py @@ -47,10 +47,7 @@ main_keyboard = InlineKeyboardMarkup(inline_keyboard=[ [ InlineKeyboardButton(text='Моя статистика', callback_data='stats'), InlineKeyboardButton(text='Создать заявку на вывод средств', callback_data='withdraw') - ], - [ - InlineKeyboardButton(text='Мои промокоды', callback_data='promocodes') - ], + ] ]) # Вместо загрузки из файла будем хранить кэш токенов пользователей @@ -85,14 +82,15 @@ async def render_links_page(page: int = 0, token: str = None): start = page * LINKS_PER_PAGE end = start + LINKS_PER_PAGE page_links = links[start:end] - header = f"{'ref':<36} | description" - sep = '-' * 36 + '-|-' + '-' * 30 + header = f"{'ref':<36} | {'promocode':<10} | description" + sep = '-' * 36 + '-|-' + '-' * 10 + '-|-' + '-' * 30 rows = [] for item in page_links: ref = item['ref'] + promocode = item.get('promocode', '') description = item['description'] url = f"https://{item['ref']}" - rows.append(f"{ref:<36} | {description}") + rows.append(f"{ref:<36} | {promocode:<10} | {description}") if not rows: table = 'Нет ссылок.' else: @@ -181,10 +179,6 @@ class LinkStates(StatesGroup): class WithdrawStates(StatesGroup): waiting_for_amount = State() -class PromoCodeStates(StatesGroup): - waiting_for_description = State() - promocode_created = State() - @dp.callback_query(F.data == 'create_link') async def create_link(callback: types.CallbackQuery, state: FSMContext): sent = await callback.message.edit_text( @@ -217,7 +211,6 @@ async def process_new_link_description(message: types.Message, state: FSMContext headers = {"Authorization": f"Bearer {token}"} data = await state.get_data() desc_msg_id = data.get('desc_msg_id') - # Убираем кнопки у сообщения с просьбой ввести описание if desc_msg_id: try: await message.bot.edit_message_reply_markup( @@ -232,7 +225,8 @@ async def process_new_link_description(message: types.Message, state: FSMContext if resp.status == 200: data = await resp.json() ref = data.get('ref') - text = f"Новая ссылка успешно создана!\n\n
ref: {ref}\ndescription: {description}
" + promocode = data.get('promocode') + text = f"Новая ссылка успешно создана!\n\n
ref: {ref}\npromocode: {promocode}\ndescription: {description}
" keyboard = InlineKeyboardMarkup( inline_keyboard=[[InlineKeyboardButton(text='Назад', callback_data='back_to_links_from_success')]] ) @@ -437,119 +431,6 @@ async def paginate_stats(callback: types.CallbackQuery): await callback.message.edit_text(text, reply_markup=keyboard, parse_mode='HTML', disable_web_page_preview=True) await callback.answer() -async def get_promocodes_from_api(token: str): - headers = {"Authorization": f"Bearer {token}"} - async with aiohttp.ClientSession() as session: - async with session.get(f'{API_URL}/promocode', headers=headers) as resp: - if resp.status == 200: - return await resp.json() - return [] - -async def create_promocode_from_api(token: str): - headers = {"Authorization": f"Bearer {token}"} - async with aiohttp.ClientSession() as session: - async with session.post(f'{API_URL}/promocode/add', headers=headers) as resp: - if resp.status == 200: - return await resp.json() - return None - -@dp.callback_query(F.data == 'promocodes') -async def show_promocodes(callback: types.CallbackQuery): - tg_id = callback.from_user.id - token = user_tokens.get(tg_id) - promocodes = await get_promocodes_from_api(token) - if not promocodes: - text = 'У вас пока нет промокодов.' - else: - # Форматирование таблицы с фиксированной шириной колонок - header = f"{'Промокод':<12} | {'%':<5} | {'description':<24}" - sep = '-' * 12 + '-|-' + '-' * 5 + '-|-' + '-' * 24 - rows = [] - for p in promocodes: - promocode = str(p['promocode'])[:12] - perc = str(p['perc'])[:5] - desc = (p.get('description') or '') - if len(desc) > 24: - desc = desc[:21] + '...' - rows.append(f"{promocode:<12} | {perc:<5} | {desc:<24}") - table = '\n'.join([header, sep] + rows) - text = f"Ваши промокоды:\n
{table}
" - keyboard = InlineKeyboardMarkup( - inline_keyboard=[ - [InlineKeyboardButton(text='Создать промокод', callback_data='create_promocode')], - [InlineKeyboardButton(text='Назад', callback_data='back_to_main')] - ] - ) - await callback.message.edit_text(text, reply_markup=keyboard, parse_mode='HTML') - await callback.answer() - -@dp.callback_query(F.data == 'create_promocode') -async def create_promocode(callback: types.CallbackQuery, state: FSMContext): - sent = await callback.message.edit_text( - 'Введите описание для нового промокода:', - reply_markup=InlineKeyboardMarkup( - inline_keyboard=[[InlineKeyboardButton(text='Назад', callback_data='back_to_promocodes')]] - ) - ) - await state.set_state(PromoCodeStates.waiting_for_description) - await state.update_data(desc_msg_id=sent.message_id) - await callback.answer() - -@dp.callback_query(F.data == 'back_to_promocodes', PromoCodeStates.waiting_for_description) -async def back_to_promocodes_from_create(callback: types.CallbackQuery, state: FSMContext): - tg_id = callback.from_user.id - token = user_tokens.get(tg_id) - promocodes = await get_promocodes_from_api(token) - if not promocodes: - text = 'У вас пока нет промокодов.' - else: - header = f"{'Промокод':<12} | % | description" - sep = '-' * 12 + '-|-' + '-' * 3 + '-|-' + '-' * 30 - rows = [f"{p['promocode']:<12} | {p['perc']} | {p.get('description','')[:30]}" for p in promocodes] - table = '\n'.join([header, sep] + rows) - text = f"Ваши промокоды:\n
{table}
" - keyboard = InlineKeyboardMarkup( - inline_keyboard=[ - [InlineKeyboardButton(text='Создать промокод', callback_data='create_promocode')], - [InlineKeyboardButton(text='Назад', callback_data='back_to_main')] - ] - ) - await callback.message.edit_text(text, reply_markup=keyboard, parse_mode='HTML') - await state.clear() - await callback.answer() - -@dp.message(PromoCodeStates.waiting_for_description) -async def process_new_promocode_description(message: types.Message, state: FSMContext): - description = message.text.strip() - tg_id = message.from_user.id - token = user_tokens.get(tg_id) - data = await state.get_data() - desc_msg_id = data.get('desc_msg_id') - if desc_msg_id: - try: - await message.bot.edit_message_reply_markup( - chat_id=message.chat.id, - message_id=desc_msg_id, - reply_markup=None - ) - except Exception: - pass - headers = {"Authorization": f"Bearer {token}"} - async with aiohttp.ClientSession() as session: - async with session.post(f'{API_URL}/promocode/add', json={"description": description}, headers=headers) as resp: - if resp.status == 200: - data = await resp.json() - promocode = data.get('promocode') - perc = data.get('perc') - text = f"Промокод успешно создан!\n
promocode: {promocode}\ndescription: {description}\nperc: {perc}
" - keyboard = InlineKeyboardMarkup( - inline_keyboard=[[InlineKeyboardButton(text='Назад к промокодам', callback_data='promocodes')]] - ) - await message.answer(text, reply_markup=keyboard, parse_mode='HTML') - await state.set_state(PromoCodeStates.promocode_created) - else: - await message.answer('Ошибка при создании промокода. Попробуйте еще раз.') - async def main(): await dp.start_polling(bot)