Search code examples
pythontelegramtelethon

Using Telethon to forward messages to a topic/thread in a different group


So I am trying to create a simple script that forwards messages from one Telegram group to a Topic or a Thread in a different group... I have the following but it never seems to forward messages, and I am not sure how to specify the topic I prefer it to forward too

from telethon import TelegramClient, events

API_ID = '2####1'
API_HASH = '9#######7'
SESSION_NAME = 'Forward-Rose-and-Kim'

SOURCE_GROUP_ID = -948809343
DESTINATION_GROUP_ID = -1001805701971

## I'm not sure if there should be an extra variable, I tried using DESTINATION_GROUP_ID = -1001805701971/6 with '6' being the topic I... but this doesn't work as well

client = TelegramClient(SESSION_NAME, API_ID, API_HASH)

async def forward_message_to_destination(message):
    await client.send_message(DESTINATION_GROUP_ID, message)
    print(f"Message forwarded: {message.text}")

@client.on(events.NewMessage(chats=SOURCE_GROUP_ID))
async def forward_messages(event):
    message = event.message
    await forward_message_to_destination(message)

with client:
    client.run_until_disconnected() 

just for sake of clarify, this works forwarding from a group to a group, but not to a topic in a group and I am not sure how to point where I want the message to go within DESTINATION_GROUP_ID = -1001805701971 as there are like 6 or 7 different topics in that channel


Solution

  • updated to await client.send_message(DESTINATION_GROUP_ID, message, reply_to=6) and working perfect now

    The relpy_to=6 being the thread ID