Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from aiogram import Bot, Dispatcher, types, executor
- from aiogram.contrib.fsm_storage.memory import MemoryStorage
- from aiogram.dispatcher import FSMContext
- from aiogram.dispatcher.filters.state import State, StatesGroup
- from pymongo import MongoClient
- bot = Bot(token="token")
- dp = Dispatcher(bot, storage=MemoryStorage())
- cluster = MongoClient("link to connect")
- collection = cluster.testdb.testcoll
- class Form(StatesGroup):
- name = State()
- passwd = State()
- @dp.message_handler(commands="reg")
- async def registration(message: types.Message):
- await Form.name.set()
- await message.answer("Please, enter your name")
- @dp.message_handler(state=Form.name)
- async def process_registration(message: types.Message, state: FSMContext):
- async with state.proxy() as data:
- data["name"] = message.text
- await Form.next()
- await message.answer("Now, enter your password")
- @dp.message_handler(state=Form.passwd)
- async def process_password(message: types.Message, state: FSMContext):
- async with state.proxy() as data:
- data["passwd"] = message.text
- if collection.count_documents({"_id": message.from_user.id, "chat_id": message.chat.id}) == 0:
- collection.insert_one(
- {
- "_id": message.from_user.id,
- "chat_id": message.chat.id,
- "name": data["name"],
- "password": data["passwd"]
- }
- )
- await message.reply("You successfully registered to the system")
- else:
- await message.reply("You already registered in the system!")
- await state.finish()
- @dp.message_handler(commands="data")
- async def get_data_of_db(message: types.Message):
- data = collection.find_one({"_id": message.from_user.id, "chat_id": message.chat.id})
- await message.reply(data)
- if __name__ == "__main__":
- executor.start_polling(dp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement