visit
To get started, we need that Discord server. Log in to Discord, click on the Add Server button and proceed to create your own. Once that’s done, you should be able to see and open your Discord server
To make it easier to organize my projects and events, I usually create a new text channel for each project or, in some cases, one per type of event. To do so, click the + button next to text channels and create one. I’m going to call mine garage-door and make it a private channel.
Then, click on the settings icon for that channel, open the integration tab and create a new Webhook. Once added, you can see the Copy Webhook URL, and that’s precisely what we need to push our events! So copy that and paste it somewhere safe on your machine. Now, we are pretty much done with Discord, and we can move to writing some code! I have provided examples for Python and JavaScript; feel free to skip to whichever you find more relevant.
Sending events via Python
First, I would like to pass in my webhook URL as an environment variable, so I will set it to WEBHOOK_URL and use the standard library in python to access the value. You can, of course, skip this entire process and add your URL directly to the code.
export WEBHOOK_URL=//discord.com/api/...
Once we have set the environment variable, we can access it by importing os and using the os.environ.get method.
import os
WEBHOOK_URL = os.environ.get('WEBHOOK_URL')
print(WEBHOOK_URL)
I will be using the
pip install requests
Finally, all we need to do is to import requests and make a POST request to the webhook url and pass our event in the JSON body with the content key.
import requests
requests.post(WEBHOOK_URL, { "content": "🦄 garage door is open" })
Once we run this code, we should get a new message in the garage-door channel telling us that our garage door has been opened.
As you can see, getting this set up is very easy while being quite powerful! I use this setup in almost all of my projects to be aware of how they are doing!
Also Published