简体   繁体   中英

Using user messages in the aiogram telegram bot function

In my bot, the user has to enter the date of birth, then I have to transfer this date into a set function, the result of which is later inserted as an index into a list, from where the corresponding image will be sent to the user. Now I will show you a part of the code, it's an example of both the handler and the function, through which I want to run the user's message. As long as you use a randomly generated number and not the result from the function, everything works perfectly. The thing is that I need exactly the result you get through the calculations and for that I need to insert the message sent by the user into a variable, so that you could continue working with it - and that's exactly what I fail to accomplish. The Question: how do you assign the user's message to a variable in aiogram in order to further interact with it?

@dp.message_handler(commands=['start'])
async def begin(message: types.Message):
    await bot.send_message(message.from_user.id, f"{text1}")


@dp.message_handler()
async def datarozh(message: types.Message):
    if message.text[2] == '/' and message.text[5] == '/' and len(message.text) == 10:
        await bot.send_message(message.from_user.id, f"blablabla", reply_markup=mainmenu)

    elif message.text == 'Begin':
        chatid = message.from_user.id
        arkankart = InputFile(path_or_bytesio=f"{img_list[result_def]}") # this is where you need to substitute the value from the function after processing the user's message
        await dp.bot.send_photo(chat_id=chatid, photo=arkankart, caption='blablabla')


data = "11/11/2011" # the plan is to use the user's message instead of data
s = data.split('/')
lisst = []
for i in s:
    lisst.append(int(i))

def day(a): # I have several different variations of the functions through which I would run the user's message, here's one of them
    result = 0
    while a[0] != 0:
        b = a[0] % 10
        result += b
        a[0] = a[0] // 10
    return result

You can use function message.text to check the user's message.

@dp.message_handler()
async def send_answer(message: types.Message):
   if message.text == "Test1":
      print("Cool!")

So if you would to assign user's message to a variable:

@dp.message_handler()
async def send_answer(message: types.Message):
   your_variable = message.text
   if your_variable == "Test1":
      print("Cool!")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM