How to send a visualization image of data created in Python to Typetalk

Purpose

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.

Required library

How to make

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)

Execution result

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

Now you can monitor your data on a regular basis!

Recommended Posts

How to send a visualization image of data created in Python to Typetalk
How to develop in a virtual environment of Python [Memo]
How to get a list of built-in exceptions in python
How to get a stacktrace in python
How to adjust image contrast in Python
How to determine the existence of a selenium element in Python
How to check the memory size of a variable in Python
How to check the memory size of a dictionary in Python
[For beginners] How to register a library created in Python in PyPI
How to create a large amount of test data in MySQL? ??
How to clear tuples in a list (Python)
How to embed a variable in a python string
How to use Python Image Library in python3 series
How to create a JSON file in Python
[Python] How to put any number of standard inputs in a list
Real-time visualization of thermography AMG8833 data in Python
Summary of how to use MNIST in Python
How to notify a Discord channel in Python
[Python] How to draw a histogram in Matplotlib
How to format a list of dictionaries (or instances) well in Python
A well-prepared record of data analysis in Python
How to pass the execution result of a shell command in a list in Python
How to get a list of files in the same directory with python
[Introduction to Python] How to get the index of data with a for statement
How to convert / restore a string with [] in python
Summary of tools needed to analyze data in Python
How to get the number of digits in Python
Power BI visualization of Salesforce data entirely in Python
How to scrape image data from flickr with python
[Python] How to expand variables in a character string
How to write a list / dictionary type of Python3
How to create an image uploader in Bottle (Python)
[Python] Created a method to convert radix in 1 second
Display a histogram of image brightness values in python
How to execute a command using subprocess in Python
Publish / upload a library created in Python to PyPI
How to develop in Python
How to identify the element with the smallest number of characters in a Python list?
How to check in Python if one of the elements of a list is in another list
[Python] How to make a list of character strings character by character
How to slice a block multiple array from a multiple array in Python
How to shuffle a part of a Python list (at random.shuffle)
A story about how to specify a relative path in python.
How to use the __call__ method in a Python class
How to import a file anywhere you like in Python
How to generate exponential pulse time series data in python
How to define multiple variables in a python for statement
I tried "How to get a method decorated in Python"
Comparison of how to use higher-order functions in Python 2 and 3
How to get the last (last) value in a list in Python
How to get an overview of your data in Pandas
Data science companion in python, how to specify elements in pandas
How to get a quadratic array of squares in a spiral!
How to make a face image data set used in machine learning (2: Frame analysis of video to obtain candidate images)
How to change python version of Notebook in Watson Studio (or Cloud Pak for Data)
How to write a Python class
[Python] How to FFT mp3 data
[Python] How to do PCA in Python
How to collect images in Python
How to use SQLite in Python
How to create an instance of a particular class from dict using __new__ () in python