bot_pm.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # █ █ ▀ █▄▀ ▄▀█ █▀█ ▀
  2. # █▀█ █ █ █ █▀█ █▀▄ █
  3. # © Copyright 2022
  4. # https://t.me/hikariatama
  5. #
  6. # 🔒 Licensed under the GNU AGPLv3
  7. # 🌐 https://www.gnu.org/licenses/agpl-3.0.html
  8. import logging
  9. from aiogram.types import Message as AiogramMessage
  10. from typing import Optional, Union
  11. from .types import InlineUnit
  12. logger = logging.getLogger(__name__)
  13. class BotPM(InlineUnit):
  14. def set_fsm_state(self, user: Union[str, int], state: Union[str, bool]) -> bool:
  15. if not isinstance(user, (str, int)):
  16. logger.error("Invalid type for `user` in `set_fsm_state`")
  17. return False
  18. if not isinstance(state, (str, bool)):
  19. logger.error("Invalid type for `state` in `set_fsm_state`")
  20. return False
  21. if state:
  22. self.fsm[str(user)] = state
  23. elif str(user) in self.fsm:
  24. del self.fsm[str(user)]
  25. return True
  26. ss = set_fsm_state
  27. def get_fsm_state(self, user: Union[str, int]) -> Union[bool, str]:
  28. if not isinstance(user, (str, int)):
  29. logger.error("Invalid type for `user` in `get_fsm_state`")
  30. return False
  31. return self.fsm.get(str(user), False)
  32. gs = get_fsm_state
  33. async def _bot_message_answer( # skipcq: PYL-E0213
  34. mod,
  35. text: str = None,
  36. message: AiogramMessage = None,
  37. parse_mode: Optional[str] = "HTML",
  38. disable_web_page_preview: Optional[bool] = True,
  39. **kwargs,
  40. ) -> bool:
  41. try:
  42. await mod.bot.send_message(
  43. message.chat.id,
  44. text,
  45. parse_mode=parse_mode,
  46. disable_web_page_preview=disable_web_page_preview,
  47. **kwargs,
  48. )
  49. except Exception:
  50. return False
  51. return True