utils.py 32 KB

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