misskey2telegram/defs/chat.py

161 lines
4.8 KiB
Python
Raw Normal View History

2023-01-17 17:11:26 +00:00
from typing import Optional
from mipac import ChatMessage, File
from mipac.models.lite import LiteUser
from pyrogram.errors import MediaEmpty
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton
from init import bot, request
2023-01-27 12:36:41 +00:00
from models.services.scheduler import add_delete_file_job, delete_file
2023-01-17 17:11:26 +00:00
2023-07-20 14:21:37 +00:00
def get_user_link(host: str, user: LiteUser) -> str:
2023-01-17 17:11:26 +00:00
if user.host:
2023-07-20 14:21:37 +00:00
return f"https://{host}/@{user.username}@{user.host}"
return f"https://{host}/@{user.username}"
2023-01-17 17:11:26 +00:00
2023-07-20 14:21:37 +00:00
def get_source_link(host: str, message: ChatMessage) -> str:
2023-01-17 17:11:26 +00:00
return (
2023-07-20 14:21:37 +00:00
f"https://{host}/my/messaging/{message.user.username}?cid={message.user.id}"
2023-01-17 17:11:26 +00:00
if not message.group and message.user
2023-07-20 14:21:37 +00:00
else f"https://{host}/my/messaging/group/{message.group.id}"
2023-01-17 17:11:26 +00:00
)
2023-07-20 14:21:37 +00:00
def gen_button(host: str, message: ChatMessage):
author = get_user_link(host, message.user)
source = get_source_link(host, message)
2023-01-17 17:11:26 +00:00
first_line = [
InlineKeyboardButton(text="Chat", url=source),
InlineKeyboardButton(text="Author", url=author),
]
return InlineKeyboardMarkup([first_line])
2023-07-20 14:21:37 +00:00
def get_content(host: str, message: ChatMessage) -> str:
2023-01-17 17:11:26 +00:00
content = message.text or ""
content = content[:768]
2023-07-20 14:21:37 +00:00
user = f'<a href="{get_user_link(host, message.user)}">{message.user.nickname}</a>'
2023-01-17 17:11:26 +00:00
if message.group:
2023-07-20 14:21:37 +00:00
group = f'<a href="{get_source_link(host, message)}">{message.group.name}</a>'
2023-01-17 17:11:26 +00:00
user += f" ( {group} )"
return f"""<b>Misskey Message</b>
{user} <code>{content}</code>"""
2023-07-20 14:21:37 +00:00
async def send_text(
host: str, cid: int, message: ChatMessage, reply_to_message_id: int
):
2023-01-17 17:11:26 +00:00
await bot.send_message(
cid,
2023-07-20 14:21:37 +00:00
get_content(host, message),
2023-01-17 17:11:26 +00:00
reply_to_message_id=reply_to_message_id,
2023-07-20 14:21:37 +00:00
reply_markup=gen_button(host, message),
2023-01-17 17:11:26 +00:00
disable_web_page_preview=True,
)
def deprecated_to_text(func):
async def wrapper(*args, **kwargs):
try:
return await func(*args, **kwargs)
except MediaEmpty:
2023-07-20 14:21:37 +00:00
return await send_text(args[0], args[1], args[3], args[4])
2023-01-17 17:11:26 +00:00
return wrapper
@deprecated_to_text
2023-07-03 14:39:52 +00:00
async def send_photo(
2023-07-20 14:21:37 +00:00
host: str, cid: int, url: str, message: ChatMessage, reply_to_message_id: int
2023-07-03 14:39:52 +00:00
):
2023-01-17 17:11:26 +00:00
if not url:
2023-07-20 14:21:37 +00:00
return await send_text(host, cid, message, reply_to_message_id)
2023-01-17 17:11:26 +00:00
await bot.send_photo(
cid,
url,
reply_to_message_id=reply_to_message_id,
2023-07-20 14:21:37 +00:00
caption=get_content(host, message),
reply_markup=gen_button(host, message),
2023-01-17 17:11:26 +00:00
)
@deprecated_to_text
2023-07-03 14:39:52 +00:00
async def send_video(
2023-07-20 14:21:37 +00:00
host: str, cid: int, url: str, message: ChatMessage, reply_to_message_id: int
2023-07-03 14:39:52 +00:00
):
2023-01-17 17:11:26 +00:00
if not url:
2023-07-20 14:21:37 +00:00
return await send_text(host, cid, message, reply_to_message_id)
2023-01-17 17:11:26 +00:00
await bot.send_video(
cid,
url,
reply_to_message_id=reply_to_message_id,
2023-07-20 14:21:37 +00:00
caption=get_content(host, message),
reply_markup=gen_button(host, message),
2023-01-17 17:11:26 +00:00
)
@deprecated_to_text
2023-07-03 14:39:52 +00:00
async def send_audio(
2023-07-20 14:21:37 +00:00
host: str, cid: int, url: str, message: ChatMessage, reply_to_message_id: int
2023-07-03 14:39:52 +00:00
):
2023-01-17 17:11:26 +00:00
if not url:
2023-07-20 14:21:37 +00:00
return await send_text(host, cid, message, reply_to_message_id)
2023-01-17 17:11:26 +00:00
await bot.send_audio(
cid,
url,
reply_to_message_id=reply_to_message_id,
2023-07-20 14:21:37 +00:00
caption=get_content(host, message),
reply_markup=gen_button(host, message),
2023-01-17 17:11:26 +00:00
)
async def fetch_document(file: File) -> Optional[str]:
file_name = f"downloads/{file.name}"
file_url = file.url
if file.size > 10 * 1024 * 1024:
return file_url
if not file_url:
return file_url
req = await request.get(file_url)
if req.status_code != 200:
return file_url
with open(file_name, "wb") as f:
f.write(req.content)
add_delete_file_job(file_name)
return file_name
@deprecated_to_text
2023-07-03 14:39:52 +00:00
async def send_document(
2023-07-20 14:21:37 +00:00
host: str, cid: int, file: File, message: ChatMessage, reply_to_message_id: int
2023-07-03 14:39:52 +00:00
):
2023-01-17 17:11:26 +00:00
file = await fetch_document(file)
if not file:
2023-07-20 14:21:37 +00:00
return await send_text(host, cid, message, reply_to_message_id)
2023-01-17 17:11:26 +00:00
await bot.send_document(
cid,
file,
reply_to_message_id=reply_to_message_id,
2023-07-20 14:21:37 +00:00
caption=get_content(host, message),
reply_markup=gen_button(host, message),
2023-01-17 17:11:26 +00:00
)
await delete_file(file)
2023-07-20 14:21:37 +00:00
async def send_chat_message(host: str, cid: int, message: ChatMessage, topic_id: int):
2023-01-17 17:11:26 +00:00
if not message.file:
2023-07-20 14:21:37 +00:00
return await send_text(host, cid, message, topic_id)
2023-01-17 17:11:26 +00:00
file_url = message.file.url
file_type = message.file.type
if file_type.startswith("image"):
2023-07-20 14:21:37 +00:00
await send_photo(host, cid, file_url, message, topic_id)
2023-01-17 17:11:26 +00:00
elif file_type.startswith("video"):
2023-07-20 14:21:37 +00:00
await send_video(host, cid, file_url, message, topic_id)
2023-01-17 17:11:26 +00:00
elif file_type.startswith("audio"):
2023-07-20 14:21:37 +00:00
await send_audio(host, cid, file_url, message, topic_id)
2023-01-17 17:11:26 +00:00
else:
2023-07-20 14:21:37 +00:00
await send_document(host, cid, message.file, message, topic_id)