query_gallery.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 asyncio
  9. import logging
  10. import time
  11. from typing import List, Optional
  12. from aiogram.types import InlineQuery, InlineQueryResultArticle, InputTextMessageContent
  13. from .. import utils
  14. from .types import InlineUnit
  15. logger = logging.getLogger(__name__)
  16. class QueryGallery(InlineUnit):
  17. async def query_gallery(
  18. self,
  19. query: InlineQuery,
  20. items: List[dict],
  21. *,
  22. force_me: Optional[bool] = False,
  23. disable_security: Optional[bool] = False,
  24. always_allow: Optional[list] = None,
  25. ) -> bool:
  26. """
  27. Answer inline query with a bunch of inline galleries
  28. :param query: `InlineQuery` which should be answered with inline gallery
  29. :param items: Array of dicts with inline results.
  30. Each dict *must* has a:
  31. - `title` - The title of the result
  32. - `description` - Short description of the result
  33. - `next_handler` - Inline gallery handler. Callback or awaitable
  34. Each dict *can* has a:
  35. - `caption` - Caption of photo. Defaults to `""`
  36. - `force_me` - Whether the button must be accessed only by owner. Defaults to `False`
  37. - `disable_security` - Whether to disable the security checks at all. Defaults to `False`
  38. :param force_me: Either this gallery buttons must be pressed only by owner scope or no
  39. :param always_allow: Users, that are allowed to press buttons in addition to previous rules
  40. :param disable_security: By default, Hikka will try to check security of gallery
  41. If you want to disable all security checks on this gallery in particular, pass `disable_security=True`
  42. :return: Status of answer
  43. """
  44. if not isinstance(force_me, bool):
  45. logger.error("Invalid type for `force_me`")
  46. return False
  47. if not isinstance(disable_security, bool):
  48. logger.error("Invalid type for `disable_security`")
  49. return False
  50. if always_allow and not isinstance(always_allow, list):
  51. logger.error("Invalid type for `always_allow`")
  52. return False
  53. if not always_allow:
  54. always_allow = []
  55. if (
  56. not isinstance(items, list)
  57. or not all(isinstance(i, dict) for i in items)
  58. or not all(
  59. "title" in i
  60. and "description" in i
  61. and "next_handler" in i
  62. and (
  63. callable(i["next_handler"])
  64. or asyncio.iscoroutinefunction(i)
  65. or isinstance(i, list)
  66. )
  67. and isinstance(i["title"], str)
  68. and isinstance(i["description"], str)
  69. for i in items
  70. )
  71. ):
  72. logger.error("Invalid `items` specified in query gallery")
  73. return False
  74. result = []
  75. for i in items:
  76. if "thumb_handler" not in i:
  77. photo_url = await self._call_photo(i["next_handler"])
  78. if not photo_url:
  79. return False
  80. if isinstance(photo_url, list):
  81. photo_url = photo_url[0]
  82. if not isinstance(photo_url, str):
  83. logger.error("Invalid result from `next_handler`")
  84. continue
  85. else:
  86. photo_url = await self._call_photo(i["thumb_handler"])
  87. if not photo_url:
  88. return False
  89. if isinstance(photo_url, list):
  90. photo_url = photo_url[0]
  91. if not isinstance(photo_url, str):
  92. logger.error("Invalid result from `thumb_handler`")
  93. continue
  94. id_ = utils.rand(16)
  95. self._custom_map[id_] = {
  96. "handler": i["next_handler"],
  97. "ttl": round(time.time()) + 120,
  98. **({"always_allow": always_allow} if always_allow else {}),
  99. **({"force_me": force_me} if force_me else {}),
  100. **({"disable_security": disable_security} if disable_security else {}),
  101. **({"caption": i["caption"]} if "caption" in i else {}),
  102. }
  103. result += [
  104. InlineQueryResultArticle(
  105. id=utils.rand(20),
  106. title=i["title"],
  107. description=i["description"],
  108. input_message_content=InputTextMessageContent(
  109. f"🌘 <b>Loading Hikka gallery...</b>\n<i>#id: {id_}</i>",
  110. "HTML",
  111. disable_web_page_preview=True,
  112. ),
  113. thumb_url=photo_url,
  114. thumb_width=128,
  115. thumb_height=128,
  116. )
  117. ]
  118. await query.answer(result, cache_time=0)
  119. return True