-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
401 lines (342 loc) · 13.7 KB
/
main.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import telebot
import json
import os
import pyrebase
import random
from dotenv import load_dotenv
from telebot import types
from urllib.request import urlopen
load_dotenv()
firebase_config = {
"apiKey": os.getenv("apiKey"),
"authDomain": os.getenv("authDomain"),
"databaseURL": os.getenv("databaseURL"),
"projectId": os.getenv("projectId"),
"storageBucket": os.getenv("storageBucket"),
"messagingSenderId": os.getenv("messagingSenderId"),
"appId": os.getenv("appId"),
"serviceAccount":
{
"type": os.getenv("type"),
"project_id": os.getenv("project_id"),
"private_key_id": os.getenv("private_key_id"),
"private_key": os.getenv("private_key"),
"client_email": os.getenv("client_email"),
"client_id": os.getenv("client_id"),
"auth_uri": os.getenv("auth_uri"),
"token_uri": os.getenv("token_uri"),
"auth_provider_x509_cert_url": os.getenv("auth_provider_x509_cert_url"),
"client_x509_cert_url": os.getenv("client_x509_cert_url")
}
}
token = os.getenv('API_TOKEN')
bot = telebot.TeleBot(token, parse_mode=None)
firebase = pyrebase.initialize_app(firebase_config)
storage = firebase.storage()
data_dir = "data/"
def get_data(chat_id):
if not os.path.isdir(data_dir):
os.mkdir(data_dir)
path = data_dir + str(chat_id) + ".json"
bucket = storage.bucket
blob = bucket.get_blob(path)
if blob:
url = storage.child(path).get_url(None)
response = urlopen(url)
data = json.loads(response.read())
else:
data = {}
save_data(chat_id,data)
return data
def add_to_dict(chat_id,listName,data,newEntry=None):
if listName in data:
data[listName].append(newEntry)
bot.send_message(chat_id,"Added " + newEntry + " into " + listName + ".")
else:
if not newEntry:
data[listName] = []
bot.send_message(chat_id,"Created list called "+ listName +".")
save_data(chat_id,data)
def save_data(chat_id,data):
path = data_dir + str(chat_id) + ".json"
with open(path, "w") as write_file:
json.dump(data, write_file)
storage.child(path).put(path)
def remove_data(chat_id,data,listName,removeItem):
if data:
if listName in data:
if removeItem in data[listName]:
data[listName].remove(removeItem)
bot.send_message(chat_id,"Successfully removed " + removeItem + " from "+ listName + ".")
else:
bot.send_message(chat_id,"No such item in the list.")
save_data(chat_id,data)
def get_lists_markup(data):
markup = types.ReplyKeyboardMarkup(one_time_keyboard=True)
kbList = []
new_row = []
for key in data.keys():
new_row.append(types.KeyboardButton(key).to_dict())
if len(new_row) == 3:
kbList.append(new_row)
new_row=[]
elif key == list(data.keys())[-1]:
kbList.append(new_row)
markup.keyboard = kbList
return markup
def get_items_markup(targetList,data):
markup = types.ReplyKeyboardMarkup(one_time_keyboard=True)
kbList = []
new_row = []
for item in data[targetList]:
new_row.append(types.KeyboardButton(item).to_dict())
if len(new_row) == 3:
kbList.append(new_row)
new_row=[]
elif item == data[targetList][-1]:
kbList.append(new_row)
markup.keyboard = kbList
return markup
def getItems(targetList,data):
if data:
all_items = ""
if targetList in data:
for items in data[targetList]:
all_items += "- "+ items + "\n"
else:
return False
else:
return False
return all_items
@bot.message_handler(commands=['start'])
def start(message):
bot.send_message(message.chat.id,
"""
Here are all the available commands:
/new - Create a new list.
/delete - Delete an existing list.
/clear - Clear all items from an existing list.
/add - Add an item to an existing list.
/remove - Remove an item from an existing list.
/show - Show all items from an existing list.
/all - Show all items from all existing lists.
/random - Will return a random item from an existing list.
""")
@bot.message_handler(commands=['add'])
def list_to_add(message):
chat_id = message.chat.id
data = get_data(chat_id)
markup = get_lists_markup(data)
if markup.keyboard:
msg = bot.send_message(chat_id, "Choose your list:", reply_markup=markup)
bot.register_next_step_handler(msg, lambda m :nameItem(m,data))
else:
bot.send_message(chat_id,"You have not created a list.")
def nameItem(message,data):
chat_id = message.chat.id
if message.text:
targetList = message.text
if targetList in data:
markup = types.ForceReply()
msg = bot.send_message(chat_id, "Enter name for item to be added to the list.", reply_markup=markup)
bot.register_next_step_handler(msg,lambda m : saveToList(m,targetList,data))
else:
bot.send_message(chat_id,"The list " + targetList + " has not been created.")
else:
bot.send_message(chat_id,"Sorry, I only support text and emojis.")
markup = get_lists_markup(chat_id)
msg = bot.send_message(chat_id, "Choose your list:", reply_markup=markup)
bot.register_next_step_handler(msg, nameItem)
def saveToList(message,targetList,data):
chat_id = message.chat.id
if message.text:
add_to_dict(chat_id,targetList,data,message.text)
else:
bot.send_message(chat_id,"Sorry, I only support text and emojis.")
markup = types.ForceReply()
msg = bot.send_message(chat_id, "Enter name for item to be added to the list.", reply_markup=markup)
bot.register_next_step_handler(msg,lambda m : saveToList(m,targetList))
@bot.message_handler(commands=['remove'])
def list_to_remove(message):
chat_id = message.chat.id
data = get_data(chat_id)
markup = get_lists_markup(data)
if markup.keyboard:
msg = bot.send_message(chat_id, "Choose your list:", reply_markup=markup)
bot.register_next_step_handler(msg, lambda m : itemFromList(m,data))
else:
bot.send_message(chat_id,"You have not created a list.")
def itemFromList(message,data):
chat_id = message.chat.id
if message.text:
targetList = message.text
all_items = getItems(targetList,data)
if not all_items:
bot.send_message(chat_id,"This list is empty or does not exist.")
else:
bot.send_message(chat_id,all_items)
markup = get_items_markup(targetList,data)
msg = bot.send_message(chat_id, "Choose item to be removed from the list.", reply_markup=markup)
bot.register_next_step_handler(msg,lambda m:removeFromList(m,targetList,all_items,data))
else:
bot.send_message(chat_id,"Sorry, I only support text and emojis.")
markup = get_lists_markup(chat_id)
msg = bot.send_message(chat_id, "Choose your list:", reply_markup=markup)
bot.register_next_step_handler(msg, itemFromList)
def removeFromList(message,targetList,all_items,data):
chat_id = message.chat.id
if message.text:
remove_data(chat_id,data,targetList,message.text)
else:
bot.send_message(chat_id,"Sorry, I only support text and emojis.")
bot.send_message(chat_id,all_items)
markup = get_items_markup(targetList,data)
msg = bot.send_message(chat_id, "Choose item to be removed from the list.", reply_markup=markup)
bot.register_next_step_handler(msg,lambda m:removeFromList(m,targetList,all_items,data))
@bot.message_handler(commands=['new'])
def new(message):
chat_id = message.chat.id
markup = types.ForceReply()
msg = bot.send_message(chat_id, "Enter name for the new list.", reply_markup=markup)
bot.register_next_step_handler(msg,newList)
def newList(message):
chat_id = message.chat.id
if message.text:
data = get_data(chat_id)
targetList = message.text
if targetList in data:
bot.send_message(chat_id,"This list already exists.")
else:
add_to_dict(chat_id,targetList,data)
else:
bot.send_message(chat_id,"Sorry, I only support text and emojis.")
markup = types.ForceReply()
msg = bot.send_message(chat_id, "Enter name for the new list.", reply_markup=markup)
bot.register_next_step_handler(msg,newList)
@bot.message_handler(commands=['delete'])
def list_to_delete(message):
chat_id = message.chat.id
data = get_data(chat_id)
markup = get_lists_markup(data)
if markup.keyboard:
msg = bot.send_message(chat_id, "Choose list to be deleted.", reply_markup=markup)
bot.register_next_step_handler(msg,lambda m:deleteList(m,data))
else:
bot.send_message(chat_id,"You have not created a list.")
def deleteList(message,data):
chat_id = message.chat.id
if message.text:
targetList = message.text
if targetList in data:
data.pop(targetList)
save_data(chat_id,data)
bot.send_message(chat_id,targetList+" has been deleted.")
else:
bot.send_message(chat_id,targetList+" does not exist.")
else:
bot.send_message(chat_id,"Sorry, I only support text and emojis.")
markup = get_lists_markup(data)
msg = bot.send_message(chat_id, "Choose list to be deleted.", reply_markup=markup)
bot.register_next_step_handler(msg,lambda m:deleteList(m,data))
@bot.message_handler(commands=['show'])
def list_to_show(message):
chat_id = message.chat.id
data = get_data(chat_id)
markup = get_lists_markup(data)
if data:
msg = bot.send_message(chat_id, "Choose your list:", reply_markup=markup)
bot.register_next_step_handler(msg, lambda m : showList(m,data))
else:
bot.send_message(chat_id,"You have not created a list.")
def showList(message,data):
chat_id = message.chat.id
if message.text:
targetList = message.text
all_items = getItems(targetList,data)
if not all_items:
bot.send_message(chat_id,"This list is empty.")
else:
bot.send_message(chat_id,targetList+" :")
bot.send_message(chat_id,all_items)
else:
markup = get_lists_markup(data)
if data:
bot.send_message(chat_id,"Sorry, I only support text and emojis.")
msg = bot.send_message(chat_id, "Choose your list:", reply_markup=markup)
bot.register_next_step_handler(msg, lambda m : showList(m,data))
else:
bot.send_message(chat_id,"You have not created a list.")
@bot.message_handler(commands=['all'])
def all(message):
chat_id = message.chat.id
data = get_data(chat_id)
if data:
all_items =""
for key in data.keys():
all_items += key + " :\n"
if getItems(key,data):
all_items+= getItems(key,data)
all_items += "\n"
else:
all_items += "Empty list. \n\n"
bot.send_message(chat_id,all_items)
else:
bot.send_message(chat_id,"You have not created a list.")
@bot.message_handler(commands=['random'])
def list_to_random(message):
chat_id = message.chat.id
data = get_data(chat_id)
markup = get_lists_markup(data)
if markup.keyboard:
msg = bot.send_message(chat_id,"Select the list you want to randomly chose from.",reply_markup=markup)
bot.register_next_step_handler(msg,lambda m:chooseRandom(m,data))
else:
bot.send_message(chat_id,"You have not created a list.")
def chooseRandom(message,data):
chat_id = message.chat.id
if message.text:
targetList = message.text
if targetList in data:
if data[targetList]:
random_item = random.choice(data[targetList])
bot.send_message(chat_id,"- " + random_item)
else:
bot.send_message(chat_id,"This list is empty.")
else:
bot.send_message(chat_id,"This list does not exists.")
else:
markup = get_lists_markup(data)
if markup.keyboard:
bot.send_message(chat_id,"Sorry, I only support text and emojis.")
msg = bot.send_message(chat_id,"Select the list you want to randomly chose from.",reply_markup=markup)
bot.register_next_step_handler(msg,lambda m:chooseRandom(m,data))
@bot.message_handler(commands=['clear'])
def list_to_clear(message):
chat_id = message.chat.id
data = get_data(chat_id)
markup = get_lists_markup(data)
if markup.keyboard:
msg = bot.send_message(chat_id,"Select the list you want to clear.",reply_markup=markup)
bot.register_next_step_handler(msg,lambda m:clearList(m,data))
else:
bot.send_message(chat_id,"You have not created a list.")
def clearList(message,data):
chat_id = message.chat.id
if message.text:
targetList = message.text
if targetList in data:
if data[targetList]:
data[targetList] = []
save_data(chat_id,data)
bot.send_message(chat_id,"The list has been cleared.")
else:
bot.send_message(chat_id,"This list is already empty.")
else:
bot.send_message(chat_id,"This list does not exists.")
else:
bot.send_message(chat_id,"Sorry, I only support text and emojis.")
markup = get_lists_markup(data)
if markup.keyboard:
msg = bot.send_message(chat_id,"Select the list you want to clear.",reply_markup=markup)
bot.register_next_step_handler(msg,lambda m:clearList(m,data))
bot.polling()