N
N
newbie632021-07-06 11:18:05
Python
newbie63, 2021-07-06 11:18:05

Not receiving a number after the command, should the bot send a message?

When someone writes to the bot: "/number 5" the bot replies: "Your number is 5" But if someone writes "/number" then an error pops up because there is no number. How to make the bot write in such a situation: "Enter a number"

@dp.message_handler(commands=['number'])
async def number(message: types.Message):
    number = message.get_args()
    number1 = int(number)*1000
    if int(number) < 0:
        await message.reply("Нельзя вводить отрицательное число")
    else:
        await message.reply(f"Твое число {number1}")

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Krostelev, 2021-07-06
@twistfire92

I suspect that number is set to None so that it can be tied to.
Try this, it might work

@dp.message_handler(commands=['number'])
async def number(message: types.Message):
    number = message.get_args()
    if number is None:
        await message.reply(f"Твое число {number}")
    else:
        await message.reply("Введите число")

B
Bl4ckm45k, 2021-07-07
@Bl4ckm45k

Based on your

code
@dp.message_handler(commands=['number'])
async def number(message: types.Message):
    number = message.get_args()
    number1 = int(number)*1000
    if int(number) < 0:
        await message.reply("Нельзя вводить отрицательное число")
    else:
        await message.reply(f"Твое число {number1}")
which you wrote in a comment to the answer above.
You should check if there is a deeplink and if it is a number
for example
@dp.message_handler(commands=['number'])
async def number(message: types.Message):
    number = message.get_args()
    if number is not None and number.isdigit():
        if int(number) < 0:
            await message.reply("Нельзя вводить отрицательное число")
        else:
            await message.reply(f"Твое число {int(number) * 1000}")
    elif number is None:
        await message.reply(f'Введите число после команды "/number 5 "')
    else:
        await message.reply(f'После команды "/number" ожидается число, а не текст')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question