M
M
matveyplt2021-07-31 17:22:56
Python
matveyplt, 2021-07-31 17:22:56

Why is the split function not working?

I just started to learn coding, so I apologize for the stupid question, but I did not find the answer on the internet

import config
import logging

from aiogram import Bot, Dispatcher, executor, types

logging.basicConfig(level=logging.INFO)

bot = Bot(token=config.TOKEN)
dp = Dispatcher(bot)

@dp.message_handler()

async def addrifm1(message: types.Message):
  mes = types.message
  list = mes.split(",")
  await message.reply(" async def rifm1(message: types.Message):")
  await message.reply("if", list[1], " in message.text:")
  await message.reply("await message.reply(", list[2], ")")


if __name__ == "__main__":
  executor.start_polling(dp, skip_updates=True)

The bot starts without errors, but when I write messages to it, it gives an error in the console
AttributeError: module 'aiogram.types.message' has no attribute 'split'

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
ars_kushaet_mars, 2021-07-31
@ars_kushaet_mars

Try wrapping mes from the line list = mes.split(",") in the dir method like this:
dir(mes) and print it.
All available methods will be displayed, among them there will be no split method, this is what caused the error.

M
Michael, 2021-07-31
@lob4Noff

You are getting an error because aiogram uses its own data type in the described case. The split() method works if the object is a string.
You can get a dictionary containing information about the user and his message, and then pass the value of the message to the list by key (the message will be split into separate words when a comma character occurs):

import config
import logging

from aiogram import Bot, Dispatcher, executor, types

logging.basicConfig(level=logging.INFO)

bot = Bot(token='')
dp = Dispatcher(bot)

@dp.message_handler()

async def addrifm1(message: types.Message):
  mes = message
  list = mes['text'].split(",")
  await message.reply(" async def rifm1(message: types.Message):")
  await message.reply("if", list[1], " in message.text:")
  await message.reply("await message.reply(", list[2], ")")


if __name__ == "__main__":
  executor.start_polling(dp, skip_updates=True)

If you are just starting to learn programming, I advise you the telebot module : its syntax is much easier, and you can get more documentation on the Web.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question