Notify error and execution completion by LINE [Python]

Introduction

It is not uncommon for one program to take several days to execute when doing machine learning. I am worried about the execution status of the program and open the terminal every few hours. Do you not spend such days? For such a person, this time I will introduce a method to notify the error and execution completion of the Python program by LINE. If you have a LINE account, you can do it in about 10 minutes, so be sure to check it out!

Preparing for LINE Notify

We use a service called LINE Notify provided by LINE to send notifications.

First, issue a token from here. https://notify-bot.line.me/my/

After logging in to your LINE account from the login button on the upper right, issue and copy the token according to the following procedure.

手順1.png

手順2.png

Set the token name as you like. This time, it is called "execution result notification". 手順3.png

Be sure to copy it here. 手順4.png

This completes the preparation for LINE Notify.

Python program for LINE notifications

All you have to do is copy and paste the following program and change the token part. (Maybe you need pip install requests)

line_notify.py


import requests

#Function to notify LINE
def line_notify(message):
    line_notify_token = 'Paste the token here'
    line_notify_api = 'https://notify-api.line.me/api/notify'
    payload = {'message': message}
    headers = {'Authorization': 'Bearer ' + line_notify_token} 
    requests.post(line_notify_api, data=payload, headers=headers)

if __name__ == '__main__':
    message = "Hello world!"
    line_notify(message)

When you run python line_notify.py, you should get a message" Hello world! "On LINE.

Notify error or execution completion

After that, just notify it in combination with exception handling. Try running the following program.

hoge.py


import requests

#Function to notify LINE
def line_notify(message):
    line_notify_token = 'Paste the token here'
    line_notify_api = 'https://notify-api.line.me/api/notify'
    payload = {'message': message}
    headers = {'Authorization': 'Bearer ' + line_notify_token} 
    requests.post(line_notify_api, data=payload, headers=headers)

# a/Function to calculate b
def foo(a, b):
    return a / b

if __name__ == '__main__':
    try:
        ans = foo(1, 0)
    except Exception as e:
        line_notify(e)
    else:
        line_notify("finished")

実行結果1.jpeg Change foo (1, 0) to foo (1, 1) and execute it. 実行結果2.jpeg You have been notified correctly.

reference

Recommended Posts

Notify error and execution completion by LINE [Python]
pytube execution and error
Try LINE Notify in Python
Strange and horrifying Python error story
Implemented in 1 minute! LINE Notify in Python
Read an Excel sheet and loop it line by line Python VBA
Socket communication and multi-thread processing by Python
Line graphs and scale lines in python
Read the file line by line in Python
Java compilation and execution understood by CLI
Read the file line by line in Python
Socket communication by C language and Python
Get iPad maintenance by scraping and notify Slack
Send a message to LINE with Python (LINE Notify)
Read line by line from a file with Python
Build PyPy and Python execution environment with Docker
Command execution triggered by file update (python edition)
Split Python images and arrange them side by side
Python> Sort by number and sort by alphabet> Use sorted ()
How to execute a schedule by specifying the Python time zone and execution frequency
Python Error Handling
LINE heroku python
Let Python notify
Java compilation and execution understood by CLI
Notify error and execution completion by LINE [Python]
Command execution triggered by file update (python edition)
pytube execution and error
Visualization method of data by explanatory variable and objective variable
[Road to Python intermediate] Dynamically specify execution method by variable name
python2 series / 3 series, character code and print statement / command line
Python text reading for multiple lines and one line
[Python3] Connection to Oracle Database and SQL execution [cx_Oracle]
[Python] Type Error: Summary of error causes and remedies for'None Type'
I made a LINE BOT with Python and Heroku
The VIF calculated by Python and the VIF calculated by Excel are different .. ??
[Python] Chapter 01-02 About Python (Execution and installation of development environment)
It is blocked by Proxy, a connection error occurs in Python or pip, and it is retried.
Implemented pix2pix in Windows environment (with kind command line execution results and common error examples)
Scraping with Python + Selenium to add Apple refurbished products to your cart and notify on line