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 ...)
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 []).
Easy! !! !! </ strong>
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