How to forward messages of selected people from telegram group A to telegram group B using telethon

Viewed 644

So I'm able to send all the chats of specified group to another group with the help of telethon's telegramclient and events below is my code.

from telethon import TelegramClient, events
api_id = YOUR_ID
api_hash = YOUR_HASH
client = TelegramClient('anon', api_id, api_hash)

@client.on(events.NewMessage(chats=CHAT_ID_A))
async def handle_new_message(event):
    await client.send_message(CHAT_ID_B, event.raw_text)

Help me to refactor this code in such a way that I can send messages of only selected users from telegram group A to telegram group B

1 Answers

After a bit of searching adding the following inside event definition will to the trick

    sender_chat_id = event.sender_id
    if sender_chat_id == SELECTED_ID:
        await client.send_message(CHAT_ID_B, event.raw_text)

Related