Search code examples
pythonpyrogram

Pyrogram, How to send /start with params


I have url https://t.me/Bot_name?start=mc_dSs8EMrECY1Jvwk How can I follow this link using Program. Or how to send a telegram message to the bot /start with param mc_dSs8EMrECY1Jvwk

I tried using client.join_chat(url) the telegram server gives an invalid username error. also try create inlinebutton with url and clicked on it, but nothing happens


Solution

  • Kind of an hacky solution, but probably the only thing you could do is just parsing the URL and extracting the start GET argument. Then you can use the messages.startBot (https://docs.pyrogram.org/telegram/functions/messages/start-bot) method to send /start <startarg> to your bot.

    Example:

    from pyrogram.raw import functions
    from urllib.parse import urlparse, parse_qs
    
    def parse_url(url: str):
        parsed = urlparse(url)
        return parsed.path[1:], parse_qs(parsed.query)['start']
    
    username, start_param = parse_url(my_url)
    
    client.invoke(
        functions.messages.StartBot(
            bot=client.resolve_peer(username),
            start_param=start_param
        )
    )