paint-brush
Sending Events and Logs to Discord via Python by@sh4yy
1,227 reads
1,227 reads

Sending Events and Logs to Discord via Python

by ShayanJanuary 11th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Music streaming services like Spotify and YouTube could benefit from introducing a token in order to change the way value is allocated in their marketplaces. Artists will be able to use the “SPOTUBE’s” token to create a new market for new content. The token will be available in limited amounts to pay for new releases and artists will have to use it to pay artists' fees. The idea is that the token will only be available to people with limited access to the new content, not to people who already have access to it.

Company Mentioned

Mention Thumbnail
featured image - Sending Events and Logs to Discord via Python
Shayan HackerNoon profile picture
Throughout my software development career, there were many times when I wanted to get instant updates and alerts when something happened within my project.


Take this, for example; you are developing a SaaS, and there are multiple valuable events that you would like to be aware of as soon as they happen. For example, users joining your waitlist or newsletter, user sign-ups, product sales, and user conversions. Having to run a long training or crawling task on a remote machine was another situation where I wanted instant updates on the progress and if something had gone wrong. I mean, you can do periodic checks on the machine to see how things are going, but I’d rather entirely forget about it and have it send me updates instead. The last example is when I wanted to automate my garage door via raspberry pi, and I wanted to know when the garage door was opening, closing, or had been left open for too long.


So why am I telling you these examples? I want you to start thinking about similar situations that you may also need a way to send yourself instant updates and to have a history of all these events in a single place.


Discord makes it relatively easy to solve this problem! We can create a Discord server specifically, create separate channels for our projects and use the webhook URL to push our events!


To get started, we need that Discord server. Log in to Discord, click on the Add Server button and proceed to create your own. Once that’s done, you should be able to see and open your Discord server


To make it easier to organize my projects and events, I usually create a new text channel for each project or, in some cases, one per type of event. To do so, click the + button next to text channels and create one. I’m going to call mine garage-door and make it a private channel.


Then, click on the settings icon for that channel, open the integration tab and create a new Webhook. Once added, you can see the Copy Webhook URL, and that’s precisely what we need to push our events! So copy that and paste it somewhere safe on your machine. Now, we are pretty much done with Discord, and we can move to writing some code! I have provided examples for Python and JavaScript; feel free to skip to whichever you find more relevant.


Sending events via Python

First, I would like to pass in my webhook URL as an environment variable, so I will set it to WEBHOOK_URL and use the standard library in python to access the value. You can, of course, skip this entire process and add your URL directly to the code.


export WEBHOOK_URL=//discord.com/api/...


Once we have set the environment variable, we can access it by importing os and using the os.environ.get method.


import os

WEBHOOK_URL = os.environ.get('WEBHOOK_URL')
print(WEBHOOK_URL)


I will be using the  package to handle my HTTP requests. You can install via PyPi by running the following command.


pip install requests


Finally, all we need to do is to import requests and make a POST request to the webhook url and pass our event in the JSON body with the content key.


import requests

requests.post(WEBHOOK_URL, { "content": "🦄 garage door is open" })


Once we run this code, we should get a new message in the garage-door channel telling us that our garage door has been opened.


As you can see, getting this set up is very easy while being quite powerful! I use this setup in almost all of my projects to be aware of how they are doing!

Also Published

바카라사이트 바카라사이트 온라인바카라