inline_stuff.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 re
  7. import string
  8. from hikkatl.errors.rpcerrorlist import YouBlockedUserError
  9. from hikkatl.tl.functions.contacts import UnblockRequest
  10. from hikkatl.tl.types import Message
  11. from .. import loader, utils
  12. from ..inline.types import BotInlineMessage
  13. @loader.tds
  14. class InlineStuff(loader.Module):
  15. """Provides support for inline stuff"""
  16. strings = {"name": "InlineStuff"}
  17. @loader.watcher(
  18. "out",
  19. "only_inline",
  20. contains="This message will be deleted automatically",
  21. )
  22. async def watcher(self, message: Message):
  23. if message.via_bot_id == self.inline.bot_id:
  24. await message.delete()
  25. @loader.watcher("out", "only_inline", contains="Opening gallery...")
  26. async def gallery_watcher(self, message: Message):
  27. if message.via_bot_id != self.inline.bot_id:
  28. return
  29. id_ = re.search(r"#id: ([a-zA-Z0-9]+)", message.raw_text)[1]
  30. await message.delete()
  31. m = await message.respond("🌘", reply_to=utils.get_topic(message))
  32. await self.inline.gallery(
  33. message=m,
  34. next_handler=self.inline._custom_map[id_]["handler"],
  35. caption=self.inline._custom_map[id_].get("caption", ""),
  36. force_me=self.inline._custom_map[id_].get("force_me", False),
  37. disable_security=self.inline._custom_map[id_].get(
  38. "disable_security", False
  39. ),
  40. silent=True,
  41. )
  42. async def _check_bot(self, username: str) -> bool:
  43. async with self._client.conversation("@BotFather", exclusive=False) as conv:
  44. try:
  45. m = await conv.send_message("/token")
  46. except YouBlockedUserError:
  47. await self._client(UnblockRequest(id="@BotFather"))
  48. m = await conv.send_message("/token")
  49. r = await conv.get_response()
  50. await m.delete()
  51. await r.delete()
  52. if not hasattr(r, "reply_markup") or not hasattr(r.reply_markup, "rows"):
  53. return False
  54. for row in r.reply_markup.rows:
  55. for button in row.buttons:
  56. if username != button.text.strip("@"):
  57. continue
  58. m = await conv.send_message("/cancel")
  59. r = await conv.get_response()
  60. await m.delete()
  61. await r.delete()
  62. return True
  63. @loader.command()
  64. async def ch_hikka_bot(self, message: Message):
  65. args = utils.get_args_raw(message).strip("@")
  66. if (
  67. not args
  68. or not args.lower().endswith("bot")
  69. or len(args) <= 4
  70. or any(
  71. litera not in (string.ascii_letters + string.digits + "_")
  72. for litera in args
  73. )
  74. ):
  75. await utils.answer(message, self.strings("bot_username_invalid"))
  76. return
  77. try:
  78. await self._client.get_entity(f"@{args}")
  79. except ValueError:
  80. pass
  81. else:
  82. if not await self._check_bot(args):
  83. await utils.answer(message, self.strings("bot_username_occupied"))
  84. return
  85. self._db.set("hikka.inline", "custom_bot", args)
  86. self._db.set("hikka.inline", "bot_token", None)
  87. await utils.answer(message, self.strings("bot_updated"))
  88. async def aiogram_watcher(self, message: BotInlineMessage):
  89. if message.text != "/start":
  90. return
  91. await message.answer_photo(
  92. "https://github.com/hikariatama/assets/raw/master/hikka_banner.png",
  93. caption=self.strings("this_is_hikka"),
  94. )