Skip to content Skip to sidebar Skip to footer

Discord - Send Message Only From Python App To Discord Channel (one Way Communication)

I am designing an app where I can send notification to my discord channel when something happen with my python code (e.g new user signup on my website). It will be a one way commun

Solution 1:

You can send the message to a Discord webhook.

First, make a webhook in the Discord channel you'd like to send messages to.

Then, use the discord.Webhook.from_url method to fetch a Webhook object from the URL Discord gave you.

Finally, use the discord.Webhook.send method to send a message using the webhook.

If you're using version 2 of discord.py, you can use this snippet:

from discord import SyncWebhook

webhook = SyncWebhook.from_url("url-here")
webhook.send("Hello World")

Otherwise, you can make use of the requests module:

import requests
from discord import Webhook, RequestsWebhookAdapter

webhook = Webhook.from_url("url-here", adapter=RequestsWebhookAdapter())
webhook.send("Hello World")

Solution 2:

I have found it. "Webhook" is the answer. Instead of using discord.py, just create a webhook for your channle and then just post the data to that endpoint.

import requests

#Webhook of my channel. Click on edit channel --> Webhooks --> Creates webhook
mUrl = "https://discord.com/api/webhooks/729017161942******/-CC0BNUXXyrSLF1UxjHMwuHA141wG-FjyOSDq2Lgt*******************"

data = {"content": 'abc'}
response = requests.post(mUrl, json=data)

print(response.status_code)

print(response.content)

Solution 3:

This might be one of the best approaches as it saves the addition of more python packages(one mentioned by @john), but I believe there is a more robust and easy solution for this scenario, as you can add images, make tables and make those notifications look more expressive.

A python library designed for the explicit purpose of sending a message to the discord server. A simple example from the PyPI page would be:

from discord_webhook import DiscordWebhook

webhook = DiscordWebhook(url='your webhook url', content='Webhook Message')
response = webhook.execute()

more examples follow on the page.

This is how the sent notification/message would look like

Discord notification with table


Post a Comment for "Discord - Send Message Only From Python App To Discord Channel (one Way Communication)"