skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    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


  2. please help, does not forward photos to Themes

    import json
    from telethon.sync import TelegramClient, functions, events
    
    api_id = 123456
    api_hash = "12345678901234567890"
    
    
    async def main():
    async with TelegramClient('anon', api_id, api_hash) as client:
        with open('channels_and_topics.json', 'r') as f:
            config = json.load(f)
    
        source_dest_pairs = config['source_dest_pairs']
    
        @client.on(events.NewMessage)
        async def handler(event):
            for pair in source_dest_pairs:
                if event.chat.username == pair['source']:
                    group_chat = pair['group_chat']
                    topic_id = pair['topic_id']
    
                    await client.send_message(
                        entity=group_chat,
                        message=event.message.message,
                        reply_to=event.message.id,
                        grouped_id=topic_id
                    )
    
        await client.run_until_disconnected()
    
    if __name__ == '__main__':
    import asyncio
    asyncio.run(main())

    file contents ‘channels_and_topics.json’

    {
    "source_dest_pairs": [“
    {
    "source": "@test_ist",
    "destination": "@test_grup_global",
    "topic_id": 24
    },
    {
    "source": "@test_ista",
    "destination": "@test_grup_global" ,
    "topic_id": 2
    }
    ]
    }

    Blockquote

    Blockquote

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search