How to notify a Discord channel in Python

Introduction

Hello, this is tmasuyama of cloud engineers.

Nowadays, online drinking parties are popular, but there are various tools. I feel that Discord is quite popular around me.

Of course, unlike Zoom, there is no time limit for 3 or more people. It's good that there is little delay and that some noise is automatically eliminated.

Although it is such Discord, you can skip notifications with Bot like Slack and LINE. You can use it like curl, so let's introduce how to use it.

Advance preparation

Get the WebHookURL that corresponds to the API key. After creating the channel, jump to the setting screen from the gear mark. image.png

When you open it, there is an item called ** Webhook **, so click it. image.png

Select ** Create Webhook **. image.png

The name field will be the bot name when notified by Discord. Also, make a copy of the ** Webhook URL ** as it will be used later in the Python file. image.png

This completes the preparations.

Creating a python file

No special preparation is required on the Python side, and the requests module is used. Here, let's notify you of the classic ** Hello, World! **.

discord_bot.py


# coding: utf-8
import requests

discord_webhook_url = 'Webhook URL you copied earlier'

data = {"content": "Hello, World!"}
requests.post(discord_webhook_url, data=data)

When you run it, you will be notified on the Discord channel in the following form. image.png

You could skip the notification with just this code. Thank you for your hard work.

Recommended Posts

How to notify a Discord channel in Python
How to get a stacktrace in python
How to clear tuples in a list (Python)
How to embed a variable in a python string
How to implement Discord Slash Command in Python
How to create a JSON file in Python
[Python] How to draw a histogram in Matplotlib
How to develop in Python
How to convert / restore a string with [] in python
[Python] How to expand variables in a character string
How to execute a command using subprocess in Python
How to write a Python class
[Python] How to do PCA in Python
How to collect images in Python
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to handle Japanese in Python
How to slice a block multiple array from a multiple array in Python
A story about how to specify a relative path in python.
How to use the __call__ method in a Python class
How to import a file anywhere you like in Python
How to define multiple variables in a python for statement
I tried "How to get a method decorated in Python"
How to develop in a virtual environment of Python [Memo]
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
Discord in Python
[Introduction to Python] How to use class in Python?
How to access environment variables in Python
How to dynamically define variables in Python
[Python] How to make a class iterable
How to do R chartr () in Python
[Python] How to convert a 2D list to a 1D list
[Itertools.permutations] How to put permutations in Python
[Python] How to invert a character string
How to display multiplication table in python
How to extract polygon area in Python
How to check opencv version in python
How to switch python versions in cloud9
How to adjust image contrast in Python
How to use __slots__ in Python class
How to dynamically zero pad in Python
How to use regular expressions in Python
How to display Hello world in python
How to use is and == in Python
How to write Ruby to_s in Python
How to run a Maya Python script
How to determine the existence of a selenium element in Python
How to pass arguments to a Python script in SPSS Modeler Batch
How to make a string into an array or an array into a string in Python
How to check the memory size of a variable in Python
How to get a string from a command line argument in python
[For beginners] How to register a library created in Python in PyPI
How to delete multiple specified positions (indexes) in a Python list
How to generate a new loggroup in CloudWatch using python within Lambda
How to use the C library in Python
How to read a CSV file with Python 2/3
How to receive command line arguments in Python
Send a message to LINE with Python (LINE Notify)