Try LINE Notify in Python

LINE Notify is a notification function that allows you to receive messages from an external web service in the LINE talk room by linking it with an external web service. You can send the message to your LINE talk room by throwing (POST) the message to the specified LINE Notify URL, so you can easily write and use the program yourself. So let's try it in Python. (Although there is already an article with almost the same content, it is my memorandum ...)

1. Initial setting of LINE Notify

  1. Log in with your LINE ID from the page above
  2. Access My Page from your name in the upper right corner
  3. Select "Issue Token" from "Issue Access Token (for developers)" LINEnotify1.png
  4. On the "Issue Token" screen, --Enter the token name (as easy as possible because it will be displayed at the beginning of each message) —— “Receive notifications from LINE Notify 1: 1” will notify you only. You can also select a LINE group. If you choose, "issue".
    LINEnotify2.png
  5. Copy and paste the token
    LINEnotify3.png
  6. When you issue a token, a message will be sent to your LINE from LINE Notify.
    <img width = "375" alt = "LINEnotify4.png " src = "https://qiita-image-store.s3.ap" -northeast-1.amazonaws.com/0/476884/5d604a74-f045-0f35-7bf5-8275cdb7d726.png ">

2. Python script

Only this.

import requests
token = "XXXXXXXXXXXXXXXXXXXXXXXX"
url = "https://notify-api.line.me/api/notify"
headers = {"Authorization": "Bearer " + token}
payload = {"message": "It's a test message"}
requests.post(url, headers=headers, data=payload)

By sending a "message" to the specified URL, LINE will deliver the message to you based on your authentication information (access token). Replace the XXX list with the token you copied above.

When you run this script, you will get a message from LINE Notify as shown below (the token name you set will be displayed in []). LINEnotify5.png

Easy! !! !! </ strong>


Supplement

It is not good to write dangerous information in the code if it is leaked to others such as an access token, so it seems better to write it in the environment variable of the OS and read it in the script. (If the shell is bash) in ~ / .bash_profile export line_access_token="xxxxxxxxxxxxx" And set the access token to the environment variable, and the top two lines of the Python code above

import os, requests
token = os.environ["LINE_ACCESS_TOKEN"]

Change to.

Recommended Posts

Try LINE Notify in Python
Implemented in 1 minute! LINE Notify in Python
Try gRPC in Python
Try 9 slices in Python
Try implementing Yubaba in Python 3
Try python
Make python segfault in one line
Try the Python LINE Pay SDK
Try implementing extension method in python
Try using LevelDB in Python (plyvel)
Let's try Fizz Buzz in Python
Try to calculate Trace in Python
Try PLC register access in Python
I tried Line notification in Python
Try using Leap Motion in Python
[Introduction] Insert line breaks in Python 3
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Try logging in to qiita with Python
CGI server (1) python edition in one line
Try using the Wunderlist API in Python
Meta-analysis in Python
Try using the Kraken API in Python
Unittest in python
Try working with binary data in Python
Try sending a SYN packet in Python
Epoch in Python
Sudoku in Python
DCI in Python
Python> try: / except:
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Read the file line by line in Python
Read the file line by line in Python
Plink in Python
Constant in python
LINE heroku python
Quickly try Microsoft's Face API in Python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
Decompose command arguments in one line in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
[Python] Invert bool value in one line
Constant in python
nCr in Python.
format in python
Scons in Python3
Try text mining your diary in Python