Bashing the Slack Python

5년 전

Hi Steemians,

The following is a guide for setting up Python, Slack Webhooks and the Terminal (Bash) to work together. The point of this post is to create a simple introduction with these 3 working pieces that will hopefully allow you to adapt it to whatever your needs are.

We are going to look at python and Bash separately, then combine all 3 elements in the final example.

Slack Incoming Webhook

First you need to setup an incoming webhook.
To do that go to: YOURDOMAIN.slack.com/apps/build

Select "Something just for my team"

Under 'Build a Custom integration', select Incoming Webhooks

Select a channel that you want to post your messages to.

Scroll down to Web hook URL and click 'Copy URL'

Additionally there is a guide in the Slack API documentation here

Bash Example

Open up a terminal.
We can use curl to post the data to our slack webhook. Use the below command and replace the URL with the one that you just setup.

curl -X POST -H 'Content-type: application/json' \
--data '{"text":"Hello World.\nSteem Example."}' \
 https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX

You should see the post in your nominated slack channel

Python Example

For python the process is similar

first we need to import json and the requests library

import json
import requests


SLACK_INCOMING_WEB_HOOK = "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX" #self explanatory
SLACK_INCOMING_USER = "steem_bot" #Slack Bot display name
SLACK_INCOMING_CHANNEL = "#yourchannel" #Slack Channel

def slack_test():

    payload = {
        "text": "Python Steem example", #message you want to send
        "username": SLACK_INCOMING_USER, #username set above
        "channel": SLACK_INCOMING_CHANNEL #Channel set above
    }


    req = requests.post(SLACK_INCOMING_WEB_HOOK, json.dumps(payload), headers={'content-type': 'application/json'}) #request to post the message

Python + Bash + Slack

The Python language is generally not praised for it's incredible processing speed. I've found that when relying on scheduled scripts or just manually running something, it can be nice to be able to walk away / close a session etc. But i still want to see the eventual output of the script, or exit status (Did it complete, return a value, fail etc).

This example will use python to run a terminal command python --version and then return the result - it will then post that result to slack. The idea being that you can post results from python, from the terminal, or a mix of both.

import os
import json
import requests
import subprocess


example = subprocess.check_output(["python", "--version"], stderr=subprocess.STDOUT)

SLACK_INCOMING_WEB_HOOK = "https://hooks.slack.com/services/XXXXXX/XXXXXX/XXXXXXXXXXXXXXX"
SLACK_INCOMING_USER = "script_bot"  # Slack Bot display name
SLACK_INCOMING_CHANNEL = "#scripts"  # Slack Channel

payload = {"text": example, "username": SLACK_INCOMING_USER, "channel": SLACK_INCOMING_CHANNEL}

requests.post(SLACK_INCOMING_WEB_HOOK, json.dumps(payload), headers={'content-type': 'application/json'})

Hope someone finds a use for this. Any adjustments/corrections etc. Just let me know in the comments. This is the first time I've written out a tutorial, so there may be a few errors.

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!
STEEMKR.COM IS SPONSORED BY
ADVERTISEMENT
Sort Order:  trending

☆☆☆☆☆😎