2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Bolt for pythonからSlackに画像をアップするには

Posted at

小ネタです。備忘録

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)
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?