POST messages from python to Slack via incoming webhook

Posting by Slack + WebHook

In Slack, you can easily post a message from a Python script by getting the URL for WebHook.

In Slack, there are message used for relatively simple messages and ʻattachments` that can represent complex message formats. I will explain how to post to slack in both of the above formats using Python.

Incoming WebHook settings

Publish the posting URL on Slack's Incoming WebHook Settings Page.

Select a channel and click the ʻAdd Incoming WebHooks Integration` button. Screenshot 2015-06-22 18.47.17.png

Make a copy of the displayed webhook URL Screenshot 2015-06-22 18.48.30.png

(Optional) You can change the default post settings if you want.

item Description
Customize Name Default user name at the time of posting
Customize Icon Default icon when posting

If you make any corrections, save them with Save Settings.

Screenshot 2015-06-22 18.52.30.png

Post from Python to Slack

Install slack web

Install slack web that allows you to easily post with WebHook via pip.

sudo pip install slackweb

Send a simple message

You can send a message as follows by using slackweb. The default fields are used for the fields omitted here.

.python


> import slackweb
> slack = slackweb.Slack(url="<Copyed URL>")
> slack.notify(text="This is a test.")

スクリーンショット 2015-06-22 19.31.17.png

You can modify the room, icon, username, or use simple markdown notation by modifying the argument of the notify method. (For more information, see Message Formatting.)

.python


> slack.notify(text="This is a *test*.", channel="#coffee", username="coffee-bot", icon_emoji=":coffee", mrkdwn=True)

スクリーンショット 2015-06-22 19.33.46.png

Send complex messages

You can also send complex messages by using the format ʻattachments`.

.python


> attachments = []
> attachment = {"title": "Sushi",
                "pretext": "Sushi _includes_ gunkanmaki",
                "text": "Eating *right now!*",
                "mrkdwn_in": ["text", "pretext"]}
> attachments.append(attachment)
> slack.notify(attachments=attachments)

スクリーンショット 2015-06-22 19.38.57.png

For more information, see Attachments (https://api.slack.com/docs/attachments).

Referenced site

slack - incoming-webhooks

Recommended Posts

POST messages from python to Slack via incoming webhook
Post from Python to Slack
Post to vim → Python → Slack
Post to Slack via Subscriber
Post to slack with Python 3
Post to Slack in Python
Post from python to facebook timeline
[Lambda] [Python] Post to Twitter from Lambda!
Post images from Python to Tumblr
Send a message from Python to Slack
Post a message from IBM Cloud Functions to Slack in Python
Send a message from Slack to a Python server
Image characters and post to slack (python slackbot)
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Send data from Python to Processing via socket communication
Cheating from PHP to Python
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
Post to Twitter using Python
Switch from python2.7 to python3.6 (centos7)
Connect to sqlite from python
Call Matlab from Python to optimize
Post Jenkins console output to Slack
Create folders from '01' to '12' with python
Connect to utf8mb4 database from python
Post to slack in Go language
Python (from first time to execution)
How to access wikipedia from python
Python to switch from another language
Easily post to twitter with Python 3
[Nanonets] How to post Memo [Python]
Did not change from Python 2 to 3
Update Python on Mac from 2 to 3
[Python] Fluid simulation: From linear to non-linear
From Python to using MeCab (and CaboCha)
How to update Google Sheets from Python
Private Python handbook (updated from time to time)
I want to use jar from python
Convert from katakana to vowel kana [python]
Push notification from Python server to Android
Log in to Slack using requests in Python
Connecting from python to MySQL on CentOS 6.4
Porting and modifying doublet-solver from python2 to python3.
Throw Incoming Webhooks to Mattermost in Python
How to access RDS from Lambda (python)
Python> Output numbers from 1 to 100, 501 to 600> For csv
L-Chika TWE Lite from Python via ToCoStick
Convert from Markdown to HTML in Python
[Amazon Linux] Switching from Python 2 series to Python 3 series
API explanation to touch mastodon from python
Connect to coincheck's Websocket API from Python
Create a setting in terraform to send a message from AWS Lambda Python3.8 to Slack
Edit Excel from Python to create a PivotTable
Try sending Metrics to datadog via python, DogStatsD
How to open a web browser from python
Study from Python Hour7: How to use classes
[Python] Convert from DICOM to PNG or CSV
Import Excel file from Python (register to DB)
I want to email from Gmail using Python.
[Python] I want to manage 7DaysToDie from Discord! 1/3
From file to graph drawing in Python. Elementary elementary