Hello world, in this article we’re going to explore discord webhooks and how can we build amazing things with them using Python so if you’re into adding more interactivity to your discord server please keep reading and let’s get right into it
What Is A Webhook ?
According to the wikipedia definition A webhook in web development is a method of augmenting or altering the behavior of a web page or web application with custom callbacks. These callbacks may be maintained, modified, and managed by third-party users and developers who may not necessarily be affiliated with the originating website or application.
Webhooks In The Current Context
In our case we’re going to generate a webhook for our discord server to send in an image online and will look into ways to automate the process and the things we can do with webhooks
There are mainly two ways to interact with Discord with code
- the Discord API using the programming language of your choice
- Webhooks
Main difference is that a Discord bot made using the API will allow more interactivity since it allows users to use commands specified by the coder and will only run when “called” unlike a webhook that runs every time you run your script
Another important difference is that webhooks won’t require obtaining any credentials or much code anyway
Now let’s make our first webhook and test it out !
First off go to your server settings as shown below
After that pick “integrations”
Then we’re going to click on “Webhooks” to create a new one
After clicking “New Webhook” you’ll be prompted to name your webhook and pick the channel it’ll send the messages in
The most import part is the WEBHOOK URL so copy that and let’s head to write some code
So how to use a webhook anyway ?
Discord webhooks are nothing but an endpoint we can make HTTP POST requests to following a specific JSON scheme you can find in this link
Using that knowledge, you can pretty much send ANY kind of data you could think of and in this example we’re going to send a simple image i found on pixabay.com and that’s just the beginning
In your favorite IDE / Text editor make a new .py file and let’s go over the script I’ve prepared for this article
So after observing the guide i linked above, i decided to take few of the attributes for our payload that we’re gonna send
requests
is the go to choice when working with HTTP requests in Python
The requsts.post()
method enables us to send POST requests and it requires two parameters : a url to which we’re gonna send the request to and the data to be sent
After running the script you should see the output in your server like this
CONGRATS !!!!!!! you’ve now successfully used your first Discord webhook and more amazing things to come :)
Ways To Improve This
So that was fairly simple to do and not much useful in reality so i decided to suggest SOME of the ideas to make a better use of those webhooks
- Host the script on PythonAnywhere.com so it runs 24/7 and add some scheduling using
schedule
- Scrape a website for some data you’re interested in and send the data into the respective channel
- Use an automation platform like IFTTT to integrate those webhooks with more real life devices
And the list goes on and on
BIG ANNOUNCEMENT
I’ll be making a similar project BUT will be fully automated on the cloud without the need to run the script manually if i see much support on this article
Conclusion
Webhooks are a nice way for us to interact with various online services and Discord allowed us to send messages in our server with just a simple POST request and this opens the door for so many ideas to spice up our servers and also improve our Python skills