utils.py 30 KB

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