Skip to content

Instantly share code, notes, and snippets.

@codeperfectplus
Last active April 23, 2021 13:10
Show Gist options
  • Save codeperfectplus/ef31c46c8362849e0516fb10e52b9df2 to your computer and use it in GitHub Desktop.
Save codeperfectplus/ef31c46c8362849e0516fb10e52b9df2 to your computer and use it in GitHub Desktop.
Send Discord message using webhook and asynchronous api
# Send discord message using webhook and asynchronous api with aiohttp and asyncio
# Asynchronous calls do not block (or wait) for the API call to return from the server.
# Execution continues on in your program, and when the call returns from the server, a “callback” function is executed.
```python
import aiohttp
import asyncio
url = "<your url>" #webhook url, from here: https://i.imgur.com/f9XnAew.png
data = {
'content': "message",
"username": "PyContributors"
}
data["embeds"] = [
{
"title": "title",
"description": "description"
}
]
async def postData(url):
async with aiohttp.ClientSession() as session:
async with session.post(url, json=data) as response:
return await response.json(content_type=None)
loop = asyncio.get_event_loop()
loop.run_until_complete(postData(webhook_url))
````
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment