types.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. # ©️ Dan Gazizullin, 2021-2023
  2. # This file is a part of Hikka Userbot
  3. # 🌐 https://github.com/hikariatama/Hikka
  4. # You can redistribute it and/or modify it under the terms of the GNU AGPLv3
  5. # 🔑 https://www.gnu.org/licenses/agpl-3.0.html
  6. import logging
  7. from aiogram.types import CallbackQuery
  8. from aiogram.types import InlineQuery as AiogramInlineQuery
  9. from aiogram.types import InlineQueryResultArticle, InputTextMessageContent
  10. from aiogram.types import Message as AiogramMessage
  11. from .. import utils
  12. logger = logging.getLogger(__name__)
  13. class InlineMessage:
  14. """Aiogram message, sent via inline bot"""
  15. def __init__(
  16. self,
  17. inline_manager: "InlineManager", # type: ignore # noqa: F821
  18. unit_id: str,
  19. inline_message_id: str,
  20. ):
  21. self.inline_message_id = inline_message_id
  22. self.unit_id = unit_id
  23. self.inline_manager = inline_manager
  24. self._units = inline_manager._units
  25. self.form = (
  26. {"id": unit_id, **self._units[unit_id]} if unit_id in self._units else {}
  27. )
  28. async def edit(self, *args, **kwargs) -> "InlineMessage":
  29. if "unit_id" in kwargs:
  30. kwargs.pop("unit_id")
  31. if "inline_message_id" in kwargs:
  32. kwargs.pop("inline_message_id")
  33. return await self.inline_manager._edit_unit(
  34. *args,
  35. unit_id=self.unit_id,
  36. inline_message_id=self.inline_message_id,
  37. **kwargs,
  38. )
  39. async def delete(self) -> bool:
  40. return await self.inline_manager._delete_unit_message(
  41. self,
  42. unit_id=self.unit_id,
  43. )
  44. async def unload(self) -> bool:
  45. return await self.inline_manager._unload_unit(unit_id=self.unit_id)
  46. class BotInlineMessage:
  47. """Aiogram message, sent through inline bot itself"""
  48. def __init__(
  49. self,
  50. inline_manager: "InlineManager", # type: ignore # noqa: F821
  51. unit_id: str,
  52. chat_id: int,
  53. message_id: int,
  54. ):
  55. self.chat_id = chat_id
  56. self.unit_id = unit_id
  57. self.inline_manager = inline_manager
  58. self.message_id = message_id
  59. self._units = inline_manager._units
  60. self.form = (
  61. {"id": unit_id, **self._units[unit_id]} if unit_id in self._units else {}
  62. )
  63. async def edit(self, *args, **kwargs) -> "BotMessage":
  64. if "unit_id" in kwargs:
  65. kwargs.pop("unit_id")
  66. if "message_id" in kwargs:
  67. kwargs.pop("message_id")
  68. if "chat_id" in kwargs:
  69. kwargs.pop("chat_id")
  70. return await self.inline_manager._edit_unit(
  71. *args,
  72. unit_id=self.unit_id,
  73. chat_id=self.chat_id,
  74. message_id=self.message_id,
  75. **kwargs,
  76. )
  77. async def delete(self) -> bool:
  78. return await self.inline_manager._delete_unit_message(
  79. self,
  80. unit_id=self.unit_id,
  81. chat_id=self.chat_id,
  82. message_id=self.message_id,
  83. )
  84. async def unload(self, *args, **kwargs) -> bool:
  85. if "unit_id" in kwargs:
  86. kwargs.pop("unit_id")
  87. return await self.inline_manager._unload_unit(
  88. *args,
  89. unit_id=self.unit_id,
  90. **kwargs,
  91. )
  92. class InlineCall(CallbackQuery, InlineMessage):
  93. """Modified version of classic aiogram `CallbackQuery`"""
  94. def __init__(
  95. self,
  96. call: CallbackQuery,
  97. inline_manager: "InlineManager", # type: ignore # noqa: F821
  98. unit_id: str,
  99. ):
  100. CallbackQuery.__init__(self)
  101. for attr in {
  102. "id",
  103. "from_user",
  104. "message",
  105. "inline_message_id",
  106. "chat_instance",
  107. "data",
  108. "game_short_name",
  109. }:
  110. setattr(self, attr, getattr(call, attr, None))
  111. self.original_call = call
  112. InlineMessage.__init__(
  113. self,
  114. inline_manager,
  115. unit_id,
  116. call.inline_message_id,
  117. )
  118. class BotInlineCall(CallbackQuery, BotInlineMessage):
  119. """Modified version of classic aiogram `CallbackQuery`"""
  120. def __init__(
  121. self,
  122. call: CallbackQuery,
  123. inline_manager: "InlineManager", # type: ignore # noqa: F821
  124. unit_id: str,
  125. ):
  126. CallbackQuery.__init__(self)
  127. for attr in {
  128. "id",
  129. "from_user",
  130. "message",
  131. "chat",
  132. "chat_instance",
  133. "data",
  134. "game_short_name",
  135. }:
  136. setattr(self, attr, getattr(call, attr, None))
  137. self.original_call = call
  138. BotInlineMessage.__init__(
  139. self,
  140. inline_manager,
  141. unit_id,
  142. call.message.chat.id,
  143. call.message.message_id,
  144. )
  145. class InlineUnit:
  146. """InlineManager extension type. For internal use only"""
  147. def __init__(self):
  148. """Made just for type specification"""
  149. class BotMessage(AiogramMessage):
  150. """Modified version of original Aiogram Message"""
  151. def __init__(self):
  152. super().__init__()
  153. class InlineQuery(AiogramInlineQuery):
  154. """Modified version of original Aiogram InlineQuery"""
  155. def __init__(self, inline_query: AiogramInlineQuery):
  156. super().__init__(self)
  157. for attr in {"id", "from_user", "query", "offset", "chat_type", "location"}:
  158. setattr(self, attr, getattr(inline_query, attr, None))
  159. self.inline_query = inline_query
  160. self.args = (
  161. self.inline_query.query.split(maxsplit=1)[1]
  162. if len(self.inline_query.query.split()) > 1
  163. else ""
  164. )
  165. @staticmethod
  166. def _get_res(title: str, description: str, thumb_url: str) -> list:
  167. return [
  168. InlineQueryResultArticle(
  169. id=utils.rand(20),
  170. title=title,
  171. description=description,
  172. input_message_content=InputTextMessageContent(
  173. "😶‍🌫️ <i>There is nothing here...</i>",
  174. parse_mode="HTML",
  175. ),
  176. thumb_url=thumb_url,
  177. thumb_width=128,
  178. thumb_height=128,
  179. )
  180. ]
  181. async def e400(self):
  182. await self.answer(
  183. self._get_res(
  184. "🚫 400",
  185. (
  186. "Bad request. You need to pass right arguments, follow module's"
  187. " documentation"
  188. ),
  189. "https://img.icons8.com/color/344/swearing-male--v1.png",
  190. ),
  191. cache_time=0,
  192. )
  193. async def e403(self):
  194. await self.answer(
  195. self._get_res(
  196. "🚫 403",
  197. "You have no permissions to access this result",
  198. "https://img.icons8.com/external-wanicon-flat-wanicon/344/external-forbidden-new-normal-wanicon-flat-wanicon.png",
  199. ),
  200. cache_time=0,
  201. )
  202. async def e404(self):
  203. await self.answer(
  204. self._get_res(
  205. "🚫 404",
  206. "No results found",
  207. "https://img.icons8.com/external-justicon-flat-justicon/344/external-404-error-responsive-web-design-justicon-flat-justicon.png",
  208. ),
  209. cache_time=0,
  210. )
  211. async def e426(self):
  212. await self.answer(
  213. self._get_res(
  214. "🚫 426",
  215. "You need to update Hikka before sending this request",
  216. "https://img.icons8.com/fluency/344/approve-and-update.png",
  217. ),
  218. cache_time=0,
  219. )
  220. async def e500(self):
  221. await self.answer(
  222. self._get_res(
  223. "🚫 500",
  224. "Internal userbot error while processing request. More info in logs",
  225. "https://img.icons8.com/external-vitaliy-gorbachev-flat-vitaly-gorbachev/344/external-error-internet-security-vitaliy-gorbachev-flat-vitaly-gorbachev.png",
  226. ),
  227. cache_time=0,
  228. )