Не передаеться data в state. aiogram
мне надо получить sale_id, buyer_id в handle_contact_accept. Мучаюсь 2 часа ничего не могу сделать) Current state: None
@dp.callback_query_handler(lambda c: c.data.startswith('contact'), state=SearchForm)
async def sale_info(callback_query: types.CallbackQuery, state: FSMContext):
try:
#await state.finish()
user_id = callback_query.from_user.id
# Извлекаем id продажи из callback_data
sale_id = int(callback_query.data.split('_')[-1])
seller_id = db.get_user_id_sale(sale_id)
language = db.user_language(seller_id)
async with state.proxy() as data:
data["sale_id"] = sale_id
data["buyer_id"] = user_id
await state.update_data(sale_id=sale_id, buyer_id=user_id)
print(data["sale_id"], data["buyer_id"])
keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=True)
if language == "RU":
buttonYes = types.KeyboardButton(text="Согласиться", request_contact=True)
buttonNo = types.KeyboardButton(text="Отказаться", callback_data=f"decline_{user_id}")
else:
buttonYes = types.KeyboardButton(text="Погодитися", request_contact=True)
buttonNo = types.KeyboardButton(text="Відмовитися", callback_data=f"decline_{user_id}{sale_id}")
keyboard.add(buttonYes, buttonNo)
if language == 'RU':
await bot.send_message(seller_id, text='С вами хотят связаться насчёт продажи/обмена!', reply_markup=keyboard)
else:
await bot.send_message(seller_id, text="З вами хочуть зв'язатися щодо продажу/обміну!", reply_markup=keyboard)
language = db.user_language(user_id)
if language == "RU":
await bot.send_message(user_id,
text="Заявка на общение успешно отправлена! Вам прийдет контакт пользователя после того как он подтверит общение с вами.")
else:
await bot.send_message(user_id,
text="Заявку на спілкування успішно надіслано! Вам прийде контакт користувача після того, як він підтвердить спілкування з вами.")
except ValueError as E:
print(f"{E}")
@dp.message_handler(content_types=types.ContentType.CONTACT)#,state=SearchForm.Contact)
async def handle_contact_accept(message: types.Message, state: FSMContext):
current_state = await state.get_state()
print("Current state:", current_state)
try:
user_id = message.from_user.id
language = db.user_language(user_id)
# Извлекаем id продажи из callback_data
data = await state.get_data()
print(f"data: {data}")
sale_id = data['sale_id']
buyer_id = data['buyer_id']
title = db.get_sale_info(sale_id)
print("Sale ID:", sale_id)
print("Buyer ID:", buyer_id)
print(title)
if message.contact:
contact = message.contact
phone_number = contact.phone_number
first_name = contact.first_name
last_name = contact.last_name if contact.last_name else None
if language == "RU":
await bot.send_message(buyer_id, text=f"Контакт пользователя с объявления {title[2]}")
await bot.send_contact(buyer_id, phone_number, first_name, last_name)
else:
await bot.send_message(buyer_id, text=f"Контакт користувача з оголошення {title[2]}")
await bot.send_contact(buyer_id, phone_number, first_name, last_name)
else:
print("error")
except ValueError as e:
print(f"{e}")