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

in automation •  7 years ago  (edited)

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:

Chris — chrisg@ubuntu-512mb-nyc3-01_ ~_steemit — ssh chrisg@165.227.186.76 — 158×47-4.png

#general - Discord-5.png

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.



Posted from my blog with SteemPress : https://makerhacks.com/python-messages-discord/

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

You got a 74.07% upvote from @ipromote courtesy of @makerhacks!
If you believe this post is spam or abuse, please report it to our Discord #abuse channel.

If you want to support our Curation Digest or our Spam & Abuse prevention efforts, please vote @themarkymark as witness.

Nothing makes me happier than an awesome Python tutorial! Keep it up!

Using requests library can make send() function much simpler.

You got a 1.66% upvote from @postpromoter courtesy of @makerhacks!

Want to promote your posts too? Check out the Steem Bot Tracker website for more info. If you would like to support the development of @postpromoter and the bot tracker please vote for @yabapmatt for witness!