Как подставить значение в ключе?

data = { "shop_id": 'sTpzaX4dHLNQ6VC', "amount": ''

}

Нужно чтобы пользователь вводил свое значение, в переписке с ботом.

После того как он его ввел, должен сгенерироваться счет с той суммой, которую он указал

Бот написан на aiogram


Ответы (1 шт):

Автор решения: Vladislav

Для этого вы можете использовать Finite state machine пример из документации

Даю вам пример запроса суммы у пользователя для создания ссылки для оплаты

Вместо Issue the bill for the specified amount вставьте код для генерации ссылки на оплату используя API вашего платежного провайдера и отправьте ссылку на оплату

Таким образом вы можете запрашивать разные данные у пользователя и сохранять их используя update_data(), и получать значения с помощью get_data()

import aiogram
from aiogram.dispatcher import FSMContext
from aiogram.dispatcher.filters.state import State, StatesGroup
from aiogram.contrib.fsm_storage.memory import MemoryStorage

# Define the states for the state machine
class Billing(StatesGroup):
    enter_amount = State()

# Create the state machine and pass it the storage type
storage = MemoryStorage()
billing_machine = aiogram.dispatcher.FSMContext(storage=storage)

# Define a handler that is triggered when the bot receives a message
@dp.message_handler(commands=['issue_bill'], state=None)
async def issue_bill(message: types.Message):
    # Ask the user to enter the amount for the bill
    await message.reply("Please enter the amount for the bill:")
    # Transition to the 'enter_amount' state
    await Billing.enter_amount.set()

# Define a handler for when the user enters the amount for the bill
@dp.message_handler(state=Billing.enter_amount)
async def process_amount(message: types.Message, state: FSMContext):
    # Get the entered amount from the user's message
    amount = message.text
    # Check if the amount is valid
    if amount.isdigit():
        # Save the amount in the state machine
        await state.update_data(amount=amount)
        # Issue the bill for the specified amount
        await message.reply(f"A bill for ${amount} has been issued.")
        # Reset the state machine
        await state.finish()
    else:
        # If the amount is not valid, ask the user to try again
        await message.reply("Please enter a valid amount for the bill:")
→ Ссылка