Telethon icon indicating copy to clipboard operation
Telethon copied to clipboard

Editing only link_preview of message gives MessageNotModifiedError

Open udf opened this issue 5 years ago • 4 comments

Checklist

  • [x] The error is in the library's code, and not in my own.
  • [x] I have searched for this issue before posting it and there isn't a duplicate.
  • [x] I ran pip install -U https://github.com/LonamiWebs/Telethon/archive/master.zip and triggered the bug in the latest version.

Code that causes the issue

import asyncio

from telethon import TelegramClient

from telethon.errors import MessageNotModifiedError
from telethon.tl.functions.messages import EditMessageRequest


async def main():
    client = TelegramClient('walter', 6, "eb06d4abfb49dc3eeb1aeb98ae0f581e")
    await client.start()

    m = await client.send_message('me', 'https://en.wikipedia.org/wiki/Telegram_(software)')

    # later on in some event handler...
    # doesn't work (MessageNotModifiedError)
    try:
        await m.edit(link_preview=False)
    except MessageNotModifiedError as e:
        print(e)

    # doesn't work (same request gets made above)
    try:
        await client(EditMessageRequest(
            peer=await m.get_input_chat(),
            id=m.id,
            no_webpage=True
        ))
    except MessageNotModifiedError as e:
        print(e)

    # works (provided message and entities)
    await client(EditMessageRequest(
        peer=await m.get_input_chat(),
        id=m.id,
        message=m.message,
        entities=m.entities,
        no_webpage=True
    ))


asyncio.run(main())

EditMessageRequest seems to fail if message and entities are not present. This happens because we pass only the message id to edit_message from Message.edit: https://github.com/LonamiWebs/Telethon/blob/3c56a6db4d54ddb2ae7a9063f08cc9c2a61080fc/telethon/tl/custom/message.py#L734-L737

A fix would be to give edit_message the message text if link_preview was provided, but edit_message expects markdown/html text and I don't know how we would provide raw entities/text to edit_message.

udf avatar Aug 10 '20 14:08 udf

We can modify client.edit_messages to accept a "private" entities parameter which would override the parsing, and from message.edit we just pass the .raw_text and .entities which we have. The same check would need to be done for edit_message, if a Message was given.

The annoying part is dealing with the *args and **kwargs to find out if the user provided another text message or not, though I guess we could maybe copy the function definition (not in favour because updating one will make it harder to remember to update the other).

Another place to be careful is CallbackQuery.

Lonami avatar Aug 10 '20 14:08 Lonami

What if we passed the entire message object to client.edit_message and then used the text and entities if we need to? (obviously with type checking instead of getattr hacks)

diff --git a/telethon/client/messages.py b/telethon/client/messages.py
index 8237c380..23e37790 100644
--- a/telethon/client/messages.py
+++ b/telethon/client/messages.py
@@ -1049,9 +1049,9 @@ class MessageMethods:
         request = functions.messages.EditMessageRequest(
             peer=entity,
             id=utils.get_message_id(message),
-            message=text,
+            message=text or getattr(message, 'message'),
             no_webpage=not link_preview,
-            entities=msg_entities,
+            entities=msg_entities or getattr(message, 'entities'),
             media=media,
             reply_markup=self.build_reply_markup(buttons),
             schedule_date=schedule
diff --git a/telethon/tl/custom/message.py b/telethon/tl/custom/message.py
index 5f286a40..162b1d04 100644
--- a/telethon/tl/custom/message.py
+++ b/telethon/tl/custom/message.py
@@ -732,7 +732,7 @@ class Message(ChatGetter, SenderGetter, TLObject, abc.ABC):
             kwargs['buttons'] = self.reply_markup
 
         return await self._client.edit_message(
-            await self.get_input_chat(), self.id,
+            await self.get_input_chat(), self,
             *args, **kwargs
         )

Not sure if this would break anything.

udf avatar Aug 10 '20 15:08 udf

That would break with the following code:

client.edit_message(chat, message_obj, new_text)

Lonami avatar Aug 10 '20 15:08 Lonami

Sorry if it is not what you are discussing , i am bit new to this field so just thought it is good place to put issue.

Getting this error : telethon.errors.rpcerrorlist.MessageNotModifiedError: Content of the message was not modified (caused by EditMessageRequest)

I am trying to edit message from callbackQuery , sometime it works but sometime it is throwing error.

In NewMessage Event function global x x = await botclient.send_message(chat, "Messaage")

In CallBackQuery event function global x await x.edit("Message", buttons=[[Button.url("Support Group","https://t.me/....")]])

shivamsaksham avatar May 08 '22 03:05 shivamsaksham

I'm going to close this issue but please let me know if you think it should be opened again, after confirming it still occurs in the current git version

Lonami avatar Apr 06 '23 12:04 Lonami