types.py 8.2 KB

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