-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathutils.py
184 lines (151 loc) · 5.58 KB
/
utils.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# utils.py
# handles your-bot utilities
from os import path, listdir
from datetime import datetime as dtime, timedelta as tdelta
import logging
from time import sleep
allowLogging = True
now = dtime.now()
_time = now.strftime("%d-%m-%Y")
ROOT_DIR = path.dirname(__file__)
ROOT_DIR = path.dirname(ROOT_DIR)
FILES_DIR = path.join(ROOT_DIR, "files")
RESOURCE_DIR = path.join(ROOT_DIR, "resource")
DATABASE_DIR = path.join(ROOT_DIR, "databases")
STATS_DIR = path.join(ROOT_DIR, "stats")
DEV_STATS_DIR = path.join(path.abspath(STATS_DIR), 'development')
PRO_STATS_DIR = path.join(path.abspath(STATS_DIR), 'production')
MODR_LOG_DIR = path.join(ROOT_DIR, "Logs") # keep log file for moderator
FOOTER = "\n\n*YOUR COMPANY MOTO*📡_" # your org moto e.g "Your #1 stop shop for earthmoving machinery🚩"
# TODO Use CSV or databases files
admin_file = "admin_help.txt"
help_file = "help.txt"
dev_file = "dev_help.txt"
command_file = "commands.txt"
notify_db = "notify.txt"
suggested_db = "suggestedProducts.txt"
feedback_db = f"feedback_{_time}_.txt"
moderator_file = f'bot_log_{_time}_.log'
admin_help = path.join(FILES_DIR, admin_file)
help_general = path.join(FILES_DIR, help_file)
commands = path.join(FILES_DIR, command_file)
suggested = path.join(DATABASE_DIR, suggested_db)
feedback = path.join(DATABASE_DIR, feedback_db)
subscribed_clients = path.join(DATABASE_DIR, notify_db)
if allowLogging:
# FIXME: initiate logging
pass
def logStats(number, command, prod_env=False):
# log bot stats
number = number.strip('whatsapp:+')
if prod_env:
# TODO:: log stats during production
pass
def log(message='', level=0):
if allowLogging:
if level == 0:
logging.debug(message)
if level == -1:
logging.error(message)
if level == 1:
logging.warning(message)
def developerProfile():
# show some love 🍻
profile = "About the bot developer 👨💻\n_Developer_: Donn🐾\nFollow 👇\n_On GitHub_: https://github.com/DonnC\n_Whatsapp_: https://wa.me/263778060126"
return profile
def checkFile(fpath_):
if path.isfile(fpath_):
return True
def notifyClients():
# automatically notify your clients on whatsapp on any notices the admin | moderator provides
contacts = []
if checkFile(subscribed_clients):
with open(subscribed_clients, encoding="utf8", errors='ignore') as clients_db:
db = clients_db.readlines()
if len(db) > 0:
db = set(db) # avoid duplicates
for number in db:
if len(number) > 11:
num = "whatsapp:+" + number.strip()
contacts.append(num)
return contacts
def saveBackNotify(lines_list):
# save back clients to notify after a -notify
lines = ""
for line in lines_list:
line = line.strip()
lines += line + '\n'
with open(subscribed_clients, 'w', encoding="utf8", errors='ignore') as notfy:
notfy.writelines(str(lines))
lines = ""
def subscribe(contact):
# subscribe a client to your notify list
contact = contact.strip("whatsapp:+")
if checkFile(subscribed_clients):
with open(subscribed_clients, 'a+', errors='ignore') as clients_db:
#with open('../databases/notify.txt', 'a+') as clients_db:
clients_db.write(f'{contact}\n')
def Command():
# get your commands in the bot's environment
commands_ = []
if checkFile(commands):
with open(commands, encoding="utf8", errors='ignore') as f:
command = f.readlines()
for command_ in command:
if len(command_) > 3 and not command_.startswith('//'):
commands_.append(command_.strip())
return commands_
def saveClientSuggestion(client, client_suggestion):
# get client suggestions and logs them to file
if checkFile(suggested):
_time = now.strftime("%d-%m-%Y_%H-%M-%S")
# TODO:: Save | log user suggestion
pass
def helpAdmin():
# get admin's help text
if checkFile(admin_help):
with open(admin_help, encoding="utf8", errors='ignore') as h:
help_file = h.read()
return help_file
def helpText():
# get general help text for users
if checkFile(help_general):
with open(help_general, encoding="utf8", errors='ignore') as hf:
help_ = hf.read()
return help_
def saveFeedback(body, contact):
# TODO: Save | log user feedback
pass
def restruct(bdy):
txt = ""
if type(bdy) == list:
for t in bdy:
txt += t + " "
return txt.strip()
else:
return bdy
def getFeedback():
# get all user feedback for the admin | moderator
fdbacks = ""
if checkFile(feedback):
with open(feedback, encoding="utf8", errors='ignore') as fdbck_db:
data = fdbck_db.readlines()
if len(data) > 0:
for fdbck in data:
all_them = fdbck.split(',')
fdback = f"*{all_them[0]}* : {restruct(all_them[1:])}\n"
fdbacks += fdback
fdbacks.strip()
return fdbacks
# handles resources, fileIO on server files
def getAgents():
agents_ = "<YOUR AGENT DETAILS IF ANY>"
return agents_
def getAdress():
adrress = "<YOUR ADDRESSES IF ANY>"
return adrress
def getModLog():
# TODO: send log file to moderator
# TODO: get number of the moder log files
numLogs = 0
return numLogs