Send experiment results (text and images) to slack with Python

Send text

Set API on slack side

  1. Open "Manage apps" from "Settings and management" of slack. fig1.png
  2. Search and add the App directory Incoming Webhook. fig2.png fig3.png
  3. Select a channel and select "Add Incoming Webhook Integration" fig4.png
  4. Copy the "Webhook URL". fig5.png
  5. You can change the bot icon and name here. fig8.png

code

import slackweb
slack = slackweb.Slack(url="Copied webhook URL")

def notify(title, text, color):
    attachments = [{"title": title,
                "text": text,
                "color": color, #good, warning, danger
                "footer": "Send from Python",
                }]
    slack.notify(text=None, attachments=attachments)

notify("test","Good morning","good")
notify("test","Hello","warning")
notify("test","Good evening","danger")

For the syntax of attachments, refer to the following

Execution result

fig11.png

Send image

--Set API on slack side

  1. Add "Bots" in the same way as 1 and 2 above.
  2. Copy the API token.

code

import requests
import json

def notifyImg(title, imageURL):
        files = {'file': open(imageURL, 'rb')}
        param = {
        'token': "Copyed API token", 
        'channels':'The name of the channel you want to post',
        'filename':"filename",
        'title': title,
        }
        requests.post(url="https://slack.com/api/files.upload",params=param, files=files)

notifyImg("Test image", "test.png ") #imageURL is the file path

What is a file path

Execution result

fig10.png

At the end

In addition to notifying that the experiment was over, I thought it would be nice if you could send me the results as well, so I checked the API of slack, so I summarized it. I'm sorry if I make a mistake.

fig12.png

reference

-Post to slack with Python3 -Summary of how to post images from the program to Slack

Recommended Posts

Send experiment results (text and images) to slack with Python
Sample to send slack notification with python lambda
Post to slack with Python 3
[Python] Try to recognize characters from images with OpenCV and pyocr
English speech recognition with python [speech to text]
HTML email with image to send with python
Send a message from Python to Slack
Capturing images with Pupil, python and OpenCV
Fractal to make and play with Python
Add Gaussian noise to images with python2.7
Importing and exporting GeoTiff images with Python
Read text in images with python OCR
Upload images to Google Drive with Python
Send a message from Slack to a Python server
Scraping tabelog with python and outputting to CSV
Send a message to LINE with Python (LINE Notify)
MessagePack-Try to link Java and Python with RPC
Image characters and post to slack (python slackbot)
Convert PDFs to images in bulk with Python
Send an email to Spushi's address with python
Send messages to Skype and Chatwork in Python
How to put OpenCV in Raspberry Pi and easily collect images of face detection results with Python
[Python] Mention to multiple people with Slack API
Flow to complete Slack authentication with Flask (Python)
Try to make BOT by linking spreadsheet and Slack with python 2/2 (python + gspread + slackbot)
Try to make BOT by linking spreadsheet and Slack with python 1/2 (python + gspread + slackbot)
Send email with Python
Procedure to load MNIST with python and output to png
I want to handle optimization with python and cplex
Try to operate DB with Python and visualize with d3
Something to enjoy with Prim Pro (X-Play) and Python
Easy to use Nifty Cloud API with botocore and python
Programming with Python and Tkinter
I tried [scraping] fashion images and text sentences in Python.
Connect to BigQuery with Python
Encryption and decryption with Python
Send Japanese email with Python3
Bordering images with python Part 1
How to install NPI + send a message to line with python
Post to vim → Python → Slack
screen and split screen with python and ssh login to remote server
Python and hardware-Using RS232C with Python-
I tried various methods to send Japanese mail with Python
Connect to Wikipedia with Python
[Python] How to play with class variables with decorator and metaclass
Experiment to make a self-catering PDF for Kindle with Python
Convert images to sepia toning with PIL (Python Imaging Library)
Convert garbled scanned images to PDF with Pillow and PyPDF
Try to bring up a subwindow with PyQt5 and Python
How to do Bulk Update with PyMySQL and notes [Python]
[Let's play with Python] Image processing to monochrome and dots
Speech to speech in python [text to speech]
Switch python to 2.7 with alternatives
Write to csv with Python
Send and receive Flask images
python with pyenv and venv
Save images on the web to Drive with Python (Colab)
Convert video to black and white with ffmpeg + python + opencv
I tried to make GUI tic-tac-toe with Python and Tkinter
[Python x Zapier] Get alert information and notify with Slack
Get additional data to LDAP with python (Writer and Reader)