Skip to content
This repository was archived by the owner on Sep 1, 2022. It is now read-only.

Commit 8a78d2b

Browse files
committed
v2.5
Added message reference links in nested handler Code cleanup
1 parent ac8782f commit 8a78d2b

File tree

4 files changed

+34
-36
lines changed

4 files changed

+34
-36
lines changed

forwarding_bot/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
__author__ = "Alexey Artishevskiy"
33
__copyright__ = f"Copyright 2020, {__author__}"
44
__license__ = "MIT"
5-
__version__ = "2.4"
5+
__version__ = "2.5"
66
__maintainer__ = __author__
77
__email__ = "1337kwiz@gmail.com"

forwarding_bot/vk/_blueprint/attachment_handlers.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,7 @@
88
logger = logging.getLogger("forwarding-bot")
99

1010

11-
async def no_attachments(
12-
chat_id: int,
13-
message_text: str
14-
):
15-
bot = Bot(data_config.bot_token)
16-
await bot.send_message(chat_id=chat_id, text=message_text)
17-
18-
19-
async def one_attachment(
11+
async def handle_attachment(
2012
bot: Bot,
2113
message_text: str,
2214
msg_attachment: MessageAttachment,

forwarding_bot/vk/_blueprint/blueprint.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ async def handler(message: Message) -> None:
3636
text=formatted_message,
3737
parse_mode=data_config.parse_mode)
3838
elif len(attachments) == 1 and not message.fwd_messages:
39-
await attachment_handlers.one_attachment(
39+
await attachment_handlers.handle_attachment(
4040
bot,
4141
formatted_message,
4242
attachments[0]

forwarding_bot/vk/_blueprint/nested_handlers.py

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logging
2-
from typing import Dict, List
2+
from typing import Dict, List, Tuple
33
from typing import NamedTuple
44

55
from aiogram import Bot, types
@@ -52,7 +52,7 @@ async def walk_message_tree(node: Message, depth: int = 0):
5252
return result
5353

5454

55-
def parse_text(message_: ParsedMessage, attachment_storage: List) -> str:
55+
def parse_text(message_: ParsedMessage, attachment_storage: List[Tuple[str, MessageAttachment]]) -> str:
5656
if not message_.text and not message_.attachments:
5757
return ""
5858
if message_.text:
@@ -63,10 +63,11 @@ def parse_text(message_: ParsedMessage, attachment_storage: List) -> str:
6363
attach_names = {"doc": "документ", "photo": "фото"}
6464

6565
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)
7071
return text
7172

7273

@@ -75,10 +76,12 @@ async def handle_nested(
7576
message: Message
7677
):
7778
"""Handle nested forwarded messages"""
78-
attachments: List[MessageAttachment] = []
79+
attachments: List[Tuple[str, MessageAttachment]] = []
80+
message_references: Dict[str, str] = {}
7981
tree = await get_tree(message)
8082
logger.debug("Got tree")
8183

84+
# Prepare text, insert {} and append to attachments
8285
message_text = ""
8386
for msg in tree:
8487
message_text += "{indent}{sender}:\n{text}".format(
@@ -88,28 +91,31 @@ async def handle_nested(
8891
# Avoid \n on empty text
8992
text=parse_text(message_=msg, attachment_storage=attachments)
9093
)
94+
logger.debug("Prepared text, sending attachments...")
9195

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:
10198
if attach.type == "photo":
10299
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)
107104
logger.debug("sent photo")
108-
elif attach.type == "doc":
105+
else:
109106
data = types.InputFile.from_url(url=attach.doc.url,
110107
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)
115112
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

Comments
 (0)