4
31
Fork
You've already forked simplematrixbotlib
21

Handling large messages #61

Open
opened 2024年04月25日 22:18:40 +02:00 by Dreamsorcerer · 2 comments
Contributor
Copy link

matrix-rss-bridge silently fails when a blog post is too large (for example, matrix.org's post dated 19th April).

The first issue here, is that the API functions don't return anything:

await self._send_room(room_id=room_id,

Changing that function to return the result, I get an object that tells me it failed to send because the message was over the limit of 65,536. However, I'd expect an exception to be thrown if there was an error, so ideally this function could check the response and throw an exception if it failed.

Second issue is to have a convenient way to send such large messages automatically. It would be nice if it simply split the message nicely and sent it in parts without needing to implement that logic in each application.

This is the code I created so far:

while msg:
 if len(msg) > 30000:
 i = msg.rfind("\n", 0, 30000)
 part = msg[:i]
 msg = msg[i+1:]
 else:
 part = msg
 msg = ""
 await self._bot.api.send_markdown_message(room_id, part)

One problem is that I think .send_markdown_message() includes the content twice (as HTML and MD?), which means that I can't tell how big the message will actually be, but my actual limit is likely 30,000-35,000 bytes. I've just cut it at 30,000 bytes for now, which seems to be working, but may be splitting some messages unnecessarily.

This can also mess up formatting by cutting in the middle of a code/quote block or similar. Maybe it would be better to try and look for <h1> to split at, then falling back to h2 and h3 before falling back to \n.

matrix-rss-bridge silently fails when a blog post is too large (for example, matrix.org's post dated 19th April). The first issue here, is that the API functions don't return anything: https://codeberg.org/imbev/simplematrixbotlib/src/commit/fda7fa0f19876f9fa128624f3e4f4c644b7ccc43/simplematrixbotlib/api.py#L296 Changing that function to return the result, I get an object that tells me it failed to send because the message was over the limit of 65,536. However, I'd expect an exception to be thrown if there was an error, so ideally this function could check the response and throw an exception if it failed. Second issue is to have a convenient way to send such large messages automatically. It would be nice if it simply split the message nicely and sent it in parts without needing to implement that logic in each application. This is the code I created so far: ``` while msg: if len(msg) > 30000: i = msg.rfind("\n", 0, 30000) part = msg[:i] msg = msg[i+1:] else: part = msg msg = "" await self._bot.api.send_markdown_message(room_id, part) ``` One problem is that I think .send_markdown_message() includes the content twice (as HTML and MD?), which means that I can't tell how big the message will actually be, but my actual limit is likely 30,000-35,000 bytes. I've just cut it at 30,000 bytes for now, which seems to be working, but may be splitting some messages unnecessarily. This can also mess up formatting by cutting in the middle of a code/quote block or similar. Maybe it would be better to try and look for `<h1>` to split at, then falling back to h2 and h3 before falling back to `\n`.
Owner
Copy link

Will be added in v3

Will be added in v3
Author
Contributor
Copy link

So, a nuisance for this, is that I was trying to make the content of a feed viewable in an expandable section using <details>. But, if the content is too big and gets split, then you have half the content hidden in the first message and the rest displayed in the second part. Pretty annoying, but not sure I can think of a good solution.

So, a nuisance for this, is that I was trying to make the content of a feed viewable in an expandable section using `<details>`. But, if the content is too big and gets split, then you have half the content hidden in the first message and the rest displayed in the second part. Pretty annoying, but not sure I can think of a good solution.
Sign in to join this conversation.
No Branch/Tag specified
master
v3
send_reaction
v2.13.1
v2.13.0
v2.12.2
v2.12.1
v2.12.0
v2.11.0
v2.10.3
v2.10.2
v2.10.1
v2.10.0
v2.9.1
v2.9.0
v2.7.4
v2.7.3
v2.7.2
v2.7.0
v2.6.4
v2.6.3
v2.6.2
v2.6.1
v2.6.0
v1.6.0
v2.5.1
v2.5.0
v2.4.1
v2.4.0
v2.3.0
v2.2.0
v2.1.0
v2.0.0
v1.6.1
v1.5.3
v1.5.2
v1.5.1
v1.5.0
v1.4.0
v1.3.3
v1.3.1
v1.3.0
v1.2.0
v1.3.2
v1.0.1
v1.0.0
v1.1.0
Milestone
Clear milestone
No items
No milestone
Projects
Clear projects
No items
No project
Assignees
Clear assignees
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
imbev/simplematrixbotlib#61
Reference in a new issue
imbev/simplematrixbotlib
No description provided.
Delete branch "%!s()"

Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?