-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlinenotify.py
More file actions
96 lines (89 loc) · 3.27 KB
/
linenotify.py
File metadata and controls
96 lines (89 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from urequests import post
__version__ = '0.0.1'
__author__ = 'Teeraphat Kullanankanjana'
class LineNotify():
"""
It is a MicroPython library for sending notifications to Line Notify,
which can be used with ESP8266 and ESP32.
Can send both text messages, stickers and images.
"""
def __init__(self,token):
self.__token = token
self.__url = 'https://notify-api.line.me/api/notify'
self.__header = {'content-type':'application/x-www-form-urlencoded',
'Authorization':'Bearer '+self.__token}
def __sendRequest(self,payload):
"""
Posting header and payload to the Line Notify API.
-----
Parameter:
(str)payload => Request parameters
---
Return:
(int)req.status_code => HTTP status code from Line server
"""
try:
req = post(self.__url,headers=self.__header,data=payload)
req.close()
return req.status_code
except OSError as err:
if err.errno == -202:
print('No Internet Connection!!')
print('Try again!!')
def notify(self,msg):
"""
Notify only text message to Line Notify
-----
Parameter:
(str)msg => The message you want to send notifications to LINE Notify.
---
Return:
(int)httpCode => HTTP status code from Line server
200: Success
400: Bad request
401: Invalid access token
500: Failure due to server error
Other: Processed over time or stopped
"""
payload = 'message={}'.format(msg).encode('utf-8')
httpCode = self.__sendRequest(payload)
return httpCode
def notifySticker(self,packageID,stickerID,msg='\n'):
"""
Notify Sticker and text message to Line Notify
-----
Parameter:
(int)stickerPackageId => Sticker Package ID.
(int)stickerID => Sticker ID.
(str)msg => The message you want to send notifications to LINE Notify.
---
Return:
(int)httpCode => HTTP status code from Line server
200: Success
400: Bad request
401: Invalid access token
500: Failure due to server error
Other: Processed over time or stopped
"""
payload = 'message={}&stickerPackageId={}&stickerId={}'.format(msg,packageID,stickerID).encode('utf-8')
httpCode = self.__sendRequest(payload)
return httpCode
def notifyImageURL(self,url,msg='\n'):
"""
Notify Image from URL and text message to Line Notify
-----
Parameter:
(str)url => Image URL (HTTP/HTTPS format)
(str)msg => The message you want to send notifications to LINE Notify.
---
Return:
(int)httpCode => HTTP status code from Line server
200: Success
400: Bad request
401: Invalid access token
500: Failure due to server error
Other: Processed over time or stopped
"""
payload = 'message={}&imageThumbnail={}&imageFullsize={}'.format(msg,url,url).encode('utf-8')
httpCode = self.__sendRequest(payload)
return httpCode