Search code examples
pythondiscord

Sending private messages to discord


How to send a private message using a token without using a server bot. I looked through the POST request, but there is a link sent to the ID of the dialog with the user, how do I send a personal message via python by the user ID. For example, I have several user IDs to whom I want to send a message, how do I do this using python?


Solution

  • First we need to get the chat ID using the code::

    userid = 9999999
    USER_TOKEN = 'abcdf'
    payload = {
        "content": f"Text message."
    }
    u = 'https://discord.com/api/v9/users/@me/channels'
    d = {
        "recipients": [f"{userid}"]
    }
    header = {
        "authorization": USER_TOKEN
    }
    r = requests.post(url=u, json=d, headers=header) 
    jss = json.loads(r.text)
    

    And now we are already sending a request to send a message:

    url = f'https://discord.com/api/v9/channels/{jss["id"]}/messages' 
    r = requests.post(url,data=payload,headers=header)
    

    The message has been sent.)