The simplest way to send Discord messages from Python
Discord has more than one way for an app to send messages to a server, but the simplest of these is a Webhook. A Webhook is essentially a URL that should receive a request over the internet when something has happened. In this example your program will send a request to this URL when it runs and sending that request will cause a message to appear in Discord.
Webhooks are perfect for when you just want to send updates or notifications to a Discord channel. It’s not going to work if you want your program to react to what people are saying in the channel. In that case you’d need to write a full bot or use slash-commands.
Creating a Webhook in Discord
You won’t be able to create a Webhook for any Discord server – it needs to be one where you have permission to add Webhooks. If you are an admin you’ll have permission, if not you’d need to ask your server admin to either grant you permission or create the Webhook for you. You’ll find this in the Discord permissions settings as “Manage Webhooks”.
Find the channel that you want to post to and You’ll be able to tell if you have permission when you go to the channel settings for the channel you want to post messages to.

Creating a Webhook will show you this page, where you can choose the name and display picture for the bot that will post your Webhook’s messages in the Discord channel.

Once you are happy with the name and picture click “Copy Webhook URL” which will copy a link to your clipboard. When you paste this URL, you’ll notice that it’s really long, it will look something like this:
https://discord.com/api/webhooks/1234567890123456012/xxxXXXxxxXXXxxxXXXxxxXXXxxxXXXxxxXXXxxxXXXxxxXXXxxxXXXxxxXXXxxxXXXxx
It’s important that you don’t share this URL with other people, this URL contains the keys for this Webhook, anyone who has this URL can send messages to your Discord channel under the bot’s name.
Using the Webhook URL safely
We want to avoid storing secrets like a Webhook URL in our code, that way we can share our code with other people and they can use that code with their own Webhook URL.
In SplootCode we can use the configuration settings to create a secret environment variable to store our Webhook URL so that it’s safe to share our code.

In our code we can access the Webhook URL like this:

Sending a message
To send a message to Discord we need to send a web request to this Webhook URL, and to do that we’re going to use the Python requests
library.

Discord expects the request to include a JSON object as the payload, but we can use a Python Dictionary and the requests library will translate that into JSON for us.

Discord Webhooks only accepts POST requests, so we need to use the post
function from the requests
library to send our message.


And that’s it! When we run this program we can see our message show up in Discord from the Spidey Bot:
The Complete Example
If you don’t want to type out all this code, use this example code. You will need to provide your own Webhook URL and add it to the DISCORD_WEBHOOK_URL
environment variable.

And if you’re working text-based Python instead of SplootCode, here’s the equivalent code, once again using an environment variable to store the Webhook URL.
import os
import requests
DISCORD_WEBHOOK_URL = os.environ['DISCORD_WEBHOOK_URL']
payload = {'content': 'Hello from Python!'}
response = requests.post(DISCORD_WEBHOOK_URL, json=payload)
Troubleshooting
If the message isn’t showing up in Discord like you expect, check the Status Code of the response like this:

In this example, the status code is 400
which indicates that there was something wrong with the request we sent and Discord didn’t understand something about it. In this example, I’d used "Content"
with a capital C instead of a lowercase C in the payload. Discord couldn’t find the content of the message so it thought it was empty.
If you see a Status Code of 401
, that means you’re not authorised to make that request – double check that you have copy-pasted the URL exactly correctly and that the Webhook hasn’t been deleted in the Discord settings.

Want to learn more about SplootCode? Here’s a link to our homepage