1
1
import logging
2
- from typing import Dict , List
2
+ from typing import Dict , List , Tuple
3
3
from typing import NamedTuple
4
4
5
5
from aiogram import Bot , types
@@ -52,7 +52,7 @@ async def walk_message_tree(node: Message, depth: int = 0):
52
52
return result
53
53
54
54
55
- def parse_text (message_ : ParsedMessage , attachment_storage : List ) -> str :
55
+ def parse_text (message_ : ParsedMessage , attachment_storage : List [ Tuple [ str , MessageAttachment ]] ) -> str :
56
56
if not message_ .text and not message_ .attachments :
57
57
return ""
58
58
if message_ .text :
@@ -63,10 +63,11 @@ def parse_text(message_: ParsedMessage, attachment_storage: List) -> str:
63
63
attach_names = {"doc" : "документ" , "photo" : "фото" }
64
64
65
65
for attach_ in message_ .attachments :
66
- attachment_storage .append (attach_ )
67
- text += '{indent}<u>{{{name}_{id}}}</u>\n ' .format (indent = " " * message_ .indent ,
68
- name = attach_names [str (attach_ .type )],
69
- id = len (attachment_storage ))
66
+ name = "{name}_{id}" .format (name = attach_names [str (attach_ .type )],
67
+ id = len (attachment_storage ))
68
+ attachment_storage .append ((name , attach_ ))
69
+ text += '{indent}<u>{{{name}}}</u>\n ' .format (indent = " " * message_ .indent ,
70
+ name = name )
70
71
return text
71
72
72
73
@@ -75,10 +76,12 @@ async def handle_nested(
75
76
message : Message
76
77
):
77
78
"""Handle nested forwarded messages"""
78
- attachments : List [MessageAttachment ] = []
79
+ attachments : List [Tuple [str , MessageAttachment ]] = []
80
+ message_references : Dict [str , str ] = {}
79
81
tree = await get_tree (message )
80
82
logger .debug ("Got tree" )
81
83
84
+ # Prepare text, insert {} and append to attachments
82
85
message_text = ""
83
86
for msg in tree :
84
87
message_text += "{indent}{sender}:\n {text}" .format (
@@ -88,28 +91,31 @@ async def handle_nested(
88
91
# Avoid \n on empty text
89
92
text = parse_text (message_ = msg , attachment_storage = attachments )
90
93
)
94
+ logger .debug ("Prepared text, sending attachments..." )
91
95
92
- logger .debug ("Parsed messages" )
93
-
94
- await bot .send_message (chat_id = data_config .destination_id ,
95
- text = message_text ,
96
- parse_mode = data_config .parse_mode )
97
-
98
- logger .info ("Sent text, sending docs..." )
99
-
100
- for num , attach in enumerate (attachments , 1 ):
96
+ # Send attachments
97
+ for name , attach in attachments :
101
98
if attach .type == "photo" :
102
99
source = max (attach .photo .sizes , key = lambda size : size .width )
103
- await bot .send_photo (chat_id = data_config .destination_id ,
104
- caption = f"фото_ { num } " ,
105
- photo = source .url ,
106
- parse_mode = data_config .parse_mode )
100
+ resp = await bot .send_photo (chat_id = data_config .destination_id ,
101
+ caption = name ,
102
+ photo = source .url ,
103
+ parse_mode = data_config .parse_mode )
107
104
logger .debug ("sent photo" )
108
- elif attach . type == "doc" :
105
+ else :
109
106
data = types .InputFile .from_url (url = attach .doc .url ,
110
107
filename = attach .doc .title )
111
- await bot .send_document (chat_id = data_config .destination_id ,
112
- caption = f"документ_ { num } " ,
113
- document = data ,
114
- parse_mode = data_config .parse_mode )
108
+ resp = await bot .send_document (chat_id = data_config .destination_id ,
109
+ caption = name ,
110
+ document = data ,
111
+ parse_mode = data_config .parse_mode )
115
112
logger .debug ("sent document" )
113
+
114
+ message_references [name ] = f'<a href="https://t.me/c/{ str (resp .chat .id )[2 :]} /{ resp .message_id } ">{ name } </a>'
115
+ logger .debug ("Sent attachments, sending text..." )
116
+
117
+ await bot .send_message (chat_id = data_config .destination_id ,
118
+ text = message_text .format (** message_references ),
119
+ parse_mode = data_config .parse_mode )
120
+
121
+ logger .info ("Sent text" )
0 commit comments