• Skip to main content
  • Skip to header right navigation
  • Skip to site footer
Maker Hacks

Maker Hacks

Ideas, news & tutorials for makers and hackers – Arduino/Raspberry Pi, 3D printing, robotics, laser cutting, and more

  • Home
  • About
  • YouTube
  • Recommendations
  • Contact

Linux Automation Tip: Send Messages to Your Discord Server Using Python

You are here: Home / Hacks, Tips, and Tutorials / Linux Automation Tip: Send Messages to Your Discord Server Using Python
FacebookTweetPin
Author: Chris Garrett

ping discord with python

I am on a mission to declutter my email. Once you get past the unsubscribe/filter stage, the next thing is to get the important information in a different way. Once I started down this path, however, I discovered this approach allows me to have fewer tabs open, and even concentrate more.

What is this magic? Ping your own Discord server … wait! Let me explain!

Before we start, the tool I am using to unsubscribe from the cruft that clogs my inbox is called Unroll.me – check it out.

Now, Discord.

Discord is a live chat system – but not JUST that

Most people know Discord from gaming. It’s a chat system like Slack or Hipchat, but more informal and less business focused. The best part is the free service is full of neat features.

While joining chats might seem counter to the goal of productivity and calm, you don’t have to chat with anyone.

Hence me using Discord instead of Slack. My work is full of Slack conversations and sometimes I need to close Slack and get some writing or code produced (often on a deadline). I can’t use Slack to keep me informed if that is going to draw me into conversations.

My Discord server, however, can be live without anyone knowing, and can work across all my devices.

Sign up here to get started.

Pinging your server with Python

There is a fully featured API, but the easy start is with Webhooks – simple URLs that you pass your message to. When you ping these URLs your payload gets passed to your Discord channel.

Be aware these Webhooks can send messages from anyone if they get into the wrong hands, treat it a bit like a password.

Grab your webhook using this guide.

Code

# system library for getting the command line argument
import sys
# web library
import http.client
 
def send( message ):
    # your webhook URL
    webhookurl = "https://discordapp.com/api/webhooks/YOURWEBHOOK"
    # compile the form data (BOUNDARY can be anything)
    formdata = "------:::BOUNDARY:::\r\nContent-Disposition: form-data; name=\"content\"\r\n\r\n" + message + "\r\n------:::BOUNDARY:::--"
 
    # get the connection and make the request
    connection = http.client.HTTPSConnection("discordapp.com")
    connection.request("POST", webhookurl, formdata, {
        'content-type': "multipart/form-data; boundary=----:::BOUNDARY:::",
        'cache-control': "no-cache",
        })
 
    # get the response
    response = connection.getresponse()
    result = response.read()
 
    # return back to the calling function with the result
    return result.decode("utf-8")

# send the messsage and print the response
print( send( sys.argv[1] ) )

To test this example script out, simply call the .py with your message as the first parameter:

Sending an important message!


Now anything that might otherwise have gone to my inbox (eg. my dev machine IP address) can instead just go into the channel, and I can see my alerts in the usual way, or check in later with selective muting.

Related

Category: Hacks, Tips, and TutorialsTag: automation, discord, linux, programming, python, scripting
FacebookTweetPin

About Chris Garrett

Marketing Director by day, maker, retro gaming, tabletop war/roleplaying nerd by night. Co-author of the Problogger Book with Darren Rowse. Husband, Dad, 🇨🇦 Canadian.

Check out Retro Game Coders for retro gaming/computing.

☕️ Support Maker Hacks on Ko-Fi and get exclusive content and rewards!

Previous Post:Cryptocurrency Mining RigBuilding a Cryptocurrency Mining Rig is Easier than You Might Think
Next Post:Creality CR-10 MINI ReviewCreality CR-10 MINI

Sidebar

  • Facebook
  • Twitter
  • Instagram
  • YouTube

Recently Popular

  • Gweike Cloud Review
  • How to choose the right 3D printer for you
  • Glowforge Review – Glowforge Laser Engraver Impressions, Plus Glowforge Versus Leading Laser Cutters
  • Original Prusa i3 Mk3S Review
  • Best 3D Printing Facebook Groups
  • Elegoo Mars Review – Review of the Elegoo Mars MSLA Resin 3D Printer
  • Glowforge ‘Pass-Through’ Hack: Tricking the Front Flap of the Glowforge with Magnets to Increase Capacity
  • How to Make a DIY “Internet of Things” Thermometer with ESP8266/Arduino
  • Wanhao Duplicator i3 Review
  • IKEA 3D Printer Enclosure Hack for Wanhao Di3
  • Creality CR-10 3d printer review – Large format, quality output, at a low price!
  • 3D Printed Tardis with Arduino Lights and Sounds
  • Anet A8 Review – Budget ($200 or less!) 3D Printer Kit Review
  • Make your own PEI 3D printer bed and get every print to stick!
  • Upgrading the Wanhao Di3 from Good to Amazing
  • How to Install and Set Up Octopi / Octoprint
  • Creality CR-10 S5 Review

Copyright © 2023 · Maker Hacks · All Rights Reserved · Powered by Mai Theme