1

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

CallMeStag
  • 5,467
  • 1
  • 7
  • 22

1 Answers1

1

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)

  • the key take away here is that event has a property calls sender_id which tells the person who sent the message so whether message is from group or individual chat you will get the person who send this message – cryp tacker Jun 14 '21 at 08:59