fix: more flexible link handling
This commit is contained in:
parent
102d52cd5b
commit
535960b8ae
2 changed files with 12 additions and 7 deletions
|
@ -54,7 +54,7 @@ class TelegramAdapter:
|
|||
router.message(F.text.startswith('\\'))(handle_reverse_actions)
|
||||
router.message(F.text == '我是笨蛋')(handle_baka)
|
||||
# link 模块
|
||||
router.message(F.text.regexp(r'https?:\/\/(?:www\.)?([-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b)*(\/[\/\d\w\.-]*)*(?:[\?])*(.+)*'))(handle_links)
|
||||
router.message(F.text.contains('http'))(handle_links)
|
||||
# repeater 模块
|
||||
router.message(F.chat.type.in_({'group', 'supergroup'}))(MessageRepeater().handle_message)
|
||||
|
||||
|
|
17
core/link.py
17
core/link.py
|
@ -165,21 +165,26 @@ async def process_url(url):
|
|||
else:
|
||||
# 对于其他链接,直接对其进行跟踪参数清理
|
||||
final_url = remove_tracking_params(extended_url)
|
||||
|
||||
return final_url
|
||||
|
||||
if url != final_url:
|
||||
return final_url
|
||||
return None
|
||||
|
||||
async def handle_links(message: Message):
|
||||
if not config.is_feature_enabled('link', message.chat.id):
|
||||
return
|
||||
# URL regex pattern
|
||||
url_pattern = r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'
|
||||
|
||||
text = message.text or message.caption
|
||||
# Extract URLs from message text
|
||||
if message.text:
|
||||
urls = re.findall(url_pattern, message.text)
|
||||
if text:
|
||||
urls = re.findall(url_pattern, text)
|
||||
if not urls:
|
||||
return
|
||||
final_urls = await asyncio.gather(*[process_url(url) for url in urls])
|
||||
|
||||
# Filter out None values
|
||||
final_urls = [url for url in final_urls if url is not None]
|
||||
|
||||
# 回复处理后的链接
|
||||
if final_urls:
|
||||
await message.reply(f"{"\n".join(final_urls)}\n消息里有包含跟踪参数的链接,已经帮你转换了哦")
|
Loading…
Add table
Add a link
Reference in a new issue