I made it because I wanted to visualize the data using matplotlib and then send the results to Typetalk on a regular basis. This is a method to send the locally created image as it is without saving it.
import requests
import json
import matplotlib.pyplot as plt
import io
image_buffer = io.BytesIO()
plt.plot([1,2,3]) #Draw a line graph
plt.savefig(image_buffer, format='jpeg') #Export here
token = '<Get the Typetalk bot token and assign it>'
topic_id = '<Substitute the topic ID posted by Typetalk>'
#URL for sending a message
message_url = f"https://typetalk.com/api/v1/topics/{topic_id}?typetalkToken={token}"
#URL for file upload
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)
#Confirmation of execution
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': 'Today's result!', 'fileKeys[0]': fileKey})
#Confirmation of execution
print(message_result.status_code)
print(message_result.content)
Now you can monitor your data on a regular basis!
Recommended Posts