Search code examples
telethontelegram-api

Telethon Get Update When Contact Join Telegram


I'm using Telethon and i'm trying to implement a handler to listen events when a contact of mine joined to Telegram, but i don't find any documentation about that.

I see the docs of telethon updates events Telethon Doc - Update Events but i don't find a way to filter this specific event. Right now Telegram send a push notification when any of your contact join telegram and create a chat, so i think there should be an event that marks this

Can you help me?

Thanks!!


Solution

  • Listen to events.Raw and do a few checks on the event object that should look like this.

    import asyncio
    from telethon import TelegramClient, events
    from telethon.tl.types import MessageActionContactSignUp, UpdateNewMessage
    
    client = TelegramClient('YOUR_SESSION_NAME', 'YOUR_API_ID', 'YOUR_API_HASH')
    client.start()
    
    @events.register(events.Raw)
    async def contact_join(event):
        if isinstance(event, UpdateNewMessage) and isinstance(event.message.action, MessageActionContactSignUp):
            print(event.message.from_id) #show the id of the contact joined
    
    loop = asyncio.get_event_loop()
    client.add_event_handler(check_join)
    loop.run_forever()