简体   繁体   中英

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

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

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 technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM