简体   繁体   English

在 aiogram 电报机器人 function 中使用用户消息

[英]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.在我的机器人中,用户必须输入出生日期,然后我必须将这个日期转移到一个集合 function 中,其结果随后作为索引插入到列表中,相应的图像将从那里发送到用户。 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.现在我将向您展示部分代码,它是处理程序和 function 的示例,我想通过它运行用户的消息。 As long as you use a randomly generated number and not the result from the function, everything works perfectly.只要您使用随机生成的数字而不是 function 的结果,一切都会完美无缺。 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?问题:如何将用户的消息分配给 aiogram 中的变量以便与其进一步交互?

@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.您可以使用 function message.text 查看用户的消息。

@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!")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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