Skip to content

Bug in forwardMessages when forwarding deleted messages (Channels) #841

@souome

Description

@souome

Bug in forwardMessages when forwarding deleted messages (Channels only)

Context

I was initially using forwardMessages endpoint to make sure some messages that were to be deleted manually by an user had actually been deleted and i came across with this bug.
As i wrote this i noticed this should be related to this issue #714

Some remakrs

When trying to replicate and figure out this bug i noticed:

  • I was only able to do it on a channels (private), it does not happen on groups... not sure about private bot dm....
  • Happens with both isolated messages and mediaGroupMessages

The Bug

According to the documentation forwardMessages skips messages not found...

Lets say you make a bot send 3 messages to a channel and save their message ids,
for example:

  • test message 1 (text)
  • test message 2 (text)
  • test message 3 (text)

Then you manually go to the channel on the telegram app and delete 1 of the messages, say for example message 3

Then you try to use forwardMessages using all the original 3 message ids
The expected behaviour would be that it ignores message 3 forwarding the first 2 and returning success. This is what happens on groups as i mentioned before.
Instead it returns Bad Request: failed to send message #1 with the error message "Message was not forwarded" (it does forward the messages tho)

Weird subsequent requests behaviour

What makes this even more weird is that if we repeat the forward request right after the first one in the exact same conditions without touching anything it just magically works returning success... (it works on multiple subsequent request, so just the 1st one fails...)

Replicate

  1. Create a channel and add a bot to it as admin
  2. Make it send multiple messages or a group of them and save the message ids
  3. Manually delete 1 of them, or more (as long as you don't delete all)
  4. Call the API endpoint forwardMessages with all the original ids
  5. Should forward the messages but throw a Bad Request Error
  6. Call the endpoint again with the exact same ids
  7. Should forward the message and return success

To debug i used AI to generate this script:

import requests
import time

BOT_TOKEN   = "XXXXXXXXXXXXXXXXXXXXXX"
CHAT_ID     = "-1XXXXXXXXXXXX"
BASE_URL    = f"https://api.telegram.org/bot{BOT_TOKEN}"

def send_message(text: str) -> int:
    resp = requests.post(f"{BASE_URL}/sendMessage", json={
        "chat_id": CHAT_ID,
        "text":    text,
    })
    data = resp.json()
    assert data["ok"], f"sendMessage failed: {data}"
    return data["result"]["message_id"]

def forward_messages(message_ids: list[int]) -> dict:
    resp = requests.post(f"{BASE_URL}/forwardMessages", json={
        "chat_id":      CHAT_ID,
        "from_chat_id": CHAT_ID,
        "message_ids":  sorted(message_ids),
    })
    return resp.json()


if __name__ == "__main__":
    # Step 1 — send 3 messages
    print("Sending 3 messages...")
    ids = []
    for i in range(1, 4):
        msg_id = send_message(f"Test message {i}")
        ids.append(msg_id)
        print(f"  Sent message {i} → ID {msg_id}")

    print(f"\nMessage IDs: {ids}")

    # Step 2 — prompt user to delete
    print(f"\nGo to the chat and manually delete one of these messages: {ids}")
    input("Press Enter when done...")

    # Step 3 — attempt to forward
    print(f"\nForwarding message IDs: {sorted(ids)}\n")

    for i in range (1, 3):

        result = forward_messages(ids)
        print(f"ok     : {result.get('ok')}")
        if not result.get("ok"):
            print(f"error  : {result.get('description')}")
        else:
            forwarded_ids = [m["message_id"] for m in result["result"]]
            print(f"got    : {len(forwarded_ids)} forwards → {forwarded_ids}")

            time.sleep(1)
        print()

Visuals

Image Image

Then manually delete message 3.
Image

Then hit enter on the script.
Image

The really weird thing here is that the first forwarding seems to affect the subsequent requests, like some sort of caching behaviour...
There is also some weird behaviour when you forward the messages to a different group instead and this subsequent success seems to disappear and keeps failing with the same Bad Request message (not sure this is because its because i used a group as the forward target in this case or because chat_id != from_chat_id.

Sorry for the long issue, i tried to give as much context and information i gathered as possible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions