When doing long processing with Python
** "I'm free until the end, but I don't want to waste time because I want to start working as soon as I'm done!" ** ** "I want to respond immediately if an error occurs!" "But I'm tired of sticking to my computer all the time." **
I think you there! If you read this article, you can get rid of such wasted time for the rest of your life.
With the API provided by LINE, you will be able to automatically send messages to LINE from python code without special authentication. (This is different from LINE Bot)
Copy the code of the article (10 seconds)-> Token acquisition (30 seconds)-> Finally, make some modifications ... ** Done! ** **
Now copy and paste this code into main.py and at the end of your favorite file.
Copy and paste code.py
def notify(message):
import requests
url = "https://notify-api.line.me/api/notify"
token = "" #Enter the token you want to get later here
headers = {"Authorization": "Bearer " + token}
message = message
payload = {"message": message}
requests.post(url, headers=headers, params=payload)
Get a token. Open LINE Notify's My Page. Link here Please log in with your own LINE account. Press ** "Issue Token" **. Here, select ** "Receive notifications from LINE Notify 1: 1" **. (Details will be described later) Please fill in the ** token name **, anything is fine. (Here, it is ** test **) Then, a token will be issued. Although it is hidden here, a character string such as ** "g7YIhv7W ..." ** is issued. Make a note of this ** always ** somewhere. If you forget your note, please issue a new token again. (Not so big) This completes the token acquisition.
Now, enter the obtained token into the copy code.
Copy and paste code.py
def notify(message):
import requests
url = "https://notify-api.line.me/api/notify"
token = "g7YIhv7W..." #Please enter the token you got here
headers = {"Authorization": "Bearer " + token}
message = message
payload = {"message": message}
requests.post(url, headers=headers, params=payload)
Finally, add just one line to the file where you first pasted the ** copy and paste code **. Insert the code below wherever you like.
Finish.py
message = "All processing is finished" #Please enter your favorite message here
notify(message)
That's it. When ** notify (message) ** is called, the message will be delivered to LINE on your smartphone or computer.
When performing a long process.
Usage example 1.py
#Long processing...
message = "The process is finished!"
notify(message)
def notify(message):
import requests
url = "https://notify-api.line.me/api/notify"
token = "g7YIhv7W..." #Please enter the token you got here
headers = {"Authorization": "Bearer " + token}
message = message
payload = {"message": message}
requests.post(url, headers=headers, params=payload)
When an error occurs.
Usage example 2.py
while True:
try:
... #Main processing
except Exception as e:
message = e
notify(message)
def notify(message):
import requests
url = "https://notify-api.line.me/api/notify"
token = "g7YIhv7W..." #Please enter the token you got here
headers = {"Authorization": "Bearer " + token}
message = message
payload = {"message": message}
requests.post(url, headers=headers, params=payload)
Of course, it's OK because you can use this copy code as it is, but you can do various things depending on how you devise it. It may be interesting to let us know not only that the process has been completed or that an error has occurred, but also the end time, how long it took, and what kind of error occurred. Also, this time, when issuing the token, ** "Receive notifications from LINE Notify 1: 1" ** was selected, but you can also select other groups. Please try various things and make your own original template.
Recommended Posts