-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathline_notify.py
More file actions
67 lines (58 loc) · 2.64 KB
/
line_notify.py
File metadata and controls
67 lines (58 loc) · 2.64 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
# Klipper plugin line notify
# import sys
# sys.path.append("/usr/lib/python3/dist-packages")
import urllib.request
import urllib.parse
import json
class LineNotify:
def __init__(self, config) -> None:
self.name = config.get_name().split()[-1]
self.printer = config.get_printer()
self.gcode = self.printer.lookup_object('gcode')
# Get configuration
self.access_token = config.get('access_token')
self.timeout = config.get('timeout', 10)
# Register commands
self.gcode.register_command(
"PUSH_LINE_NOTIFY",
self.PUSH_LINE_NOTIFY,
desc=self.CMD_PUSH_LINE_NOTIFY_HELP)
CMD_PUSH_LINE_NOTIFY_HELP = "Sending message to Line Notify"
def PUSH_LINE_NOTIFY(self, params):
message = params.get('MSG', '')
silent = params.get('SILENT', 'False').lower() == 'true'
# Function usage
if message == '':
self.gcode.respond_info("""
Line Notify
USAGE: PUSH_LINE_NOTIFY MSG="message" [SILENT=False]
Parameters:
MSG: string (required) - The message to send
SILENT: boolean (optional) - If set to True, don't send notification when send message
Examples:
PUSH_LINE_NOTIFY MSG="Print completed"
PUSH_LINE_NOTIFY MSG="Print progress: 50%"
PUSH_LINE_NOTIFY MSG="Print started" SILENT=True
""")
return
url = 'https://notify-api.line.me/api/notify'
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Bearer ' + self.access_token
}
data = urllib.parse.urlencode({'message': message, 'notificationDisabled': silent}).encode('utf-8')
try:
req = urllib.request.Request(url, data=data, headers=headers, method='POST')
with urllib.request.urlopen(req, timeout=self.timeout) as response:
if response.status == 200:
response_data = json.loads(response.read().decode('utf-8'))
message = response_data.get('message')
self.gcode.respond_info(f"Message from LINE Notify: {message}")
else:
raise self.gcode.error(f"Message from LINE Notify: HTTP error {response.status}")
except urllib.error.URLError as e:
raise self.gcode.error(f"Message from LINE Notify: Connection error - {str(e)}")
except Exception as e:
raise self.gcode.error(f"Message from LINE Notify: Unexpected error - {str(e)}")
def load_config(config):
return LineNotify(config)