-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.py
71 lines (45 loc) · 1.55 KB
/
state.py
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
import json
import os
from pymongo import MongoClient
client = MongoClient('mongodb', 27017)
db = client.langbot
class State:
def __init__(self):
if not os.path.exists('state.json'):
with open('state.json', 'w+') as j:
json.dump(dict("{}"), j)
j.close()
self.state = ''
@property
def get(self):
return self.state
def set_action(self, chat_id, action):
self.state[chat_id][0] = action
def set_dest(self, chat_id, dest):
self.state[chat_id][1] = dest
def check(self, chat_id):
chat_id = str(chat_id)
if self.state == '':
self.load(chat_id)
if chat_id not in self.state:
self.state[chat_id]=["reply", "en"]
return chat_id
def load(self, chat_id):
with open('state.json', 'r') as j:
jsonstate = json.load(j)
if chat_id in jsonstate:
self.state = {chat_id: [jsonstate[chat_id][0], jsonstate[chat_id][1]]}
else:
self.state = {chat_id: ["reply", "en"]}
j.close()
def save(self, chat_id):
json_data = {}
with open('state.json', 'r') as j:
json_data = json.load(j)
if isinstance(json_data, str):
json_data = dict(eval(json_data))
json_data[chat_id] = [self.state[chat_id][0], self.state[chat_id][1]]
with open('state.json', 'w') as file:
json.dump(json_data, file)
j.close()
file.close()