Advertisement
Fsoky

Anonim Chat BOT Python + MongoDB

Aug 11th, 2021
2,435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.17 KB | None | 0 0
  1. from aiogram import Bot, Dispatcher, types, executor
  2. from aiogram.types import ReplyKeyboardMarkup, KeyboardButton, \
  3.                             InlineKeyboardMarkup, InlineKeyboardButton
  4. from aiogram.utils.callback_data import CallbackData
  5. from aiogram.contrib.fsm_storage.memory import MemoryStorage
  6. from aiogram.dispatcher import FSMContext
  7. from aiogram.dispatcher.filters.state import State, StatesGroup
  8. from pymongo import MongoClient
  9. import random
  10.  
  11. bot = Bot(token="token")
  12. dp = Dispatcher(bot, storage=MemoryStorage())
  13.  
  14. cluster = MongoClient("link")
  15. collqueue = cluster.anonimdb.queue
  16. collusers = cluster.anonimdb.users
  17. collchats = cluster.anonimdb.chats
  18.  
  19.  
  20. class SetBio(StatesGroup):
  21.     user_bio = State()
  22.  
  23.  
  24. @dp.message_handler(commands="start")
  25. async def menu(message: types.Message):
  26.     keyboard = ReplyKeyboardMarkup(
  27.         [
  28.             [
  29.                 KeyboardButton("πŸ₯‘ Account"),
  30.                 KeyboardButton("β˜•οΈ Search user"),
  31.                 KeyboardButton("🍻 Search for a user group")
  32.             ]
  33.         ],
  34.         resize_keyboard=True
  35.     )
  36.  
  37.     await message.answer("πŸ’ Main menu", reply_markup=keyboard)
  38.  
  39.  
  40. @dp.message_handler(commands=["bio", "set_bio", "new_bio", "about_me"])
  41. async def user_bio(message: types.Message):
  42.     if collusers.count_documents({"_id": message.from_user.id}) == 0:
  43.         await account_user(message)
  44.     else:
  45.         await SetBio.user_bio.set()
  46.         await message.answer("Please, enter a new bio")
  47.  
  48.  
  49. @dp.message_handler(state=SetBio.user_bio)
  50. async def process_set_bio(message: types.Message, state: FSMContext):
  51.     async with state.proxy() as data:
  52.         data["user_bio"] = message.text
  53.         collusers.update_one({"_id": message.from_user.id}, {"$set": {"bio": data["user_bio"]}})
  54.  
  55.         await message.answer("You are successfully set a new bio")
  56.         await state.finish()
  57.  
  58.  
  59. @dp.message_handler(commands="account")
  60. async def account_user(message: types.Message):
  61.     if collusers.count_documents({"_id": message.from_user.id}) == 0:
  62.         keyboard = ReplyKeyboardMarkup([[KeyboardButton("🍷 Registration")]], resize_keyboard=True, one_time_keyboard=True)
  63.         await message.answer("You are not register in the system", reply_markup=keyboard)
  64.     else:
  65.         acc = collusers.find_one({"_id": message.from_user.id})
  66.         text = f"""User ID: {message.from_user.id}\nBalance: {acc['balance']}\nReputation: {acc['reputation']}\nBio: {acc['bio']}"""
  67.         keyboard = ReplyKeyboardMarkup(
  68.             [
  69.                 [
  70.                     KeyboardButton("πŸ’£ Remove account"),
  71.                     KeyboardButton("πŸ’ Back to menu")
  72.                 ]
  73.             ],
  74.             resize_keyboard=True
  75.         )
  76.  
  77.         await message.answer(text, reply_markup=keyboard)
  78.  
  79.  
  80. @dp.message_handler(commands=["remove", "remove_acc", "remove_account"])
  81. async def remove_account_act(message: types.Message):
  82.     if collusers.count_documents({"_id": message.from_user.id}) != 0:
  83.         keyboard = InlineKeyboardMarkup(
  84.             inline_keyboard=[
  85.                 [
  86.                     InlineKeyboardButton("Yes, I agree.", callback_data=CallbackData("choice", "action").new(action="remove")),
  87.                     InlineKeyboardButton("Cancel", callback_data=CallbackData("choice", "action").new(action="cancel"))
  88.                 ]
  89.             ],
  90.             one_time_keyboard=True
  91.         )
  92.  
  93.         await message.answer("You are really wanna remove the account?", reply_markup=keyboard)
  94.     else:
  95.         await message.answer("You don't have an account")
  96.         await menu(message)
  97.  
  98.  
  99. @dp.message_handler(commands=["reg", "registration"])
  100. async def account_registration_act(message: types.Message):
  101.     if collusers.count_documents({"_id": message.from_user.id}) == 0:
  102.         collusers.insert_one(
  103.             {
  104.                 "_id": message.from_user.id,
  105.                 "balance": 0,
  106.                 "reputation": 0,
  107.                 "bio": "The unknown user of the Internet"
  108.             }
  109.         )
  110.         hearts = ["❀️", "🧑", "πŸ’›", "πŸ’š", "πŸ’™", "πŸ’œ", "πŸ–€", "🀍"]
  111.         await message.answer(f"{random.choice(hearts)} You are successfully registered in the system")
  112.         await account_user(message)
  113.     else:
  114.         await message.answer("You already registered in the system")
  115.         await account_user(message)
  116.  
  117.  
  118. @dp.message_handler(commands=["search_user", "searchuser"])
  119. async def search_user_act(message: types.Message):
  120.     if message.chat.type == "private":
  121.         if collusers.count_documents({"_id": message.from_user.id}) == 0:
  122.             await account_user(message)
  123.         else:
  124.             if collchats.count_documents({"user_chat_id": message.chat.id}) != 0:
  125.                 await message.answer("You already have an active chat")
  126.             else:
  127.                 keyboard = ReplyKeyboardMarkup([[KeyboardButton("πŸ“› Stop search")]], resize_keyboard=True, one_time_keyboard=True)
  128.                 interlocutor = collqueue.find_one({})
  129.  
  130.                 if interlocutor is None:
  131.                     collqueue.insert_one({"_id": message.chat.id})
  132.                     await message.answer("πŸ•’ Search user began, please wait... Or you can stop searching", reply_markup=keyboard)
  133.                 else:
  134.                     if collqueue.count_documents({"_id": interlocutor["_id"]}) != 0:
  135.                         collqueue.delete_one({"_id": message.chat.id})
  136.                         collqueue.delete_one({"_id": interlocutor["_id"]})
  137.  
  138.                         collchats.insert_one(
  139.                             {
  140.                                 "user_chat_id": message.chat.id,
  141.                                 "interlocutor_chat_id": interlocutor["_id"]
  142.                             }
  143.                         )
  144.                         collchats.insert_one(
  145.                             {
  146.                                 "user_chat_id": interlocutor["_id"],
  147.                                 "interlocutor_chat_id": message.chat.id
  148.                             }
  149.                         )
  150.  
  151.                         keyboard_leave = ReplyKeyboardMarkup([[KeyboardButton("πŸ’” Leave from chat")]], resize_keyboard=True, one_time_keyboard=True)
  152.                         chat_info = collchats.find_one({"user_chat_id": message.chat.id})["interlocutor_chat_id"]
  153.  
  154.                         await message.answer("Interlocutor found! You can begin communicate.", reply_markup=keyboard_leave)
  155.                         await bot.send_message(text="Interlocutor found! You can begin communicate.", chat_id=chat_info, reply_markup=keyboard_leave)
  156.                     else:
  157.                         collqueue.insert_one({"_id": message.chat.id})
  158.                         await message.answer("πŸ•’ Search user began, please wait... Or you can stop searching", reply_markup=keyboard)
  159.  
  160.  
  161. @dp.message_handler(commands=["stop_search"])
  162. async def stop_search_act(message: types.Message):
  163.     if collqueue.count_documents({"_id": message.chat.id}) != 0:
  164.         collqueue.delete_one({"_id": message.chat.id})
  165.         await menu(message)
  166.     else:
  167.         await message.answer("You didn't begin search an interlocutor")
  168.  
  169.  
  170. @dp.message_handler(commands=["leave", "leave_chat"])
  171. async def leave_from_chat_act(message: types.Message):
  172.     if collchats.count_documents({"user_chat_id": message.chat.id}) != 0:
  173.         keyboard = ReplyKeyboardMarkup(
  174.             [
  175.                 [
  176.                     KeyboardButton("πŸ₯‘ Account"),
  177.                     KeyboardButton("β˜•οΈ Search user"),
  178.                     KeyboardButton("🍻 Search for a user group")
  179.                 ]
  180.             ],
  181.             resize_keyboard=True
  182.         )
  183.  
  184.         await message.answer("You are left the chat")
  185.         await bot.send_message(text="The Interlocutor left the chat", chat_id=collchats.find_one({"user_chat_id": message.chat.id})["interlocutor_chat_id"], reply_markup=keyboard)
  186.         await menu(message)
  187.  
  188.         collchats.delete_one({"user_chat_id": collchats.find_one({"user_chat_id": message.chat.id})["interlocutor_chat_id"]})
  189.         collchats.delete_one({"user_chat_id": message.chat.id})
  190.     else:
  191.         await message.answer("You didn't begin a chat with an interlocutor")
  192.  
  193.  
  194. @dp.message_handler(content_types=["text", "sticker", "photo", "voice", "document"])
  195. async def some_text(message: types.Message):
  196.     if message.text == "🍷 Registration":
  197.         await account_registration_act(message)
  198.     elif message.text == "πŸ₯‘ Account":
  199.         await account_user(message)
  200.     elif message.text == "πŸ’ Back to menu":
  201.         await menu(message)
  202.     elif message.text == "πŸ’£ Remove account":
  203.         await remove_account_act(message)
  204.     elif message.text == "β˜•οΈ Search user":
  205.         await search_user_act(message)
  206.     elif message.text == "πŸ“› Stop search":
  207.         await stop_search_act(message)
  208.     elif message.text == "πŸ’” Leave from chat":
  209.         await leave_from_chat_act(message)
  210.     elif message.content_type == "sticker":
  211.         try:
  212.             await bot.send_sticker(chat_id=collchats.find_one({"user_chat_id": message.chat.id})["interlocutor_chat_id"], sticker=message.sticker["file_id"])
  213.         except TypeError:
  214.             pass
  215.     elif message.content_type == "photo":
  216.         try:
  217.             await bot.send_photo(chat_id=collchats.find_one({"user_chat_id": message.chat.id})["interlocutor_chat_id"], photo=message.photo["file_id"])
  218.         except TypeError:
  219.             pass
  220.     elif message.content_type == "voice":
  221.         try:
  222.             await bot.send_voice(chat_id=collchats.find_one({"user_chat_id": message.chat.id})["interlocutor_chat_id"], voice=message.voice["file_id"])
  223.         except TypeError:
  224.             pass
  225.     elif message.content_type == "document":
  226.         try:
  227.             await bot.send_document(chat_id=collchats.find_one({"user_chat_id": message.chat.id})["interlocutor_chat_id"], document=message.document["file_id"])
  228.         except TypeError:
  229.             pass
  230.     else:
  231.         try:
  232.             await bot.send_message(text=message.text, chat_id=collchats.find_one({"user_chat_id": message.chat.id})["interlocutor_chat_id"])
  233.         except TypeError:
  234.             pass
  235.  
  236.  
  237. @dp.callback_query_handler(text_contains="remove")
  238. async def process_remove_account(callback: types.CallbackQuery):
  239.     collusers.delete_one({"_id": callback.from_user.id})
  240.     await callback.message.answer("You are successfully removed the account")
  241.     await menu(callback.message)
  242.  
  243.  
  244. @dp.callback_query_handler(text_contains="cancel")
  245. async def process_cancel(callback: types.CallbackQuery):
  246.     await callback.message.answer("Ok. Don't joke with me.")
  247.  
  248. if __name__ == "__main__":
  249.     executor.start_polling(dp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement