Telethon icon indicating copy to clipboard operation
Telethon copied to clipboard

The logical contradiction in the logic code for obtaining real-time data results in only being able to obtain the latest real-time message

Open sanmensrs opened this issue 9 months ago • 2 comments

Code that causes the issue

`

async for m in client.iter_messages(chat, from_user=from_user, offset_date=offset_date, reverse=False,scheduled=True):
    _message_queue.put((chat, chat_entity, m))

`

Expected behavior

can traverse all scheduled messages in the session

Actual behavior

Only the latest scheduled message sent for each session can be obtained

Traceback

all scheduled messages are placed in “_message_queue”,but only the latest message sent can be obtained. The reason is that self.reverse cannot be set to True, resulting in line 176 in telethon/client/message "self.last_id = 0 if self.reverse else float('inf')" self.last_id is float('inf') In the traversal loop of the following code, the second traversal returns True after judging self._message_in_range(message), resulting in only the latest message being obtained each time.

`

       for message in messages:
            if (isinstance(message, types.MessageEmpty)
                    or self.from_id and message.sender_id != self.from_id):
                continue
      
            if not self._message_in_range(message):
                return True
            # There has been reports that on bad connections this method
            # was returning duplicated IDs sometimes. Using ``last_id``
            # is an attempt to avoid these duplicates, since the message
            # IDs are returned in descending order (or asc if reverse).
            self.last_id = message.id
            message._finish_init(self.client, entities, self.entity)
            self.buffer.append(message)

`

Check the processing logic in the method. The first time the function is entered, self.last_id is positive infinity. The second time the function is entered, the outer loop has reassigned self.last_id, "self.last_id = message.id", resulting in self.last_id being 1 when entering the function for the second time. However, message.id is 2 at this time, and "message.id >= self.last_id" is judged as True, causing the loop to return directly without processing the next data. `

      def _message_in_range(self, message):
          """
          Determine whether the given message is in the range or
          it should be ignored (and avoid loading more chunks).
          """
          # No entity means message IDs between chats may vary
          if self.entity:
              if self.reverse:
                  if message.id <= self.last_id or message.id >= self.max_id:
                      return False

              else:
                  if message.id >= self.last_id or message.id <= self.min_id:
                      return False

          return True

`

There is a code logic conflict now. When scheduled=True, reverse cannot be True, otherwise it will cause an error that self.request lacks the add_offset attribute. if self.reverse: self.request.add_offset -= _MAX_CHUNK_SIZE

Telethon version

1.36.0

Python version

3.8.10

Operating system (including distribution name and version)

windows 10

Other details

No response

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 an open duplicate.
  • [x] I ran pip install -U https://github.com/LonamiWebs/Telethon/archive/v1.zip and triggered the bug in the latest version.

sanmensrs avatar Mar 17 '25 03:03 sanmensrs

Thanks for the details. Feel free to send a PR.

Lonami avatar Mar 17 '25 07:03 Lonami