utils.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  1. """Utilities"""
  2. # Friendly Telegram (telegram userbot)
  3. # Copyright (C) 2018-2021 The Authors
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU Affero General Public License for more details.
  12. # You should have received a copy of the GNU Affero General Public License
  13. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. # █ █ ▀ █▄▀ ▄▀█ █▀█ ▀
  15. # █▀█ █ █ █ █▀█ █▀▄ █
  16. # © Copyright 2022
  17. # https://t.me/hikariatama
  18. #
  19. # 🔒 Licensed under the GNU AGPLv3
  20. # 🌐 https://www.gnu.org/licenses/agpl-3.0.html
  21. import asyncio
  22. import functools
  23. import io
  24. import json
  25. import logging
  26. import os
  27. import random
  28. import re
  29. import shlex
  30. import string
  31. import time
  32. import inspect
  33. from datetime import timedelta
  34. from typing import Any, List, Optional, Tuple, Union
  35. from urllib.parse import urlparse
  36. import git
  37. import grapheme
  38. import requests
  39. import telethon
  40. from telethon.hints import Entity
  41. from telethon.tl.custom.message import Message
  42. from telethon.tl.functions.account import UpdateNotifySettingsRequest
  43. from telethon.tl.functions.channels import CreateChannelRequest, EditPhotoRequest
  44. from telethon.tl.functions.messages import (
  45. GetDialogFiltersRequest,
  46. UpdateDialogFilterRequest,
  47. )
  48. from telethon.tl.types import (
  49. Channel,
  50. InputPeerNotifySettings,
  51. MessageEntityBankCard,
  52. MessageEntityBlockquote,
  53. MessageEntityBold,
  54. MessageEntityBotCommand,
  55. MessageEntityCashtag,
  56. MessageEntityCode,
  57. MessageEntityEmail,
  58. MessageEntityHashtag,
  59. MessageEntityItalic,
  60. MessageEntityMention,
  61. MessageEntityMentionName,
  62. MessageEntityPhone,
  63. MessageEntityPre,
  64. MessageEntitySpoiler,
  65. MessageEntityStrike,
  66. MessageEntityTextUrl,
  67. MessageEntityUnderline,
  68. MessageEntityUnknown,
  69. MessageEntityUrl,
  70. MessageMediaWebPage,
  71. PeerChannel,
  72. PeerChat,
  73. PeerUser,
  74. User,
  75. Chat,
  76. UpdateNewChannelMessage,
  77. )
  78. from .inline.types import InlineCall, InlineMessage
  79. FormattingEntity = Union[
  80. MessageEntityUnknown,
  81. MessageEntityMention,
  82. MessageEntityHashtag,
  83. MessageEntityBotCommand,
  84. MessageEntityUrl,
  85. MessageEntityEmail,
  86. MessageEntityBold,
  87. MessageEntityItalic,
  88. MessageEntityCode,
  89. MessageEntityPre,
  90. MessageEntityTextUrl,
  91. MessageEntityMentionName,
  92. MessageEntityPhone,
  93. MessageEntityCashtag,
  94. MessageEntityUnderline,
  95. MessageEntityStrike,
  96. MessageEntityBlockquote,
  97. MessageEntityBankCard,
  98. MessageEntitySpoiler,
  99. ]
  100. ListLike = Union[list, set, tuple]
  101. emoji_pattern = re.compile(
  102. "["
  103. "\U0001F600-\U0001F64F" # emoticons
  104. "\U0001F300-\U0001F5FF" # symbols & pictographs
  105. "\U0001F680-\U0001F6FF" # transport & map symbols
  106. "\U0001F1E0-\U0001F1FF" # flags (iOS)
  107. "]+",
  108. flags=re.UNICODE,
  109. )
  110. parser = telethon.utils.sanitize_parse_mode("html")
  111. def get_args(message: Message) -> List[str]:
  112. """Get arguments from message (str or Message), return list of arguments"""
  113. if not (message := getattr(message, "message", message)):
  114. return False
  115. if len(message := message.split(maxsplit=1)) <= 1:
  116. return []
  117. message = message[1]
  118. try:
  119. split = shlex.split(message)
  120. except ValueError:
  121. return message # Cannot split, let's assume that it's just one long message
  122. return list(filter(lambda x: len(x) > 0, split))
  123. def get_args_raw(message: Message) -> str:
  124. """Get the parameters to the command as a raw string (not split)"""
  125. if not (message := getattr(message, "message", message)):
  126. return False
  127. return args[1] if len(args := message.split(maxsplit=1)) > 1 else ""
  128. def get_args_split_by(message: Message, separator: str) -> List[str]:
  129. """Split args with a specific separator"""
  130. return [
  131. section.strip() for section in get_args_raw(message).split(separator) if section
  132. ]
  133. def get_chat_id(message: Message) -> int:
  134. """Get the chat ID, but without -100 if its a channel"""
  135. return telethon.utils.resolve_id(message.chat_id)[0]
  136. def get_entity_id(entity: Entity) -> int:
  137. """Get entity ID"""
  138. return telethon.utils.get_peer_id(entity)
  139. def escape_html(text: str, /) -> str: # sourcery skip
  140. """Pass all untrusted/potentially corrupt input here"""
  141. return str(text).replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
  142. def escape_quotes(text: str, /) -> str:
  143. """Escape quotes to html quotes"""
  144. return escape_html(text).replace('"', "&quot;")
  145. def get_base_dir() -> str:
  146. """Get directory of this file"""
  147. from . import __main__
  148. return get_dir(__main__.__file__)
  149. def get_dir(mod: str) -> str:
  150. """Get directory of given module"""
  151. return os.path.abspath(os.path.dirname(os.path.abspath(mod)))
  152. async def get_user(message: Message) -> Union[None, User]:
  153. """Get user who sent message, searching if not found easily"""
  154. try:
  155. return await message.client.get_entity(message.sender_id)
  156. except ValueError: # Not in database. Lets go looking for them.
  157. logging.debug("User not in session cache. Searching...")
  158. if isinstance(message.peer_id, PeerUser):
  159. await message.client.get_dialogs()
  160. return await message.client.get_entity(message.sender_id)
  161. if isinstance(message.peer_id, (PeerChannel, PeerChat)):
  162. try:
  163. return await message.client.get_entity(message.sender_id)
  164. except Exception:
  165. pass
  166. async for user in message.client.iter_participants(
  167. message.peer_id,
  168. aggressive=True,
  169. ):
  170. if user.id == message.sender_id:
  171. return user
  172. logging.error("User isn't in the group where they sent the message")
  173. return None
  174. logging.error("`peer_id` is not a user, chat or channel")
  175. return None
  176. def run_sync(func, *args, **kwargs):
  177. """
  178. Run a non-async function in a new thread and return an awaitable
  179. :param func: Sync-only function to execute
  180. :returns: Awaitable coroutine
  181. """
  182. return asyncio.get_event_loop().run_in_executor(
  183. None,
  184. functools.partial(func, *args, **kwargs),
  185. )
  186. def run_async(loop, coro):
  187. """Run an async function as a non-async function, blocking till it's done"""
  188. # When we bump minimum support to 3.7, use run()
  189. return asyncio.run_coroutine_threadsafe(coro, loop).result()
  190. def censor(
  191. obj,
  192. to_censor: Optional[List[str]] = None,
  193. replace_with: Optional[str] = "redacted_{count}_chars",
  194. ):
  195. """May modify the original object, but don't rely on it"""
  196. if to_censor is None:
  197. to_censor = ["phone"]
  198. for k, v in vars(obj).items():
  199. if k in to_censor:
  200. setattr(obj, k, replace_with.format(count=len(v)))
  201. elif k[0] != "_" and hasattr(v, "__dict__"):
  202. setattr(obj, k, censor(v, to_censor, replace_with))
  203. return obj
  204. def relocate_entities(
  205. entities: list,
  206. offset: int,
  207. text: Optional[str] = None,
  208. ) -> list:
  209. """Move all entities by offset (truncating at text)"""
  210. length = len(text) if text is not None else 0
  211. for ent in entities.copy() if entities else ():
  212. ent.offset += offset
  213. if ent.offset < 0:
  214. ent.length += ent.offset
  215. ent.offset = 0
  216. if text is not None and ent.offset + ent.length > length:
  217. ent.length = length - ent.offset
  218. if ent.length <= 0:
  219. entities.remove(ent)
  220. return entities
  221. async def answer(
  222. message: Union[Message, InlineCall, InlineMessage],
  223. response: str,
  224. *,
  225. reply_markup: Optional[Union[List[List[dict]], List[dict], dict]] = None,
  226. **kwargs,
  227. ) -> Union[InlineCall, InlineMessage, Message]:
  228. """Use this to give the response to a command"""
  229. # Compatibility with FTG\GeekTG
  230. if isinstance(message, list) and message:
  231. message = message[0]
  232. if reply_markup is not None:
  233. if not isinstance(reply_markup, (list, dict)):
  234. raise ValueError("reply_markup must be a list or dict")
  235. if reply_markup:
  236. if isinstance(message, (InlineMessage, InlineCall)):
  237. await message.edit(response, reply_markup)
  238. return
  239. reply_markup = message.client.loader.inline._normalize_markup(reply_markup)
  240. result = await message.client.loader.inline.form(
  241. response,
  242. message=message if message.out else get_chat_id(message),
  243. reply_markup=reply_markup,
  244. **kwargs,
  245. )
  246. return result
  247. if isinstance(message, (InlineMessage, InlineCall)):
  248. await message.edit(response)
  249. return message
  250. kwargs.setdefault("link_preview", False)
  251. if not (edit := message.out):
  252. kwargs.setdefault(
  253. "reply_to",
  254. getattr(message, "reply_to_msg_id", None),
  255. )
  256. parse_mode = telethon.utils.sanitize_parse_mode(
  257. kwargs.pop(
  258. "parse_mode",
  259. message.client.parse_mode,
  260. )
  261. )
  262. if isinstance(response, str) and not kwargs.pop("asfile", False):
  263. text, entity = parse_mode.parse(response)
  264. if len(text) >= 4096:
  265. try:
  266. if not message.client.loader.inline.init_complete:
  267. raise
  268. strings = list(smart_split(text, entity, 4096))
  269. if len(strings) > 10:
  270. raise
  271. list_ = await message.client.loader.inline.list(
  272. message=message,
  273. strings=strings,
  274. )
  275. if not list_:
  276. raise
  277. return list_
  278. except Exception:
  279. file = io.BytesIO(text.encode("utf-8"))
  280. file.name = "command_result.txt"
  281. result = await message.client.send_file(
  282. message.peer_id,
  283. file,
  284. caption=(
  285. "<b>📤 Command output seems to be too long, so it's sent in"
  286. " file.</b>"
  287. ),
  288. )
  289. if message.out:
  290. await message.delete()
  291. return result
  292. result = await (message.edit if edit else message.respond)(
  293. text,
  294. parse_mode=lambda t: (t, entity),
  295. **kwargs,
  296. )
  297. elif isinstance(response, Message):
  298. if message.media is None and (
  299. response.media is None or isinstance(response.media, MessageMediaWebPage)
  300. ):
  301. result = await message.edit(
  302. response.message,
  303. parse_mode=lambda t: (t, response.entities or []),
  304. link_preview=isinstance(response.media, MessageMediaWebPage),
  305. )
  306. else:
  307. result = await message.respond(response, **kwargs)
  308. else:
  309. if isinstance(response, bytes):
  310. response = io.BytesIO(response)
  311. elif isinstance(response, str):
  312. response = io.BytesIO(response.encode("utf-8"))
  313. if name := kwargs.pop("filename", None):
  314. response.name = name
  315. if message.media is not None and edit:
  316. await message.edit(file=response, **kwargs)
  317. else:
  318. kwargs.setdefault(
  319. "reply_to",
  320. getattr(message, "reply_to_msg_id", None),
  321. )
  322. result = await message.client.send_file(message.chat_id, response, **kwargs)
  323. return result
  324. async def get_target(message: Message, arg_no: Optional[int] = 0) -> Union[int, None]:
  325. if any(
  326. isinstance(entity, MessageEntityMentionName)
  327. for entity in (message.entities or [])
  328. ):
  329. e = sorted(
  330. filter(lambda x: isinstance(x, MessageEntityMentionName), message.entities),
  331. key=lambda x: x.offset,
  332. )[0]
  333. return e.user_id
  334. if len(get_args(message)) > arg_no:
  335. user = get_args(message)[arg_no]
  336. elif message.is_reply:
  337. return (await message.get_reply_message()).sender_id
  338. elif hasattr(message.peer_id, "user_id"):
  339. user = message.peer_id.user_id
  340. else:
  341. return None
  342. try:
  343. entity = await message.client.get_entity(user)
  344. except ValueError:
  345. return None
  346. else:
  347. if isinstance(entity, User):
  348. return entity.id
  349. def merge(a: dict, b: dict) -> dict:
  350. """Merge with replace dictionary a to dictionary b"""
  351. for key in a:
  352. if key in b:
  353. if isinstance(a[key], dict) and isinstance(b[key], dict):
  354. b[key] = merge(a[key], b[key])
  355. elif isinstance(a[key], list) and isinstance(b[key], list):
  356. b[key] = list(set(b[key] + a[key]))
  357. else:
  358. b[key] = a[key]
  359. b[key] = a[key]
  360. return b
  361. async def set_avatar(
  362. client: "TelegramClient", # type: ignore
  363. peer: Entity,
  364. avatar: str,
  365. ) -> bool:
  366. """Sets an entity avatar"""
  367. if isinstance(avatar, str) and check_url(avatar):
  368. f = (
  369. await run_sync(
  370. requests.get,
  371. avatar,
  372. )
  373. ).content
  374. elif isinstance(avatar, bytes):
  375. f = avatar
  376. else:
  377. return False
  378. res = await client(
  379. EditPhotoRequest(
  380. channel=peer,
  381. photo=await client.upload_file(f, file_name="photo.png"),
  382. )
  383. )
  384. try:
  385. await client.delete_messages(
  386. peer,
  387. message_ids=[
  388. next(
  389. update
  390. for update in res.updates
  391. if isinstance(update, UpdateNewChannelMessage)
  392. ).message.id
  393. ],
  394. )
  395. except Exception:
  396. pass
  397. return True
  398. async def asset_channel(
  399. client: "TelegramClient", # type: ignore
  400. title: str,
  401. description: str,
  402. *,
  403. silent: Optional[bool] = False,
  404. archive: Optional[bool] = False,
  405. avatar: Optional[str] = "",
  406. _folder: Optional[str] = "",
  407. ) -> Tuple[Channel, bool]:
  408. """
  409. Create new channel (if needed) and return its entity
  410. :param client: Telegram client to create channel by
  411. :param title: Channel title
  412. :param description: Description
  413. :param silent: Automatically mute channel
  414. :param archive: Automatically archive channel
  415. :param avatar: Url to an avatar to set as pfp of created peer
  416. :param _folder: Do not use it, or things will go wrong
  417. :returns: Peer and bool: is channel new or pre-existent
  418. """
  419. async for d in client.iter_dialogs():
  420. if d.title == title:
  421. return d.entity, False
  422. peer = (
  423. await client(
  424. CreateChannelRequest(
  425. title,
  426. description,
  427. megagroup=True,
  428. )
  429. )
  430. ).chats[0]
  431. if silent:
  432. await dnd(client, peer, archive)
  433. elif archive:
  434. await client.edit_folder(peer, 1)
  435. if avatar:
  436. await set_avatar(client, peer, avatar)
  437. if _folder:
  438. if _folder != "hikka":
  439. raise NotImplementedError
  440. folders = await client(GetDialogFiltersRequest())
  441. try:
  442. folder = next(folder for folder in folders if folder.title == "hikka")
  443. except Exception:
  444. return
  445. if any(
  446. peer.id == getattr(folder_peer, "channel_id", None)
  447. for folder_peer in folder.include_peers
  448. ):
  449. return
  450. folder.include_peers += [await client.get_input_entity(peer)]
  451. await client(
  452. UpdateDialogFilterRequest(
  453. folder.id,
  454. folder,
  455. )
  456. )
  457. return peer, True
  458. async def dnd(
  459. client: "TelegramClient", # type: ignore
  460. peer: Entity,
  461. archive: Optional[bool] = True,
  462. ) -> bool:
  463. """
  464. Mutes and optionally archives peer
  465. :param peer: Anything entity-link
  466. :param archive: Archive peer, or just mute?
  467. :returns: `True` on success, otherwise `False`
  468. """
  469. try:
  470. await client(
  471. UpdateNotifySettingsRequest(
  472. peer=peer,
  473. settings=InputPeerNotifySettings(
  474. show_previews=False,
  475. silent=True,
  476. mute_until=2**31 - 1,
  477. ),
  478. )
  479. )
  480. if archive:
  481. await client.edit_folder(peer, 1)
  482. except Exception:
  483. logging.exception("utils.dnd error")
  484. return False
  485. return True
  486. def get_link(user: Union[User, Channel], /) -> str:
  487. """Get telegram permalink to entity"""
  488. return (
  489. f"tg://user?id={user.id}"
  490. if isinstance(user, User)
  491. else (
  492. f"tg://resolve?domain={user.username}"
  493. if getattr(user, "username", None)
  494. else ""
  495. )
  496. )
  497. def chunks(_list: Union[list, tuple, set], n: int, /) -> list:
  498. """Split provided `_list` into chunks of `n`"""
  499. return [_list[i : i + n] for i in range(0, len(_list), n)]
  500. def get_named_platform() -> str:
  501. """Returns formatted platform name"""
  502. try:
  503. if os.path.isfile("/proc/device-tree/model"):
  504. with open("/proc/device-tree/model") as f:
  505. model = f.read()
  506. if "Orange" in model:
  507. return f"🍊 {model}"
  508. return f"🍇 {model}" if "Raspberry" in model else f"❓ {model}"
  509. except Exception:
  510. # In case of weird fs, aka Termux
  511. pass
  512. is_termux = "com.termux" in os.environ.get("PREFIX", "")
  513. is_okteto = "OKTETO" in os.environ
  514. is_docker = "DOCKER" in os.environ
  515. is_heroku = "DYNO" in os.environ
  516. if is_heroku:
  517. return "♓️ Heroku"
  518. if is_docker:
  519. return "🐳 Docker"
  520. if is_termux:
  521. return "🕶 Termux"
  522. if is_okteto:
  523. return "☁️ Okteto"
  524. is_lavhost = "LAVHOST" in os.environ
  525. return f"✌️ lavHost {os.environ['LAVHOST']}" if is_lavhost else "📻 VDS"
  526. def uptime() -> int:
  527. """Returns userbot uptime in seconds"""
  528. return round(time.perf_counter() - init_ts)
  529. def formatted_uptime() -> str:
  530. """Returnes formmated uptime"""
  531. return f"{str(timedelta(seconds=uptime()))}"
  532. def ascii_face() -> str:
  533. """Returnes cute ASCII-art face"""
  534. return escape_html(
  535. random.choice(
  536. [
  537. "ヽ(๑◠ܫ◠๑)ノ",
  538. "(◕ᴥ◕ʋ)",
  539. "ᕙ(`▽´)ᕗ",
  540. "(✿◠‿◠)",
  541. "(▰˘◡˘▰)",
  542. "(˵ ͡° ͜ʖ ͡°˵)",
  543. "ʕっ•ᴥ•ʔっ",
  544. "( ͡° ᴥ ͡°)",
  545. "(๑•́ ヮ •̀๑)",
  546. "٩(^‿^)۶",
  547. "(っˆڡˆς)",
  548. "ψ(`∇´)ψ",
  549. "⊙ω⊙",
  550. "٩(^ᴗ^)۶",
  551. "(´・ω・)っ由",
  552. "( ͡~ ͜ʖ ͡°)",
  553. "✧♡(◕‿◕✿)",
  554. "โ๏௰๏ใ ื",
  555. "∩。• ᵕ •。∩ ♡",
  556. "(♡´౪`♡)",
  557. "(◍>◡<◍)⋈。✧♡",
  558. "╰(✿´⌣`✿)╯♡",
  559. "ʕ•ᴥ•ʔ",
  560. "ᶘ ◕ᴥ◕ᶅ",
  561. "▼・ᴥ・▼",
  562. "ฅ^•ﻌ•^ฅ",
  563. "(΄◞ิ౪◟ิ‵)",
  564. "٩(^ᴗ^)۶",
  565. "ᕴーᴥーᕵ",
  566. "ʕ→ᴥ←ʔ",
  567. "ʕᵕᴥᵕʔ",
  568. "ʕᵒᴥᵒʔ",
  569. "ᵔᴥᵔ",
  570. "(✿╹◡╹)",
  571. "(๑→ܫ←)",
  572. "ʕ·ᴥ· ʔ",
  573. "(ノ≧ڡ≦)",
  574. "(≖ᴗ≖✿)",
  575. "(〜^∇^ )〜",
  576. "( ノ・ェ・ )ノ",
  577. "~( ˘▾˘~)",
  578. "(〜^∇^)〜",
  579. "ヽ(^ᴗ^ヽ)",
  580. "(´・ω・`)",
  581. "₍ᐢ•ﻌ•ᐢ₎*・゚。",
  582. "(。・・)_且",
  583. "(=`ω´=)",
  584. "(*•‿•*)",
  585. "(*゚∀゚*)",
  586. "(☉⋆‿⋆☉)",
  587. "ɷ◡ɷ",
  588. "ʘ‿ʘ",
  589. "(。-ω-)ノ",
  590. "( ・ω・)ノ",
  591. "(=゚ω゚)ノ",
  592. "(・ε・`*) …",
  593. "ʕっ•ᴥ•ʔっ",
  594. "(*˘︶˘*)",
  595. ]
  596. )
  597. )
  598. def array_sum(array: List[Any], /) -> List[Any]:
  599. """Performs basic sum operation on array"""
  600. result = []
  601. for item in array:
  602. result += item
  603. return result
  604. def rand(size: int, /) -> str:
  605. """Return random string of len `size`"""
  606. return "".join(
  607. [random.choice("abcdefghijklmnopqrstuvwxyz1234567890") for _ in range(size)]
  608. )
  609. def smart_split(
  610. text: str,
  611. entities: List[FormattingEntity],
  612. length: Optional[int] = 4096,
  613. split_on: Optional[ListLike] = ("\n", " "),
  614. min_length: Optional[int] = 1,
  615. ):
  616. """
  617. Split the message into smaller messages.
  618. A grapheme will never be broken. Entities will be displaced to match the right location. No inputs will be mutated.
  619. The end of each message except the last one is stripped of characters from [split_on]
  620. :param text: the plain text input
  621. :param entities: the entities
  622. :param length: the maximum length of a single message
  623. :param split_on: characters (or strings) which are preferred for a message break
  624. :param min_length: ignore any matches on [split_on] strings before this number of characters into each message
  625. :return:
  626. """
  627. # Authored by @bsolute
  628. # https://t.me/LonamiWebs/27777
  629. encoded = text.encode("utf-16le")
  630. pending_entities = entities
  631. text_offset = 0
  632. bytes_offset = 0
  633. text_length = len(text)
  634. bytes_length = len(encoded)
  635. while text_offset < text_length:
  636. if bytes_offset + length * 2 >= bytes_length:
  637. yield parser.unparse(
  638. text[text_offset:],
  639. list(sorted(pending_entities, key=lambda x: x.offset)),
  640. )
  641. break
  642. codepoint_count = len(
  643. encoded[bytes_offset : bytes_offset + length * 2].decode(
  644. "utf-16le",
  645. errors="ignore",
  646. )
  647. )
  648. for search in split_on:
  649. search_index = text.rfind(
  650. search,
  651. text_offset + min_length,
  652. text_offset + codepoint_count,
  653. )
  654. if search_index != -1:
  655. break
  656. else:
  657. search_index = text_offset + codepoint_count
  658. split_index = grapheme.safe_split_index(text, search_index)
  659. split_offset_utf16 = (
  660. len(text[text_offset:split_index].encode("utf-16le"))
  661. ) // 2
  662. exclude = 0
  663. while (
  664. split_index + exclude < text_length
  665. and text[split_index + exclude] in split_on
  666. ):
  667. exclude += 1
  668. current_entities = []
  669. entities = pending_entities.copy()
  670. pending_entities = []
  671. for entity in entities:
  672. if (
  673. entity.offset < split_offset_utf16
  674. and entity.offset + entity.length > split_offset_utf16 + exclude
  675. ):
  676. # spans boundary
  677. current_entities.append(
  678. _copy_tl(
  679. entity,
  680. length=split_offset_utf16 - entity.offset,
  681. )
  682. )
  683. pending_entities.append(
  684. _copy_tl(
  685. entity,
  686. offset=0,
  687. length=entity.offset
  688. + entity.length
  689. - split_offset_utf16
  690. - exclude,
  691. )
  692. )
  693. elif entity.offset < split_offset_utf16 < entity.offset + entity.length:
  694. # overlaps boundary
  695. current_entities.append(
  696. _copy_tl(
  697. entity,
  698. length=split_offset_utf16 - entity.offset,
  699. )
  700. )
  701. elif entity.offset < split_offset_utf16:
  702. # wholly left
  703. current_entities.append(entity)
  704. elif (
  705. entity.offset + entity.length
  706. > split_offset_utf16 + exclude
  707. > entity.offset
  708. ):
  709. # overlaps right boundary
  710. pending_entities.append(
  711. _copy_tl(
  712. entity,
  713. offset=0,
  714. length=entity.offset
  715. + entity.length
  716. - split_offset_utf16
  717. - exclude,
  718. )
  719. )
  720. elif entity.offset + entity.length > split_offset_utf16 + exclude:
  721. # wholly right
  722. pending_entities.append(
  723. _copy_tl(
  724. entity,
  725. offset=entity.offset - split_offset_utf16 - exclude,
  726. )
  727. )
  728. current_text = text[text_offset:split_index]
  729. yield parser.unparse(
  730. current_text,
  731. list(sorted(current_entities, key=lambda x: x.offset)),
  732. )
  733. text_offset = split_index + exclude
  734. bytes_offset += len(current_text.encode("utf-16le"))
  735. def _copy_tl(o, **kwargs):
  736. d = o.to_dict()
  737. del d["_"]
  738. d.update(kwargs)
  739. return o.__class__(**d)
  740. def check_url(url: str) -> bool:
  741. """Checks url for validity"""
  742. try:
  743. return bool(urlparse(url).netloc)
  744. except Exception:
  745. return False
  746. def get_git_hash() -> Union[str, bool]:
  747. """Get current Hikka git hash"""
  748. try:
  749. repo = git.Repo()
  750. return repo.heads[0].commit.hexsha
  751. except Exception:
  752. return False
  753. def is_serializable(x: Any, /) -> bool:
  754. """Checks if object is JSON-serializable"""
  755. try:
  756. json.dumps(x)
  757. return True
  758. except Exception:
  759. return False
  760. def get_lang_flag(countrycode: str) -> str:
  761. """
  762. Gets an emoji of specified countrycode
  763. :param countrycode: 2-letter countrycode
  764. :returns: Emoji flag
  765. """
  766. if (
  767. len(
  768. code := [
  769. c
  770. for c in countrycode.lower()
  771. if c in string.ascii_letters + string.digits
  772. ]
  773. )
  774. == 2
  775. ):
  776. return "".join([chr(ord(c.upper()) + (ord("🇦") - ord("A"))) for c in code])
  777. return countrycode
  778. def get_entity_url(
  779. entity: Union[User, Channel],
  780. openmessage: Optional[bool] = False,
  781. ) -> str:
  782. """
  783. Get link to object, if available
  784. :param entity: Entity to get url of
  785. :param openmessage: Use tg://openmessage link for users
  786. :return: Link to object or empty string
  787. """
  788. return (
  789. (
  790. f"tg://openmessage?id={entity.id}"
  791. if openmessage
  792. else f"tg://user?id={entity.id}"
  793. )
  794. if isinstance(entity, User)
  795. else (
  796. f"tg://resolve?domain={entity.username}"
  797. if getattr(entity, "username", None)
  798. else ""
  799. )
  800. )
  801. async def get_message_link(
  802. message: Message,
  803. chat: Optional[Union[Chat, Channel]] = None,
  804. ) -> str:
  805. if message.is_private:
  806. return (
  807. f"tg://openmessage?user_id={get_chat_id(message)}&message_id={message.id}"
  808. )
  809. if not chat:
  810. chat = await message.get_chat()
  811. return (
  812. f"https://t.me/{chat.username}/{message.id}"
  813. if getattr(chat, "username", False)
  814. else f"https://t.me/c/{chat.id}/{message.id}"
  815. )
  816. def remove_html(text: str, escape: Optional[bool] = False) -> str:
  817. """
  818. Removes HTML tags from text
  819. :param text: Text to remove HTML from
  820. :param escape: Escape HTML
  821. :return: Text without HTML
  822. """
  823. return (escape_html if escape else str)(
  824. re.sub(
  825. r"(<\/?a.*?>|<\/?b>|<\/?i>|<\/?u>|<\/?strong>|<\/?em>|<\/?code>|<\/?strike>|<\/?del>|<\/?pre.*?>)",
  826. "",
  827. text,
  828. )
  829. )
  830. def get_kwargs() -> dict:
  831. """
  832. Get kwargs of function, in which is called
  833. :return: kwargs
  834. """
  835. # https://stackoverflow.com/a/65927265/19170642
  836. frame = inspect.currentframe().f_back
  837. keys, _, _, values = inspect.getargvalues(frame)
  838. return {key: values[key] for key in keys if key != "self"}
  839. init_ts = time.perf_counter()
  840. # GeekTG Compatibility
  841. def get_git_info():
  842. # https://github.com/GeekTG/Friendly-Telegram/blob/master/friendly-telegram/utils.py#L133
  843. try:
  844. repo = git.Repo()
  845. ver = repo.heads[0].commit.hexsha
  846. except Exception:
  847. ver = ""
  848. return [
  849. ver,
  850. f"https://github.com/hikariatama/Hikka/commit/{ver}" if ver else "",
  851. ]
  852. def get_version_raw():
  853. """Get the version of the userbot"""
  854. # https://github.com/GeekTG/Friendly-Telegram/blob/master/friendly-telegram/utils.py#L128
  855. from . import version
  856. return ".".join(list(map(str, list(version.__version__))))
  857. get_platform_name = get_named_platform