inline_stuff.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 re
  9. import string
  10. from hikka.inline.types import BotInlineMessage
  11. from telethon.errors.rpcerrorlist import YouBlockedUserError
  12. from telethon.tl.functions.contacts import UnblockRequest
  13. from telethon.tl.types import Message
  14. from .. import loader, utils
  15. @loader.tds
  16. class InlineStuffMod(loader.Module):
  17. """Provides support for inline stuff"""
  18. strings = {
  19. "name": "InlineStuff",
  20. "bot_username_invalid": (
  21. "<emoji document_id=5415905755406539934>🚫</emoji> <b>Specified bot"
  22. " username is invalid. It must end with </b><code>bot</code><b> and contain"
  23. " at least 4 symbols</b>"
  24. ),
  25. "bot_username_occupied": (
  26. "<emoji document_id=5415905755406539934>🚫</emoji> <b>This username is"
  27. " already occupied</b>"
  28. ),
  29. "bot_updated": (
  30. "<emoji document_id=6318792204118656433>🎉</emoji> <b>Config successfully"
  31. " saved. Restart userbot to apply changes</b>"
  32. ),
  33. "this_is_hikka": (
  34. "🌘 <b>Hi! This is Hikka — powerful modular Telegram userbot. You can"
  35. " install it to your account!</b>\n\n<b>🌍 <a"
  36. ' href="https://github.com/hikaraitama/Hikka">GitHub</a></b>\n<b>👥 <a'
  37. ' href="https://t.me/hikka_talks">Support chat</a></b>'
  38. ),
  39. }
  40. strings_ru = {
  41. "bot_username_invalid": (
  42. "<emoji document_id=5415905755406539934>🚫</emoji> <b>Неправильный ник"
  43. " бота. Он должен заканчиваться на </b><code>bot</code><b> и быть не короче"
  44. " чем 5 символов</b>"
  45. ),
  46. "bot_username_occupied": (
  47. "<emoji document_id=5415905755406539934>🚫</emoji> <b>Такой ник бота уже"
  48. " занят</b>"
  49. ),
  50. "bot_updated": (
  51. "<emoji document_id=6318792204118656433>🎉</emoji> <b>Настройки сохранены."
  52. " Для их применения нужно перезагрузить юзербот</b>"
  53. ),
  54. "this_is_hikka": (
  55. "🌘 <b>Привет! Это Hikka — мощный модульный Telegram юзербот. Вы можете"
  56. " установить его на свой аккаунт!</b>\n\n<b>🌍 <a"
  57. ' href="https://github.com/hikariaitama/Hikka">GitHub</a></b>\n<b>👥 <a'
  58. ' href="https://t.me/hikka_talks">Чат поддержки</a></b>'
  59. ),
  60. }
  61. async def watcher(self, message: Message):
  62. if (
  63. getattr(message, "out", False)
  64. and getattr(message, "via_bot_id", False)
  65. and message.via_bot_id == self.inline.bot_id
  66. and "This message will be deleted automatically"
  67. in getattr(message, "raw_text", "")
  68. ):
  69. await message.delete()
  70. return
  71. if (
  72. not getattr(message, "out", False)
  73. or not getattr(message, "via_bot_id", False)
  74. or message.via_bot_id != self.inline.bot_id
  75. or "Opening gallery..." not in getattr(message, "raw_text", "")
  76. ):
  77. return
  78. id_ = re.search(r"#id: ([a-zA-Z0-9]+)", message.raw_text)[1]
  79. await message.delete()
  80. m = await message.respond("🌘 <b>Opening gallery...</b>")
  81. await self.inline.gallery(
  82. message=m,
  83. next_handler=self.inline._custom_map[id_]["handler"],
  84. caption=self.inline._custom_map[id_].get("caption", ""),
  85. force_me=self.inline._custom_map[id_].get("force_me", False),
  86. disable_security=self.inline._custom_map[id_].get(
  87. "disable_security", False
  88. ),
  89. silent=True,
  90. )
  91. async def _check_bot(self, username: str) -> bool:
  92. async with self._client.conversation("@BotFather", exclusive=False) as conv:
  93. try:
  94. m = await conv.send_message("/token")
  95. except YouBlockedUserError:
  96. await self._client(UnblockRequest(id="@BotFather"))
  97. m = await conv.send_message("/token")
  98. r = await conv.get_response()
  99. await m.delete()
  100. await r.delete()
  101. if not hasattr(r, "reply_markup") or not hasattr(r.reply_markup, "rows"):
  102. return False
  103. for row in r.reply_markup.rows:
  104. for button in row.buttons:
  105. if username != button.text.strip("@"):
  106. continue
  107. m = await conv.send_message("/cancel")
  108. r = await conv.get_response()
  109. await m.delete()
  110. await r.delete()
  111. return True
  112. @loader.command(ru_doc="<юзернейм> - Изменить юзернейм инлайн бота")
  113. async def ch_hikka_bot(self, message: Message):
  114. """<username> - Change your Hikka inline bot username"""
  115. args = utils.get_args_raw(message).strip("@")
  116. if (
  117. not args
  118. or not args.lower().endswith("bot")
  119. or len(args) <= 4
  120. or any(
  121. litera not in (string.ascii_letters + string.digits + "_")
  122. for litera in args
  123. )
  124. ):
  125. await utils.answer(message, self.strings("bot_username_invalid"))
  126. return
  127. try:
  128. await self._client.get_entity(f"@{args}")
  129. except ValueError:
  130. pass
  131. else:
  132. if not await self._check_bot(args):
  133. await utils.answer(message, self.strings("bot_username_occupied"))
  134. return
  135. self._db.set("hikka.inline", "custom_bot", args)
  136. self._db.set("hikka.inline", "bot_token", None)
  137. await utils.answer(message, self.strings("bot_updated"))
  138. async def aiogram_watcher(self, message: BotInlineMessage):
  139. if message.text != "/start":
  140. return
  141. await message.answer_photo(
  142. "https://github.com/hikariatama/assets/raw/master/hikka_banner.png",
  143. caption=self.strings("this_is_hikka"),
  144. )
  145. async def client_ready(self, client, db):
  146. if self.get("migrated"):
  147. return
  148. self.set("migrated", True)
  149. async with self._client.conversation("@BotFather") as conv:
  150. for msg in [
  151. "/cancel",
  152. "/setinline",
  153. f"@{self.inline.bot_username}",
  154. "user@hikka:~$",
  155. ]:
  156. m = await conv.send_message(msg)
  157. r = await conv.get_response()
  158. await m.delete()
  159. await r.delete()