-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
65 lines (60 loc) · 2.44 KB
/
Copy pathutils.py
File metadata and controls
65 lines (60 loc) · 2.44 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
import os
import json
CONFIG_PATH = "config.json"
API_PATH = "api.json"
DEFAULT_ACTIONS = {
"Any message": [],
"Specific message": [["trigger_text", "Trigger text", "entry"]],
"Timer": [["duration_slider", "Timer (slider, sec)", "slider", 1, 120]],
"Subscription check": [["channel", "Channel (@username)", "entry"]],
"Send message": [["text", "Message text", "entry"], ["file", "Attach file", "file"]],
"Reply in private": [["text", "Private message text", "entry"]],
"Send sticker": [["sticker_id", "Sticker ID", "entry"]],
"Reply to command": [["command", "Command without /", "entry"]],
"Send photo": [["photo", "Choose photo", "file"]],
"Send voice message": [["voice", "Choose voice (ogg/mp3)", "file"]],
"Inline button": [
["text", "Message text", "entry"],
["buttons", "Buttons (JSON)", "entry"],
],
"Switch to": [["to", "Switch to DO", "entry"]]
}
def load_config():
if not os.path.exists(CONFIG_PATH):
data = {"api_keys": [], "DO1": []}
save_config(data)
return data
try:
with open(CONFIG_PATH, "r", encoding="utf-8") as f:
data = json.load(f)
if "api_key" in data:
data["api_keys"] = [data.pop("api_key")]
save_config(data)
if "api_keys" not in data:
data["api_keys"] = []
if "DO1" not in data:
data["DO1"] = []
return data
except Exception as e:
print(f"err cfg load: {e}")
return {"api_keys": [], "DO1": []}
def save_config(data):
with open(CONFIG_PATH, "w", encoding="utf-8") as f:
json.dump(data, f, indent=4, ensure_ascii=False)
print("Saved config [config.json]")
def load_actions():
if not os.path.exists(API_PATH):
with open(API_PATH, "w", encoding="utf-8") as f:
json.dump(DEFAULT_ACTIONS, f, indent=2, ensure_ascii=False)
return DEFAULT_ACTIONS
try:
with open(API_PATH, "r", encoding="utf-8") as f:
data = json.load(f)
if isinstance(data, dict) and data:
return data
raise ValueError("error format actions")
except Exception:
with open(API_PATH, "w", encoding="utf-8") as f:
json.dump(DEFAULT_ACTIONS, f, indent=2, ensure_ascii=False)
print("Loaded default actions config.")
return DEFAULT_ACTIONS