小ネタです。備忘録
Slack Bolt for pythonを使って外部から取得した画像をSlackにアップロードするには。以下のようなやり方
fileupload.py
import requests
def get_and_upload():
filename = "test.gif"
url = "https://lne.st/test.gif" #取得したいファイルの存在するURLを入れる
urlData = requests.get(url).content
download_file(url, filename)
try:
result = client.files_upload(
channels=body['channel'],#channel_id を適宜入れること
file=filename,
)
logger.info(result)
uploadedurl = result['file']['permalink_public']
except SlackApiError as e:
print("Error uploading file: {}".format(e))
finally:
pass
import urllib.error
import urllib.request
def download_file(url, dst_path):
try:
with urllib.request.urlopen(url) as web_file, open(dst_path, 'wb') as local_file:
local_file.write(web_file.read())
except urllib.error.URLError as e:
print(e)