Support tg://resolve

This commit is contained in:
夜坂雅 2023-09-04 13:00:25 +08:00
parent d1b7145255
commit 439191f736
Signed by: ShadowRZ
GPG Key ID: A24CE3ACA80FD3E4
2 changed files with 12 additions and 1 deletions

View File

@ -35,6 +35,7 @@ from nyx_bot.utils import (
make_divergence, make_divergence,
parse_matrixdotto_link, parse_matrixdotto_link,
parse_wordcloud_args, parse_wordcloud_args,
tg_link_to_tdotme_link,
) )
from nyx_bot.wordcloud import send_wordcloud from nyx_bot.wordcloud import send_wordcloud
@ -186,6 +187,8 @@ class Command:
if not self.args: if not self.args:
raise NyxBotValueError("No external URL given.") raise NyxBotValueError("No external URL given.")
target_url = self.args[0] target_url = self.args[0]
if target_url.startswith("tg://resolve"):
target_url = tg_link_to_tdotme_link(target_url)
result = ( result = (
MatrixMessage.select() MatrixMessage.select()
.where( .where(

View File

@ -5,7 +5,7 @@ from html.parser import HTMLParser
from io import StringIO from io import StringIO
from random import Random from random import Random
from typing import Dict, Optional, Tuple from typing import Dict, Optional, Tuple
from urllib.parse import unquote, urlparse from urllib.parse import parse_qs, unquote, urlparse
import xxhash import xxhash
from nio import AsyncClient, Event, MatrixRoom, RoomGetEventError, RoomMessageText from nio import AsyncClient, Event, MatrixRoom, RoomGetEventError, RoomMessageText
@ -121,6 +121,14 @@ def make_datetime(origin_server_ts: int):
return datetime.fromtimestamp(ts) return datetime.fromtimestamp(ts)
def tg_link_to_tdotme_link(tg_link: str):
parsed = urlparse(tg_link)
qs_parsed = parse_qs(parsed.query)
domain = qs_parsed["domain"][0]
post_id = qs_parsed["post"][0]
return f"https://t.me/{domain}/{post_id}"
def parse_matrixdotto_link(link: str): def parse_matrixdotto_link(link: str):
replaced = link.replace("https://matrix.to/#/", "https://matrix.to/") replaced = link.replace("https://matrix.to/#/", "https://matrix.to/")
parsed = urlparse(replaced) parsed = urlparse(replaced)