Implemented in 1 minute! LINE Notify in Python

Introduction

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.

What is LINE Notify?

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)

Implementation flow

Copy the code of the article (10 seconds)-> Token acquisition (30 seconds)-> Finally, make some modifications ... ** Done! ** **

Implementation

Copy

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 token

(There are many images from here, you can let it flow more and more)

Get a token. Open LINE Notify's My Page. Link here Please log in with your own LINE account. qiita素材1.png Press ** "Issue Token" **. qiita素材2.png Here, select ** "Receive notifications from LINE Notify 1: 1" **. (Details will be described later) qiita素材3.png Please fill in the ** token name **, anything is fine. (Here, it is ** test **) qiita素材4.png 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) qiita素材5.png This completes the token acquisition.

Finish

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.


Usage example

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)

Finally

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

Implemented in 1 minute! LINE Notify in Python
Try LINE Notify in Python
Implemented SimRank in Python
Implemented Shiritori in Python
Fizzbuzz in Python (in one line)
Sudoku solver implemented in Python 3
6 Ball puzzle implemented in python
Make python segfault in one line
Implemented image segmentation in python (Union-Find)
Widrow-Hoff learning rules implemented in Python
Implemented label propagation method in Python
Implemented Perceptron learning rules in Python
I tried Line notification in Python
[Introduction] Insert line breaks in Python 3
CGI server (1) python edition in one line
A simple HTTP client implemented in Python
Line graphs and scale lines in python
Implemented in Python PRML Chapter 7 Nonlinear SVM
Read the file line by line in Python
Read the file line by line in Python
I implemented Cousera's logistic regression in Python
Implemented in Python PRML Chapter 5 Neural Networks
Implemented Stooge sort in Python3 (Bubble sort & Quicksort)
[Python] Invert bool value in one line
Implemented in Python PRML Chapter 1 Bayesian Inference
[Python] Read the specified line in the file
Mastering OpenWRT (LEDE): rtorrent
Mastering OpenWRT (LEDE): Until sharing samba as NAS
Try LINE Notify in Python
Implemented in 1 minute! LINE Notify in Python
Quadtree in Python --2
Python in optimization
CURL in python
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
LINE heroku python
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Let Python notify
Quad-tree in Python