async def enter_price(message: types.Message, state: FSMContext): data = await state.get_data() item = data.get("item") try: price = int(message.text) except ValueError: await message.answer(_("Incorrect value, input for example: 100000")) return item.price = price await message.answer(_("Price:{price:.2f}\n" "Confirm ? Click /cancel for decline operation").format(price=item.price), reply_markup=item_confirm) await state.update_data(item=item) await NewItem.Confirm.set()
async def confirm_price(call: CallbackQuery, state: FSMContext): await call.message.edit_reply_markup() data = await state.get_data() item = data.get("item") await item.create() await call.message.answer(_("Item was created")) await state.finish()
async def change_purchase(call: CallbackQuery, state: FSMContext): await call.message.edit_reply_markup() data = await state.get_data() purchase = data.get("purchase") item = data.get("item") await purchase.create() await call.message.answer( _("Allright. Pay <b>{amount:,}</b> in one of methods below\n" "Click on button to accept").format(amount=purchase.amount / 100)) currency = "UAH" need_name = True need_phone_number = False need_email = False need_shipping_address = True await bot.send_invoice( chat_id=call.from_user.id, title=item.name, description=item.name, payload=str(purchase.id), start_parameter=str(purchase.id), currency=currency, prices=[LabeledPrice(label=item.name, amount=purchase.amount)], provider_token=PAYMENT_TOKEN, need_email=need_email, need_name=need_name, need_phone_number=need_phone_number, need_shipping_address=need_shipping_address) await state.update_data(purchase=purchase) await states.Purchase.Payment.set()
async def enter_text(message: types.Message, state: FSMContext): text = message.text await state.update_data(text=text) await message.answer(_("In what language you do you want to send mailing?\n" "Text:\n" "{text}").format(text=text), reply_markup=mailing_buttons) await Mailing.Language.set()
async def buying_item(call: CallbackQuery, callback_data: dict, state: FSMContext): item_id = int(callback_data.get("item_id")) await call.message.edit_reply_markup() item = await Item.get(item_id) if not item: await call.message.answer(_("This item not exists")) return text = _( "Do you want to buy this item?\"<b>{name}</b>\" by price {price:,}\n" "Input count of items which you want to buy or click /cancel").format( name=item.name, price=item.price / 100) await call.message.answer(text) await states.Purchase.EnterQuantity.set() await state.update_data(item=item, purchase=Purchase(item_id=item_id, purchases=datetime.now(), receiver=call.from_user.id))
async def query(query: PreCheckoutQuery, state: FSMContext): await bot.answer_pre_checkout_query(query.id, ok=True) data = await state.get_data() purchase: Purchase = data.get('purchase') success = await check_payment(purchase) if success: await purchase.update( succefull=True, shipping_address=query.order_info.shipping_address.to_python() if query.order_info.shipping_address else None, phone_number=query.order_info.phone_number, receiver=query.order_info.name, email=query.order_info.email).appply() await state.reset_state() await bot.send_message(chat_id=query.from_user.id, text=_("Thank you for purchase!")) else: await bot.send_message( chat_id=query.from_user.id, text=_("Purchase not complete, try again later"))
async def enter_name(message: types.Message, state: FSMContext): name = message.text item = Item() item.name = name await message.answer(_("Name of product: {name}\n" "Send me photo of product(no document) or click /cancel").format( name=name ) ) await NewItem.Photo.set() await state.update_data(item=item)
async def add_photo(message: types.Message, state: FSMContext): photo = message.photo[-1].file_id data = await state.get_data() item = data.get("item") item.photo = photo await message.answer_photo( photo=photo, caption=_("Product name: {name}" "\nSend me product price in coins or click /cancel").format(name=item.name) ) await NewItem.Price.set() await state.update_data(item=item)
async def choose_language(call: CallbackQuery, state: FSMContext): data = await state.get_data() text = data.get("text") await state.reset_state() await call.message.edit_reply_markup() users = await User.query.where(User.language == call.data).gino.all() for user in users: try: await bot.send_message(chat_id=user.user_id, text=text) await sleep(0.3) except Exception: pass await call.message.answer(_("Mailing successfully ended"))
async def show_items(message: types.Message): all_items = await db_commands.show_items() text = _("<b>Item</b> \t№{id}: <u>{name}</u>\n" "<b>Price:</b> \t{price:,}\n") for item in all_items: buy_button = InlineKeyboardMarkup(inline_keyboard=[[ InlineKeyboardButton(text="Buy", callback_data=buy_item.new(item_id=item.id)) ]]) await message.answer_photo(photo=item.photo, caption=text.format(id=item.id, name=item.name, price=item.price / 100), reply_markup=buy_button) await sleep(0.3)
async def enter_quantity(message: types.Message, state: FSMContext): quantity = int(message.text) async with state.proxy() as data: data["purchase"].quantity = quantity item = data["item"] amount = item.price * quantity data["purchase"].amount = amount purchase = data["purchase"] await message.answer(_( "Okey, you want to buy <i>{quantity}</i> {name} by price <b>{price:,}/item</b>\n" "It will be <b>{amount:,}</b>. Accepted?").format( quantity=quantity, name=item.name, amount=amount / 100, price=item.price / 100), reply_markup=quantity_buttons) await state.update_data(purchase=purchase, item=item) await states.Purchase.Approval.set()
async def bot_start(message: types.Message): global user chat_id = message.from_user.id referral = message.get_args() try: user = await commands.add_new_user(id=chat_id, referral=referral) except asyncpg.exceptions.UniqueViolationError: pass id = user.user_id bot_username = (await bot.me).username bot_link = f"https://t.me/{bot_username}?start={id}" count_users = await commands.count_users() text = _("Welcome\n" "Now in base {count_users} users\n" "Your referral link: {bot_link}" "Check referrals command /referrals\n" "Items list: /items").format(count_users=count_users, bot_link=bot_link) await message.answer(text, reply_markup=languages_markup)
async def check_referrals(message: types.Message): referrals = await commands.check_referrals() text = _("Your referrals: {referrals}").format(referrals=referrals) await message.answer(text)
async def choose_language(call: CallbackQuery): await call.message.edit_reply_markup() lang = call.data[-2:] await commands.set_language(language=lang) await call.message.answer(_("Your language was changed.", locale=lang))
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton from middlewares.language_packs_middleware import _ quantity_buttons = InlineKeyboardMarkup(inline_keyboard=[[ InlineKeyboardButton(text=_("I'm agree"), callback_data="agree") ], [ InlineKeyboardButton(text=_("Change quantity"), callback_data="change") ], [InlineKeyboardButton(text=_("Cancel buying"), callback_data="cancel")]])
async def change_purchase(call: CallbackQuery, state: FSMContext): await call.message.edit_reply_markup() await call.message.answer(_("Re-enter the quantity")) await states.Purchase.EnterQuantity.set()
async def cancel_purchase(call: CallbackQuery, state: FSMContext): await call.message.edit_reply_markup() await call.message.answer(_("We cancel this purchase")) await state.reset_state()
async def wrong_quantity(message: types.Message): await message.answer(_("Wrong value, input number"))
async def add_item(message: types.Message): await message.answer(_("Input item name or click /cancel")) await NewItem.Name.set()
async def change_price(call: CallbackQuery, state: FSMContext): await call.message.edit_reply_markup() await call.message.answer(_("If you want to change price\n" "Input price in coins format")) await NewItem.Price.set()
async def cancel(message: types.Message, state: FSMContext): await message.answer(_("You decline creating of item")) await state.reset_state()
async def mailing(message: types.Message): await message.answer(_("Send text of mailing")) await Mailing.Text.set()
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton from middlewares.language_packs_middleware import _ item_confirm = InlineKeyboardMarkup(inline_keyboard=[[ InlineKeyboardButton(text=_("Confirm"), callback_data="confirm"), InlineKeyboardButton(text=_("Change"), callback_data="change") ]])