Skip to content

Instantly share code, notes, and snippets.

@dehio3
Last active March 15, 2019 09:13
Show Gist options
  • Save dehio3/6a9a73f0cd89a2d880236f8653f0cd5a to your computer and use it in GitHub Desktop.
Save dehio3/6a9a73f0cd89a2d880236f8653f0cd5a to your computer and use it in GitHub Desktop.
slackへ画像をアップロード
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import logging
import requests
# logging
formatter = '%(asctime)s %(levelname)s %(name)s %(message)s'
logging.basicConfig(level=logging.INFO, format=formatter)
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
def slack_send_image(filename, token, channel_id, comment, title):
""" slackへファイルをアップロード
args :
filename : アップロードしたい画像のパス
token : files.upload APP のtoken
channel_id : 画像をアップロードするチャンネルID
comment : slack通知時のコメント
title : 画像の名前
"""
logger.info('slack send image')
files = {'file': open(filename, 'rb')}
param = {
'token': token,
'channels': channel_id,
'filename': "filename",
'initial_comment': comment,
'title': title
}
r = requests.post(url="https://slack.com/api/files.upload",
params=param, files=files)
respons_json = r.json()
if respons_json["ok"] != True:
logger.error(r.json())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment