There's another good way of doing this if you just want to get multiple links without some specific order.
You need only one state to do this.
class Links(StatesGroup):
get = State()
Then register handlers which start a conversation
@dp.message_handler(commands=["links"])
async def collect_links_start(message: Message, state: FSMContext):
await message.reply("Send me links.\nTo finish, type or click /done")
await Links.get.set()
# You can use your own saving logic instead, this is just an example
await state.update_data(links=[])
…, append links
# You can set more advanced filter for links, `text_startswith` is just an example
@dp.message_handler(text_startswith=["http"], state=Links.get)
async def collect_links(message: Message, state: FSMContext):
# You can use your own saving logic instead, this is just an example
data = await state.get_data()
links = data["links"]
links.append(message.text)
await state.update_data(links=links)
await message.reply("Got it!\nTo finish, type or click /done")
# This handler is a fallback in case user didn't provide valid link
@dp.message_handler(state=Links.get)
async def invalid_link(message: Message):
await message.reply("This doesn't look like a valid link!\nTo finish, type or click /done")
… and end the conversation.
@dp.message_handler(commands=["done"], state=Links.get)
async def collect_links_finish(message: Message, state: FSMContext):
# You can use your own loading logic instead, this is just an example
data = await state.get_data()
links = data["links"]
await state.finish()
link_list = "\n".join(links)
await message.reply(f"Links:\n{link_list}")