Discord - Send message only from python app to discord channel (one way communication)
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
john
Updated on August 12, 2022Comments
-
john almost 2 years
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 communication as only python app will send message to discord channel.
Here is what I have tried.
import os import discord import asyncio TOKEN = "" GUILD = "" def sendMessage(message): client = discord.Client() @client.event async def on_ready(): channel = client.get_channel(706554288985473048) await channel.send(message) print("done") return "" client.run(TOKEN) print("can you see me?") if __name__ == '__main__': sendMessage("abc") sendMessage("def")
The issue is only first message is being sent (i-e abc) and then aysn function is blocking the second call (def).
I don't need to listen to discord events and I don't need to keep the network communication open. Is there any way where I can just post the text (post method of api like we use normally) to discord server without listening to events?
Thanks.
-
Frank almost 3 yearsThis is awesome and is a prefect answer to the question, just bare in mind that webhooks can only post to CHANNELS, not direct messages to users or so on.
-
Tendekai Muchenje about 2 yearshow would I do it for Direct messages?
-
Ben Soyka almost 2 years@TendekaiMuchenje Webhooks won't work in direct messages. You'd need to make a bot and send the messages programmatically via the bot.