Commit 707bc3e
Changed files (2)
src
messages
others
src/messages/chat_history.py
@@ -7,22 +7,14 @@ from pyrogram.types import Message
from messages.parser import parse_msg
-async def get_chat_history(client: Client, message: Message, offset_id: int, num_history: int = 0) -> list[str]:
- """Get given number of chat history in text format."""
+async def get_chat_history(client: Client, message: Message, offset_id: int, num_history: int = 0) -> list[dict]:
+ """Get given number of chat history in parserd json format."""
if num_history <= 0:
return []
history = []
async for msg in client.get_chat_history(chat_id=message.chat.id, offset_id=offset_id, limit=num_history): # type: ignore
if msg.empty:
continue
-
info = parse_msg(msg, silent=True)
- res = ""
- if info["full_name"]:
- res += f"@{info['full_name']} "
- res += f"{info['time']}\n"
- media = f"[{msg.media.name}]" if msg.media else ""
- res += f"{media}{info['text']}"
- if res.strip():
- history.append(res)
+ history.append(info)
return history[::-1]
src/others/combine_history.py
@@ -15,14 +15,20 @@ from utils import to_int
HELP = f"""
💬**合并对话历史**
使用说明:
-`{PREFIX.COMBINATION} + #Number` (最多{COMBINATION_MAX_HISTORY}条)
+1. `{PREFIX.COMBINATION} + #N` (最多{COMBINATION_MAX_HISTORY}条)
将最近的N条消息文本合并为txt文件
-如果以 `{PREFIX.COMBINATION} + #Number` 回复消息M
+2. `{PREFIX.COMBINATION} + #N + @User` (最多{COMBINATION_MAX_HISTORY}条)
+将最近的N条消息中只属于User发送的消息合并为txt文件
+
+如果以 `{PREFIX.COMBINATION} + #N` (或附加User) 回复消息M
则合并消息M之前的N条消息文本 (包含M)
示例:
-`{PREFIX.COMBINATION} #10`: 合并最近10条消息文本
+1. `{PREFIX.COMBINATION} #10`: 合并最近10条消息为txt文本
+2. `{PREFIX.COMBINATION} #20 @123456`: 合并最近20条消息中UID为12345678发送的消息为txt文本
+3. `{PREFIX.COMBINATION} #20 @John`: 合并最近20条消息中用户名为John(大小写均可)发送的消息为txt文本
+如果用户名中有空格, 请去除空格。例如: 想指定用户名为John Doe请使用 `@JohnDoe`
"""
@@ -39,8 +45,14 @@ async def combine_history(client: Client, message: Message, **kwargs):
# get the number of messages to combine
num_history = 0
- if matched := re.match(r"^" + PREFIX.COMBINATION + r"\s+#(\d+)", message.text):
+ if matched := re.match(r"^" + PREFIX.COMBINATION + r"\s+#(\d+)\s+@(\w+)", message.text):
+ num_history = int(matched.group(1))
+ filter_user = str(matched.group(2))
+ file_name = f"最近{num_history}条消息中{filter_user}的发言.txt"
+ elif matched := re.match(r"^" + PREFIX.COMBINATION + r"\s+#(\d+)", message.text):
num_history = int(matched.group(1))
+ filter_user = ""
+ file_name = f"最近{num_history}条消息记录.txt"
else:
await send2tg(client, message, texts=HELP, **kwargs)
return
@@ -51,7 +63,19 @@ async def combine_history(client: Client, message: Message, **kwargs):
message = message.reply_to_message
offset_id = message.id + 1 # include the reply message
history = await get_chat_history(client, message, offset_id, num_history)
- combination = "\n\n".join(history)
+ if filter_user:
+ history = [x for x in history if x["full_name"].replace(" ", "").lower() == filter_user.lower() or str(x["uid"]) == filter_user]
+ if not history:
+ await send2tg(client, message, texts=f"最近{num_history}条消息中未找到符合条件的消息", **kwargs)
+ return
+ combination = ""
+ for info in history:
+ if info["full_name"]:
+ combination += f"@{info['full_name']} "
+ combination += f"{info['time']}\n"
+ media = f"[{info['mtype']}]" if info["mtype"] != "text" else ""
+ combination += f"{media}{info['text']}"
+ combination += "\n\n"
length = len(combination)
reading_minutes = length / READING_SPEED # minutes
@@ -62,7 +86,7 @@ async def combine_history(client: Client, message: Message, **kwargs):
await client.send_document(
to_int(target_chat),
f,
- file_name=f"最近{num_history}条消息历史.txt",
+ file_name=file_name,
reply_parameters=reply_parameters,
caption=f"总字符: {length}\n 预计时长: {reading_minutes:.1f}分钟",
)