1
0

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.

Pythonで作成したデータの可視化画像をTypetalkに送る方法

Last updated at Posted at 2020-05-20

目的

データをmatplotlibを使って可視化した後、その結果を定期的にTypetalkに送りたいと思ったので作りました。
ローカルに作成した画像を保存せずにそのまま送信する方法になります。

必要なライブラリ

作り方

import requests
import json
import matplotlib.pyplot as plt
import io

image_buffer = io.BytesIO()
plt.plot([1,2,3]) # 折れ線グラフを書く
plt.savefig(image_buffer, format='jpeg') # ここで書き出し

token = '<Typetalkのbotのトークンを取得して代入してください>'
topic_id = '<Typetalkの投稿するトピックIDを代入してください>'

# メッセージ送信用のURL
message_url = f"https://typetalk.com/api/v1/topics/{topic_id}?typetalkToken={token}"

# ファイルアップロード用のURL
upload_file_url = f'https://typetalk.com/api/v1/topics/{topic_id}/attachments?typetalkToken={token}'

files = {'file': ('line.jpeg', image_buffer.getvalue(), 'image/jpeg')}

upload_file_result = requests.post(upload_file_url, files=files)

# 実行の確認
print(upload_file_result.status_code)
print(upload_file_result.content)

fileKey = json.loads(upload_file_result.content)['fileKey']

message_result = requests.post(url_message, {'message': '今日の結果!', 'fileKeys[0]': fileKey})

# 実行の確認
print(message_result.status_code)
print(message_result.content)

実行結果

スクリーンショット 2020-05-20 20.29.44.png

これで定期的なデータのモニタリングができる!

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?