Closed3

pythonで数字をグラフ化してSlackに画像として投げ込みたい

ara_ta3ara_ta3

files.api使うほうが無難に行けそうだった

import requests
from io import BytesIO
import matplotlib.pyplot as plt

def upload_file_to_slack(token, channels, file_buffer, filename):
    headers = {
        "Authorization": f"Bearer {token}"
    }
    payload = {
        "channels": channels,
        "filename": filename
    }
    files = {
        "file": file_buffer
    }
    response = requests.post(
        "https://slack.com/api/files.upload",
        headers=headers,
        data=payload,
        files=files
    )
    return response

def main():
    plt.plot([1, 2, 3, 4])
    plt.ylabel('some numbers')

    buf = BytesIO()
    plt.savefig(buf, format='png')
    buf.seek(0)

    token = 'OAuth Token'
    channels = 'channel id'

    # 画像をSlackにアップロード
    response = upload_file_to_slack(token, channels, buf, "graph.png")
    print(response.text)

if __name__ == "__main__":
    main()

reqiurements.txt

matplotlib
requests
このスクラップは2023/12/17にクローズされました