forked from xinbei-shen/BotMaMa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
1041 lines (948 loc) · 46.8 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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import logging, datetime, pytz, telepot, urllib3, time
from telegram import InlineKeyboardButton, ReplyKeyboardRemove, Update, ReplyKeyboardMarkup, InlineKeyboardMarkup, constants, Bot
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext, ConversationHandler, CallbackQueryHandler
from dbhelper import DBHelper
from google.cloud import storage
from firebase import firebase
from flask import Flask, request, Response
from flask_sslify import SSLify
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support import expected_conditions as EC
API_KEY = os.getenv('API_KEY')
NAME, PHOTO, SERVINGS, INGREDIENTS, STEPS, SEND_RECIPE, CONFIRMATION, DELETION = range(8)
RECIPE_CHOICE, RECIPE_PART, EDIT_NAME, EDIT_PHOTO, EDIT_SERVINGS, EDIT_INGREDIENTS, EDIT_STEPS, END_ROUTES = range(8,16)
ADD_INGREDIENT, UPDATE_INGREDIENT, SAVE_INGREDIENT, DELETE_INGREDIENT = range(16,20)
ADD_STEP, UPDATE_STEP, SAVE_STEP, DELETE_STEP = range(20,24)
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO
)
logger = logging.getLogger(__name__)
db = DBHelper()
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]='credentials.json'
firebase = firebase.FirebaseApplication(os.getenv('DB_URL'))
client = storage.Client()
bucket = client.get_bucket(os.getenv('FIREBASE_URL'))
def build_keyboard(items):
keyboard = [[item] for item in items]
markup = ReplyKeyboardMarkup(keyboard, resize_keyboard=True, one_time_keyboard=True)
return markup
def build_inline_keyboard(buttons):
keyboard = [[InlineKeyboardButton(col, callback_data=col) for col in row] for row in buttons]
markup = InlineKeyboardMarkup(keyboard)
return markup
def get_ingredient_list(user_id, recipe_name):
ingredient_list = 'Ingredients\n'
for ingredient in db.get_ingredients(user_id, recipe_name):
ingredient_list = ingredient_list + "- " + ingredient + "\n"
return ingredient_list
def get_step_list(user_id, recipe_name):
step_list = 'Directions\n'
count = 1
for step in db.get_steps(user_id, recipe_name):
step_list = step_list + str(count) + ". " + step + "\n"
count+=1
return step_list
def full_recipe(user_id, recipe_name):
if not db.is_public(user_id, recipe_name):
msg = recipe_name + " [private]\n\n"
else:
msg = recipe_name + " [public]\n\n"
servings = db.get_servings(user_id, recipe_name)
ingredients = get_ingredient_list(user_id, recipe_name)
steps = get_step_list(user_id, recipe_name)
if len(servings) == 0:
msg = msg + ingredients + "\n" + steps
else:
msg = msg + "Serves " + servings[0] + "\n\n" + ingredients + "\n" + steps
return msg
def start(update: Update, _: CallbackContext) -> None:
"""Send a message when the command /start is issued."""
user = update.effective_user
username = user.username
if username == None:
db.add_user(user.id, update.message.chat_id, "None")
else:
db.add_user(user.id, update.message.chat_id, username)
update.message.reply_markdown_v2(
fr"Hi {user.mention_markdown_v2()}\! I'm BotMaMa\! "
fr"I can help you manage your recipes and even search for new ones from all over the web\.{os.linesep}"
fr"{os.linesep}To add a new recipe, use /add\."
fr"{os.linesep}To view your recipes, use /view\."
fr"{os.linesep}To edit your recipes, use /edit\."
fr"{os.linesep}To delete a recipe, use /delete\."
fr"{os.linesep}Search for recipes of other users using /search \@\<username\>\. Currently, this only works for users with a username\."
fr"{os.linesep}Alternatively, search for new recipes from the internet with /search \<search term\>\."
)
def add_recipe(update: Update, _: CallbackContext) -> int:
"""Asks user for the recipe name."""
_.user_data.clear()
db.update_username(update.message.from_user.id, update.message.from_user.username)
print(isinstance(update.message.from_user.username, str))
update.message.reply_text(
"Please tell me the name of your new recipe or type /cancel if you change your mind anytime!\n"
"All recipes are set to public by default. You can change this by editing the recipe later. "
"Private recipes will not be accessed by other users."
)
return NAME
def name(update: Update, _: CallbackContext) -> int:
"""Stores given name and asks for a photo of the recipe."""
user_id = update.message.from_user.id
db.update_username(user_id, update.message.from_user.username)
recipes = db.get_recipes(user_id)
recipe_name = update.message.text
if recipe_name in recipes:
update.message.reply_text("Recipe name already exists! Please choose a different name.")
return NAME
elif recipe_name == "remove yield":
update.message.reply_text("Sorry! Please choose a different name.")
return NAME
else:
db.add_recipe(user_id, recipe_name)
_.user_data['recipe name'] = recipe_name
update.message.reply_text(
"Perfect! Next, please send a picture of your food so Mama knows what it looks like.\n"
"Type /skip if you do not have a photo to show Mama."
)
return PHOTO
def photo(update: Update, _:CallbackContext) -> int:
"""Stores the given photo and asks for the yield of the recipe."""
db.update_username(update.message.from_user.id, update.message.from_user.username)
photo = update.message.photo[-1].get_file()
blob_name = str(update.message.from_user.id) + "-" + _.user_data['recipe name'] + ".jpg"
photo.download(blob_name) #downloads to local directory
blob = bucket.blob(blob_name)
blob.upload_from_filename(blob_name)
blob.make_public()
db.add_picture_url(update.message.from_user.id, _.user_data['recipe name'], blob.public_url)
os.remove(blob_name) #deletes photo from local directory
update.message.reply_text(
"Mama is impressed! Next, please state the yield of your recipe i.e. how many people or how much food your recipe serves.\n"
"Type /skip if you are not sure."
)
return SERVINGS
def skip_photo(update: Update, _: CallbackContext) -> int:
"""Skips the photo and asks for the yield of the recipe."""
db.update_username(update.message.from_user.id, update.message.from_user.username)
update.message.reply_text(
"Don't worry. You can always show Mama the next time!\n"
"Next, please state the yield of your recipe i.e. how many people or how much food your recipe serves.\n"
"Type /skip if you are not sure."
)
return SERVINGS
def servings(update: Update, _: CallbackContext) -> int:
"""Stores the yield of the recipe and asks for the ingredients needed."""
db.update_username(update.message.from_user.id, update.message.from_user.username)
servings = update.message.text
recipe_name = _.user_data['recipe name']
db.add_servings(update.message.from_user.id, recipe_name, servings)
update.message.reply_text(
"Okay! Your recipe serves " + servings + ".\n"
"Please tell Mama what ingredients are needed next.\nType /done when you have entered all the ingredients."
)
return INGREDIENTS
def skip_servings(update: Update, _: CallbackContext) -> int:
"""Skips the recipe yield and asks for the ingredients needed."""
db.update_username(update.message.from_user.id, update.message.from_user.username)
update.message.reply_text(
"It's okay. Please tell Mama what ingredients are needed next.\n"
"Type /done when you have entered all the ingredients.")
return INGREDIENTS
def ingredients(update: Update, _: CallbackContext) -> int:
"""Stores the ingredients and asks for the steps."""
user_id = update.message.from_user.id
db.update_username(user_id, update.message.from_user.username)
recipe_name = _.user_data['recipe name']
ingredient = update.message.text
ingredient_list = db.get_ingredients(user_id, recipe_name)
if ingredient == "/done":
update.message.reply_text(
"Impressive! Now please write down the steps to the recipe. Type /done when you have entered all the steps you need."
)
return STEPS
elif ingredient in ingredient_list:
update.message.reply_text("Ingredient has already been added.\nType /done if you have entered all the ingredients needed.")
return INGREDIENTS
else:
db.add_ingredient(user_id, recipe_name, ingredient)
update.message.reply_text(get_ingredient_list(user_id, recipe_name))
return INGREDIENTS
def steps(update: Update, _: CallbackContext) -> int:
"""Stores the steps of the recipe and ends the conversation."""
user_id = update.message.from_user.id
db.update_username(user_id, update.message.from_user.username)
recipe_name = _.user_data['recipe name']
step = update.message.text
if step == "/done":
text = full_recipe(user_id, recipe_name)
photo_url = db.get_picture_url(user_id, recipe_name)
_.user_data.clear()
update.message.reply_text("Terrific! This is your new recipe:")
if len(photo_url) != 0:
update.message.reply_photo(photo_url[0])
update.message.reply_text(text)
return ConversationHandler.END
else:
db.add_step(user_id, recipe_name, step)
update.message.reply_text(get_step_list(user_id, recipe_name))
return STEPS
def cancel_add(update: Update, _: CallbackContext) -> int:
"""Cancels and ends the conversation."""
update.message.reply_text(
"It's OK! You can always come back to Mama whenever you are ready!",
reply_markup=ReplyKeyboardRemove()
)
user_id = update.message.from_user.id
db.update_username(user_id, update.message.from_user.username)
if 'recipe name' in _.user_data:
recipe_name = _.user_data['recipe name']
if len(db.get_picture_url(user_id, recipe_name)) != 0:
blob_name = str(user_id) + "-" + recipe_name + ".jpg"
bucket.delete_blob(blob_name)
db.delete_recipe(user_id, recipe_name)
_.user_data.clear()
return ConversationHandler.END
def view_recipe(update: Update, _: CallbackContext) -> int:
"""Allows user to view an existing recipe."""
user_id = update.message.from_user.id
_.user_data['user id'] = user_id
db.update_username(user_id, update.message.from_user.username)
recipes = db.get_recipes(user_id)
if len(recipes) == 0:
update.message.reply_text("You currently do not have any recipes stored. Use /add to leave your recipes with Mama!")
return ConversationHandler.END
else:
keyboard = build_keyboard(recipes)
update.message.reply_text("Which recipe would you like to view?", reply_markup=keyboard)
return SEND_RECIPE
def send_recipe(update: Update, _: CallbackContext) -> int:
"""Sends the chosen recipe to the user."""
user_id = _.user_data['user id']
db.update_username(update.message.from_user.id, update.message.from_user.username)
recipe_name = update.message.text
now = datetime.now()
timestamp = now.strftime("%d%m%Y%H%M%S")
if recipe_name in db.get_recipes(user_id):
if user_id == update.message.from_user.id or db.is_public(user_id, recipe_name):
photo_url = db.get_picture_url(user_id, recipe_name)
if len(photo_url) != 0:
update.message.reply_photo(photo_url[0] + "?a=" + timestamp)
update.message.reply_text(full_recipe(user_id, recipe_name), reply_markup=ReplyKeyboardRemove())
else:
update.message.reply_text("Sorry, you are unable to view this recipe as it has been set to private.", reply_markup=ReplyKeyboardRemove())
else:
update.message.reply_text("Sorry, Mama couldn't find the recipe.", reply_markup=ReplyKeyboardRemove())
return ConversationHandler.END
def edit_recipe(update: Update, _: CallbackContext) -> int:
"""Allows user to edit the details of a stored recipe."""
user_id = update.message.from_user.id
db.update_username(user_id, update.message.from_user.username)
_.user_data['user id'] = user_id
recipes = db.get_recipes(user_id)
if len(recipes) == 0:
update.message.reply_text("Hmm, it seems like you do not have any recipes stored currently. Type /add to start adding new recipes!")
return ConversationHandler.END
else:
keyboard = build_inline_keyboard([[recipe] for recipe in recipes])
update.message.reply_text("Which recipe would you like to edit?", reply_markup=keyboard)
return RECIPE_CHOICE
def edit_recipe_inline(update: Update, _: CallbackContext) -> int:
"""Allows user to choose a different recipe to edit when using inline buttons."""
query = update.callback_query
query.answer()
db.update_username(query.from_user.id, query.from_user.username)
recipes = db.get_recipes(_.user_data['user id'])
keyboard = build_inline_keyboard([[recipe] for recipe in recipes])
query.edit_message_text("Which recipe would you like to edit?")
query.edit_message_reply_markup(keyboard)
return RECIPE_CHOICE
def recipe_choice(update: Update, _: CallbackContext) -> int:
"""Stores the name of the recipe to be edited."""
recipe_name = update.callback_query
recipe_name.answer()
_.user_data['recipe name'] = recipe_name.data
user_id = _.user_data['user id']
db.update_username(user_id, recipe_name.from_user.username)
servings = db.get_servings(user_id, recipe_name.data)
photo_url = db.get_picture_url(user_id, recipe_name.data)
recipe = full_recipe(user_id, recipe_name.data)
buttons = ["recipe name"]
if len(photo_url) == 0:
buttons.append("add photo")
else:
now = datetime.now()
timestamp = now.strftime("%d%m%Y%H%M%S")
recipe_name.message.reply_photo(photo_url[0] + "?=a" + timestamp)
buttons.append("photo")
buttons.append("add servings") if len(servings) == 0 else buttons.append("servings")
if not db.is_public(user_id, recipe_name.data):
keyboard = build_inline_keyboard([buttons, ["ingredients", "directions"], ["set public"], ["<< back to list of recipes"]])
else:
keyboard = build_inline_keyboard([buttons, ["ingredients", "directions"], ["set private"], ["<< back to list of recipes"]])
recipe_name.message.reply_text(
recipe + "\nYou are currently editing '" + recipe_name.data + "'.\n"
"Which part of the recipe would you like to edit?",
reply_markup=keyboard
)
recipe_name.edit_message_reply_markup(None)
return RECIPE_PART
def toggle_privacy(update: Update, _: CallbackContext) -> int:
query = update.callback_query
query.answer()
user_id = _.user_data['user id']
db.update_username(user_id, query.from_user.username)
recipe_name = _.user_data['recipe name']
photo_url = db.get_picture_url(user_id, recipe_name)
buttons = ["recipe name"]
if len(photo_url) == 0:
buttons.append("add photo")
else:
now = datetime.now()
timestamp = now.strftime("%d%m%Y%H%M%S")
recipe_name.message.reply_photo(photo_url[0] + "?=a" + timestamp)
buttons.append("photo")
buttons.append("add servings") if len(db.get_servings(user_id, recipe_name)) == 0 else buttons.append("servings")
if query.data == "set private":
db.change_privacy(user_id, recipe_name, 0)
keyboard = build_inline_keyboard([buttons, ["ingredients", "directions"], ["set public"], ["<< back to list of recipes"]])
query.edit_message_text("Recipe has been set to private.\nWhat else would you like to edit?")
else:
db.change_privacy(user_id, recipe_name, 1)
keyboard = build_inline_keyboard([buttons, ["ingredients", "directions"], ["set private"], ["<< back to list of recipes"]])
query.edit_message_text("Recipe has been set to public.\nWhat else would you like to edit?")
query.edit_message_reply_markup(keyboard)
return RECIPE_PART
def edit_name(update: Update, _: CallbackContext) -> int:
"""Asks user for the new name of the recipe."""
query = update.callback_query
query.answer()
db.update_username(query.from_user.id, query.from_user.username)
current_name = _.user_data['recipe name']
keyboard = [[InlineKeyboardButton("<< back", callback_data=current_name)]]
query.edit_message_text(
"Your recipe name is currently '" + current_name + "'.\n"
"What would you like the new name of your recipe to be?"
)
query.edit_message_reply_markup(InlineKeyboardMarkup(keyboard))
return EDIT_NAME
def change_name(update: Update, _: CallbackContext) -> int:
"""Updates the recipe's name in the database."""
new_name = update.message.text
user_id = _.user_data['user id']
db.update_username(user_id, update.message.from_user.username)
current_name = _.user_data['recipe name']
if new_name == current_name or new_name in db.get_recipes(user_id) or new_name == "remove yield":
keyboard = [[InlineKeyboardButton("<< back", callback_data=current_name)]]
update.message.reply_text(
"Sorry, please choose a different name.",
reply_markup=InlineKeyboardMarkup(keyboard)
)
return EDIT_NAME
db.update_name(user_id, current_name, new_name)
_.user_data['recipe name'] = new_name
servings = db.get_servings(user_id, new_name)
photo_url = db.get_picture_url(user_id, new_name)
buttons = ["recipe name"]
buttons.append("add servings") if len(servings) == 0 else buttons.append("servings")
if len(photo_url) == 0:
buttons.append("add photo")
else:
old_blob_name = str(user_id) + "-" + current_name + ".jpg"
new_blob_name = str(user_id) + "-" + new_name + ".jpg"
blob = bucket.rename_blob(bucket.get_blob(old_blob_name), new_blob_name)
blob.make_public()
db.add_picture_url(user_id, new_name, blob.public_url)
buttons.append("photo")
if not db.is_public(user_id, new_name):
keyboard = build_inline_keyboard([buttons, ["ingredients", "directions"], ["set public"], ["<< back to list of recipes"]])
else:
keyboard = build_inline_keyboard([buttons, ["ingredients", "directions"], ["set private"], ["<< back to list of recipes"]])
update.message.reply_text(
"The name of your recipe has been changed from '" + current_name + "' to '" + new_name + "'.\n"
"What else would you like to edit?",
reply_markup=keyboard
)
return RECIPE_PART
def edit_photo(update: Update, _: CallbackContext) -> int:
"""Asks user to update the photo."""
query = update.callback_query
query.answer()
user_id = _.user_data['user id']
db.update_username(user_id, query.from_user.username)
recipe_name = _.user_data['recipe name']
if query.data == "photo":
keyboard = [[InlineKeyboardButton("remove photo", callback_data="remove photo")], [InlineKeyboardButton("<< back", callback_data=recipe_name)]]
query.edit_message_text("Please send Mama an updated photo of your recipe. Mama is excited to see what changes you have made!")
else:
keyboard = [[InlineKeyboardButton("<< back", callback_data=recipe_name)]]
query.edit_message_text("Please send Mama a picture of the recipe.")
query.edit_message_reply_markup(InlineKeyboardMarkup(keyboard))
return EDIT_PHOTO
def remove_photo(update: Update, _: CallbackContext) -> int:
"""Removes the recipe photo."""
query = update.callback_query
query.answer()
user_id = _.user_data['user id']
db.update_username(user_id, query.from_user.username)
recipe_name = _.user_data['recipe name']
servings = db.get_servings(user_id, recipe_name)
blob_name = str(user_id) + "-" + recipe_name + ".jpg"
bucket.delete_blob(blob_name)
db.delete_picture_url(user_id, recipe_name)
buttons = ["recipe name", "add photo"]
buttons.append("add servings") if len(servings) == 0 else buttons.append("servings")
if not db.is_public(user_id, recipe_name):
keyboard = build_inline_keyboard([buttons, ["ingredients", "directions"], ["set public"], ["<< back to list of recipes"]])
else:
keyboard = build_inline_keyboard([buttons, ["ingredients", "directions"], ["set private"], ["<< back to list of recipes"]])
query.edit_message_text(
"The picture of your recipe has been removed."
"\nWhat else would you like to edit?"
)
query.edit_message_reply_markup(keyboard)
return RECIPE_PART
def change_photo(update: Update, _: CallbackContext) -> int:
"""Updates the picture of the recipe."""
new_photo = update.message.photo[-1].get_file()
user_id = update.message.from_user.id
db.update_username(user_id, update.message.from_user.username)
recipe_name = _.user_data['recipe name']
blob_name = str(user_id) + "-" + recipe_name + ".jpg"
if len(db.get_picture_url(user_id, recipe_name)) != 0:
bucket.delete_blob(blob_name) #deletes old picture
new_photo.download(blob_name)
blob = bucket.blob(blob_name)
blob.upload_from_filename(blob_name)
os.remove(blob_name)
blob.make_public()
db.add_picture_url(user_id, recipe_name, blob.public_url)
buttons = ["recipe name", "photo"]
servings = db.get_servings(update.message.from_user.id, _.user_data['recipe name'])
buttons.append("add servings") if len(servings) == 0 else buttons.append("servings")
if not db.is_public(user_id, recipe_name):
keyboard = build_inline_keyboard([buttons, ["ingredients", "directions"], ["set public"], ["<< back to list of recipes"]])
else:
keyboard = build_inline_keyboard([buttons, ["ingredients", "directions"], ["set private"], ["<< back to list of recipes"]])
update.message.reply_text(
"The picture of '" + recipe_name + "' has been updated successfully!\n"
"What else would you like to edit?",
reply_markup=keyboard
)
return RECIPE_PART
def edit_servings(update: Update, _: CallbackContext) -> int:
"""Asks user to update the recipe yield."""
query = update.callback_query
query.answer()
user_id = _.user_data['user id']
db.update_username(user_id, query.from_user.username)
recipe_name = _.user_data['recipe name']
if query.data == "servings":
current_serving = db.get_servings(user_id, recipe_name)
keyboard = [[InlineKeyboardButton("remove yield", callback_data="remove yield")], [InlineKeyboardButton("<< back", callback_data=recipe_name)]]
query.edit_message_text(
"Your recipe currently serves " + current_serving[0] + ".\n"
"What is the new yield of your recipe?"
)
else:
keyboard = [[InlineKeyboardButton("<< back", callback_data=recipe_name)]]
query.edit_message_text(
"What is the yield of your recipe?"
)
query.edit_message_reply_markup(InlineKeyboardMarkup(keyboard))
return EDIT_SERVINGS
def remove_servings(update: Update, _: CallbackContext) -> int:
"""Removes the yield of the recipe."""
query = update.callback_query
query.answer()
db.update_username(query.from_user.id, query.from_user.username)
db.delete_servings(_.user_data['user id'], _.user_data['recipe name'])
photo_url = db.get_picture_url(_.user_data['user id'], _.user_data['recipe name'])
buttons = ["recipe name", "add servings"]
buttons.insert(1, "add photo") if len(photo_url) == 0 else buttons.insert(1, "photo")
if not db.is_public(_.user_data['user id'], _.user_data['recipe name']):
keyboard = build_inline_keyboard([buttons, ["ingredients", "directions"], ["set public"], ["<< back to list of recipes"]])
else:
keyboard = build_inline_keyboard([buttons, ["ingredients", "directions"], ["set private"], ["<< back to list of recipes"]])
query.edit_message_text(
"The yield of your recipe has been removed."
"\nWhat else would you like to edit?"
)
query.edit_message_reply_markup(keyboard)
return RECIPE_PART
def change_servings(update: Update, _: CallbackContext) -> int:
"""Updates the serving stored in the database."""
new_serving = update.message.text
user_id = _.user_data['user id']
db.update_username(user_id, update.message.from_user.username)
recipe_name = _.user_data['recipe name']
current_serving = db.get_servings(user_id, recipe_name)
photo_url = db.get_picture_url(user_id, recipe_name)
db.add_servings(user_id, recipe_name, new_serving)
buttons = ["recipe name", "servings"]
buttons.insert(1, "add photo") if len(photo_url) == 0 else buttons.insert(1, "photo")
if not db.is_public(user_id, recipe_name):
keyboard = build_inline_keyboard([buttons, ["ingredients", "directions"], ["set public"], ["<< back to list of recipes"]])
else:
keyboard = build_inline_keyboard([buttons, ["ingredients", "directions"], ["set private"], ["<< back to list of recipes"]])
if len(current_serving) == 0:
update.message.reply_text(
"Your recipe now serves " + new_serving + ".\n"
"What else would you like to edit?",
reply_markup=keyboard
)
else:
update.message.reply_text(
"Your recipe now serves " + new_serving + " instead of " + current_serving[0] + ".\n"
"What else would you like to edit?",
reply_markup=keyboard
)
return RECIPE_PART
def edit_ingredients(update: Update, _: CallbackContext) -> int:
"""Asks user how they would like to edit the list of ingredients."""
query = update.callback_query
query.answer()
user_id = _.user_data['user id']
db.update_username(user_id, query.from_user.username)
recipe_name = _.user_data['recipe name']
ingredient_list = get_ingredient_list(user_id, recipe_name)
keyboard = [[
InlineKeyboardButton("add an ingredient", callback_data="add"),
InlineKeyboardButton("edit an ingredient", callback_data="edit"),
InlineKeyboardButton("delete an ingredient", callback_data="delete")
], [
InlineKeyboardButton("<< back", callback_data=recipe_name)
]]
query.edit_message_text(
ingredient_list + "\n"
"What would you like to do with the list of ingredients?"
)
query.edit_message_reply_markup(InlineKeyboardMarkup(keyboard))
return EDIT_INGREDIENTS
def ingredients_list_operation(update: Update, _: CallbackContext) -> int:
"""Allows user to edit the ingredient list according to their choice (add, edit or delete)."""
query = update.callback_query
query.answer()
user_id = _.user_data['user id']
db.update_username(user_id, query.from_user.username)
recipe_name = _.user_data['recipe name']
if query.data == "add":
keyboard = build_inline_keyboard([["<< back"]])
query.edit_message_text("Please tell Mama what ingredient you would like to add.")
query.edit_message_reply_markup(keyboard)
return ADD_INGREDIENT
else:
ingredients = db.get_ingredients(user_id, recipe_name)
buttons = [[ingredient] for ingredient in ingredients]
buttons.append(["<< back"])
keyboard = build_inline_keyboard(buttons)
if query.data == "edit":
query.edit_message_text("Please select an ingredient to edit:")
query.edit_message_reply_markup(keyboard)
return UPDATE_INGREDIENT
else:
query.edit_message_text("Please select an ingredient to delete:")
query.edit_message_reply_markup(keyboard)
return DELETE_INGREDIENT
def add_ingredient(update: Update, _:CallbackContext) -> int:
"""Adds the given ingredient to the ingredient list."""
ingredient = update.message.text
user_id = _.user_data['user id']
db.update_username(user_id, update.message.from_user.username)
recipe_name = _.user_data['recipe name']
if ingredient in db.get_ingredients(user_id, recipe_name):
keyboard = build_inline_keyboard([["<< back"]])
update.message.reply_text("Ingredient already exists! Please enter a different ingredient.", reply_markup=keyboard)
return ADD_INGREDIENT
else:
keyboard = [[
InlineKeyboardButton("add an ingredient", callback_data="add"),
InlineKeyboardButton("edit an ingredient", callback_data="edit"),
InlineKeyboardButton("delete an ingredient", callback_data="delete")
], [
InlineKeyboardButton("<< back", callback_data=recipe_name)
]]
db.add_ingredient(user_id, recipe_name, ingredient)
update.message.reply_text(
"List of ingredients has been updated!\n\n" + get_ingredient_list(user_id, recipe_name) + "\nWhat else would you like to do?",
reply_markup=InlineKeyboardMarkup(keyboard)
)
return EDIT_INGREDIENTS
def update_ingredient(update: Update, _: CallbackContext) -> int:
"""Asks user what they would like to update the selected ingredient to."""
ingredient = update.callback_query
ingredient.answer()
db.update_username(ingredient.from_user.id, ingredient.from_user.username)
_.user_data['ingredient'] = ingredient.data
keyboard = InlineKeyboardMarkup([[InlineKeyboardButton("<< back", callback_data="edit")]])
ingredient.edit_message_text("What would you like to change " + ingredient.data + " to?")
ingredient.edit_message_reply_markup(keyboard)
return SAVE_INGREDIENT
def save_ingredient(update: Update, _: CallbackContext) -> int:
"""Updates the selected ingredient with the given input."""
new_ingredient = update.message.text
current_ingredient = _.user_data['ingredient']
user_id = _.user_data['user id']
db.update_username(user_id, update.message.from_user.username)
recipe_name = _.user_data['recipe name']
if new_ingredient in db.get_ingredients(user_id, recipe_name):
keyboard = InlineKeyboardMarkup([[InlineKeyboardButton("<< back", callback_data="edit")]])
update.message.reply_text(
"This ingredient already exists in the ingredient list. Please send a different one.",
reply_markup=keyboard
)
return SAVE_INGREDIENT
else:
db.update_ingredient(user_id, recipe_name, current_ingredient, new_ingredient)
ingredients = db.get_ingredients(user_id, recipe_name)
buttons = [[ingredient] for ingredient in ingredients]
buttons.append(["<< back"])
keyboard = build_inline_keyboard(buttons)
update.message.reply_text(
"List of ingredients has been updated!\n\n" + get_ingredient_list(user_id, recipe_name) + "\n"
"Select another ingredient to update, or press back to return to the previous menu.",
reply_markup=keyboard
)
return UPDATE_INGREDIENT
def delete_ingredient(update: Update, _: CallbackContext) -> int:
"""Deletes the selected ingredient from the database."""
ingredient = update.callback_query
ingredient.answer()
user_id = _.user_data['user id']
db.update_username(user_id, ingredient.from_user.username)
recipe_name = _.user_data['recipe name']
db.delete_ingredient(user_id, recipe_name, ingredient.data)
ingredients = db.get_ingredients(user_id, recipe_name)
buttons = [[ingredient] for ingredient in ingredients]
buttons.append(["<< back"])
keyboard = build_inline_keyboard(buttons)
ingredient.edit_message_text(
"List of ingredients has been updated!\n\n" + get_ingredient_list(user_id, recipe_name) + "\n"
"Select another ingredient to delete, or press back to return to the previous menu."
)
ingredient.edit_message_reply_markup(keyboard)
return DELETE_INGREDIENT
def edit_steps(update: Update, _: CallbackContext) -> int:
"""Asks user how they would like to edit the list of steps."""
query = update.callback_query
query.answer()
user_id = _.user_data['user id']
db.update_username(user_id, query.from_user.username)
recipe_name = _.user_data['recipe name']
step_list = get_step_list(user_id, recipe_name)
keyboard = [[
InlineKeyboardButton("add a step", callback_data="add"),
InlineKeyboardButton("edit a step", callback_data="edit"),
InlineKeyboardButton("delete a step", callback_data="delete")
], [
InlineKeyboardButton("<< back", callback_data=recipe_name)
]]
query.edit_message_text(
step_list + "\n"
"What would you like to do with the directions?"
)
query.edit_message_reply_markup(InlineKeyboardMarkup(keyboard))
return EDIT_STEPS
def steps_list_operation(update: Update, _: CallbackContext) -> int:
"""Allows user to edit the steps list according to their choice (add, edit, delete)."""
query = update.callback_query
query.answer()
user_id = _.user_data['user id']
db.update_username(user_id, query.from_user.username)
recipe_name = _.user_data['recipe name']
if query.data == "add":
steps = db.get_steps(user_id, recipe_name)
keyboard = ([[InlineKeyboardButton("<< back", callback_data="<< back")]])
query.edit_message_text("Please tell Mama what's the next step to the recipe.")
query.edit_message_reply_markup(InlineKeyboardMarkup(keyboard))
return ADD_STEP
else:
steps = db.get_steps(user_id, recipe_name)
x = 1
buttons = []
for step in steps:
buttons.append([InlineKeyboardButton(str(x), callback_data=step)])
x+=1
buttons.append([InlineKeyboardButton("<< back", callback_data="<< back")])
if query.data == "edit":
query.edit_message_text(get_step_list(user_id, recipe_name) + "\nPlease select a step to edit:")
query.edit_message_reply_markup(InlineKeyboardMarkup(buttons))
return UPDATE_STEP
else:
query.edit_message_text(get_step_list(user_id, recipe_name) + "\nPlease select a step to delete:")
query.edit_message_reply_markup(InlineKeyboardMarkup(buttons))
return DELETE_STEP
def add_step(update: Update, _:CallbackContext) -> int: # only allows user to append to the end of the directions
"""Adds the given step to the end of the list of steps."""
step = update.message.text
user_id = _.user_data['user id']
db.update_username(user_id, update.message.from_user.username)
recipe_name = _.user_data['recipe name']
keyboard = [[
InlineKeyboardButton("add a step", callback_data="add"),
InlineKeyboardButton("edit a step", callback_data="edit"),
InlineKeyboardButton("delete a step", callback_data="delete")
], [
InlineKeyboardButton("<< back", callback_data=recipe_name)
]]
db.add_step(user_id, recipe_name, step)
update.message.reply_text(
"The directions have been updated!\n\n" + get_step_list(user_id, recipe_name) + "\nWhat else would you like to do?",
reply_markup=InlineKeyboardMarkup(keyboard)
)
return EDIT_STEPS
def update_step(update: Update, _: CallbackContext) -> int:
"""Asks user what they would like to update the selected step to."""
step = update.callback_query
step.answer()
db.update_username(step.from_user.id, step.from_user.username)
_.user_data['step'] = step.data
keyboard = InlineKeyboardMarkup([[InlineKeyboardButton("<< back", callback_data="edit")]])
step.edit_message_text("What would you like to change the step '" + step.data + "' to?")
step.edit_message_reply_markup(keyboard)
return SAVE_STEP
def save_step(update: Update, _: CallbackContext) -> int:
"""Updates the selected step with the given input."""
new_step = update.message.text
current_step = _.user_data['step']
user_id = _.user_data['user id']
db.update_username(user_id, update.message.from_user.username)
recipe_name = _.user_data['recipe name']
db.update_step(user_id, recipe_name, current_step, new_step)
steps = db.get_steps(user_id, recipe_name)
x = 1
buttons = []
for step in steps:
buttons.append([InlineKeyboardButton(str(x), callback_data=step)])
x+=1
buttons.append([InlineKeyboardButton("<< back", callback_data="<< back")])
update.message.reply_text(
"The directions have been updated!\n\n" + get_step_list(user_id, recipe_name) + "\n"
"Select another step to update, or press back to return to the previous menu.",
reply_markup=InlineKeyboardMarkup(buttons)
)
return UPDATE_STEP
def delete_step(update: Update, _: CallbackContext) -> int:
"""Deletes the selected step from the database."""
query = update.callback_query
query.answer()
user_id = _.user_data['user id']
db.update_username(user_id, query.from_user.username)
recipe_name = _.user_data['recipe name']
db.delete_step(user_id, recipe_name, query.data)
steps = db.get_steps(user_id, recipe_name)
x = 1
buttons = []
for step in steps:
buttons.append([InlineKeyboardButton(str(x), callback_data=step)])
x+=1
buttons.append([InlineKeyboardButton("<< back", callback_data="<< back")])
query.edit_message_text(
"The directions have been updated!\n\n" + get_step_list(user_id, recipe_name) + "\n"
"Select another step to delete, or press back to return to the previous menu."
)
query.edit_message_reply_markup(InlineKeyboardMarkup(buttons))
return DELETE_STEP
def edit_timeout(update: Update, _: CallbackContext) -> int:
"""Exits the /edit conversation when no change has been made for 5 minutes."""
update.message.reply_text("Editing has been cancelled.")
return ConversationHandler.END
def exit(update: Update, _: CallbackContext) -> int:
"""Exits current conversation handler when a new valid command is sent."""
return ConversationHandler.END
def delete_recipe(update: Update, _: CallbackContext) -> int:
"""Asks user for a recipe to delete."""
user_id = update.message.from_user.id
db.update_username(user_id, update.message.from_user.username)
_.user_data['user id'] = user_id
recipes = db.get_recipes(user_id)
if len(recipes) == 0:
update.message.reply_text("Your recipe book is already empty.")
return ConversationHandler.END
else:
keyboard = build_inline_keyboard([[recipe] for recipe in recipes])
update.message.reply_text("Please select a recipe to delete.", reply_markup=keyboard)
return CONFIRMATION
def confirmation(update: Update, _: CallbackContext) -> int:
"""Makes sure user deletes the correct recipe."""
recipe_name = update.callback_query
recipe_name.answer()
db.update_username(recipe_name.from_user.id, recipe_name.from_user.username)
_.user_data["recipe name"] = recipe_name.data
keyboard = build_inline_keyboard([["yes", "no"]])
recipe_name.edit_message_text(
"Are you sure you want to delete '" + recipe_name.data + "'? Mama won't be able to recover any deleted recipes!"
)
recipe_name.edit_message_reply_markup(keyboard)
return DELETION
def deletion(update: Update, _: CallbackContext) -> None:
"""Deletes recipe from the database."""
answer = update.callback_query
answer.answer()
db.update_username(answer.from_user.id, answer.from_user.username)
if answer.data == "yes":
user_id = _.user_data['user id']
recipe_name = _.user_data["recipe name"]
if len(db.get_picture_url(user_id, recipe_name)) != 0:
blob_name = str(user_id) + "-" + recipe_name + ".jpg"
bucket.delete_blob(blob_name)
db.delete_recipe(user_id, recipe_name)
_.user_data.clear()
answer.edit_message_text(recipe_name + " has been deleted from your recipes.")
else:
_.user_data.clear()
answer.edit_message_text("Seems like you've changed your mind!")
def search_recipes(update: Update, _: CallbackContext) -> None:
"""Returns the related recipes from the given keywords."""
input = update.message.text[8:]
update.message.reply_text("searching for term...")
def search_user(update: Update, _: CallbackContext) -> int:
username = update.message.text[9:]
user_id = db.get_user_id(username)
db.update_username(update.message.from_user.id, update.message.from_user.username)
if len(user_id) == 0:
update.message.reply_text(
"Sorry, it seems like Mama doesn't know this person!\n"
"If their username was changed recently, you may check back again in a day as Mama's database may not have been updated yet."
)
return ConversationHandler.END
else:
_.user_data['user id'] = user_id[0]
recipes = db.get_public_recipes(user_id[0])
if len(recipes) == 0:
update.message.reply_text(
"Hmm, it seems like this user has not left any recipes with Mama or they do not have any public recipes!"
)
return ConversationHandler.END
else:
keyboard = build_keyboard(recipes)
update.message.reply_text(
"Which recipe from @" + username + " would you like to view?",
reply_markup=keyboard)
return SEND_RECIPE
def update_usernames(_: CallbackContext) -> None:
chat_ids = db.get_all_chat_id()
for id in chat_ids:
username = Bot.get_chat(id).username
if username != None:
db.update_username(id, username)
def main() -> None:
# Create the Updater and pass it your bot's token.
updater = Updater(API_KEY)
job = updater.job_queue
# Get the dispatcher to register handlers
dispatcher = updater.dispatcher
job.run_daily(update_usernames, datetime.time(hour=12, tzinfo=pytz.timezone('Asia/Singapore')))
# Create the Conversation Handler
add_recipe_conv_handler = ConversationHandler(
entry_points=[CommandHandler("add", add_recipe)],
states={
NAME: [MessageHandler(Filters.text & (~ Filters.command), name)],
PHOTO: [MessageHandler(Filters.photo & (~ Filters.command), photo), CommandHandler("skip", skip_photo)],
SERVINGS: [MessageHandler(Filters.text & (~ Filters.command), servings), CommandHandler("skip", skip_servings)],
INGREDIENTS: [MessageHandler(Filters.text & (~ Filters.command) | Filters.regex('/done'), ingredients)],
STEPS: [MessageHandler(Filters.text & (~ Filters.command) | Filters.regex('/done'), steps)]
},
fallbacks=[CommandHandler("cancel", cancel_add)]
)
view_recipe_conv_handler = ConversationHandler(
entry_points=[CommandHandler("view", view_recipe), CommandHandler("search", search_user)],
states = {
SEND_RECIPE: [MessageHandler(Filters.text & (~ Filters.command), send_recipe)]
},
fallbacks=[MessageHandler(Filters.command, exit)],
allow_reentry=True
)
edit_recipe_conv_handler = ConversationHandler(
entry_points=[CommandHandler("edit", edit_recipe)],
states = {
RECIPE_CHOICE: [CallbackQueryHandler(recipe_choice)],
RECIPE_PART: [
CallbackQueryHandler(edit_name, pattern="^recipe name$"),
CallbackQueryHandler(edit_photo, pattern="^photo|add photo$"),
CallbackQueryHandler(edit_servings, pattern="^servings|add servings$"),
CallbackQueryHandler(edit_ingredients, pattern="^ingredients$"),
CallbackQueryHandler(edit_steps, pattern="^directions$"),
CallbackQueryHandler(toggle_privacy, pattern="^set public|set private$"),
CallbackQueryHandler(edit_recipe_inline, pattern="^<< back to list of recipes$")
],
EDIT_NAME: [
CallbackQueryHandler(recipe_choice), MessageHandler(Filters.text, change_name)
],
EDIT_PHOTO: [
CallbackQueryHandler(remove_photo, pattern="^remove photo$"),
CallbackQueryHandler(recipe_choice),
MessageHandler(Filters.photo, change_photo)
],
EDIT_SERVINGS: [
CallbackQueryHandler(remove_servings, pattern="^remove yield$"),
CallbackQueryHandler(recipe_choice),
MessageHandler(Filters.text, change_servings)
],
EDIT_INGREDIENTS: [
CallbackQueryHandler(ingredients_list_operation, pattern="^add|edit|delete$"),
CallbackQueryHandler(recipe_choice)
],
ADD_INGREDIENT: [CallbackQueryHandler(edit_ingredients), MessageHandler(Filters.text, add_ingredient)],
UPDATE_INGREDIENT: [
CallbackQueryHandler(edit_ingredients, pattern="^<< back$"),
CallbackQueryHandler(update_ingredient)
],
SAVE_INGREDIENT: [CallbackQueryHandler(ingredients_list_operation), MessageHandler(Filters.text, save_ingredient)],
DELETE_INGREDIENT: [
CallbackQueryHandler(edit_ingredients, pattern="^<< back$"),
CallbackQueryHandler(delete_ingredient)
],
EDIT_STEPS: [
CallbackQueryHandler(steps_list_operation, pattern="^add|edit|delete$"),
CallbackQueryHandler(recipe_choice)
],
ADD_STEP: [CallbackQueryHandler(edit_steps), MessageHandler(Filters.text, add_step)],
UPDATE_STEP: [
CallbackQueryHandler(edit_steps, pattern="^<< back$"),
CallbackQueryHandler(update_step)
],
SAVE_STEP: [CallbackQueryHandler(steps_list_operation), MessageHandler(Filters.text, save_step)],
DELETE_STEP: [
CallbackQueryHandler(edit_steps, pattern="^<< back$"),
CallbackQueryHandler(delete_step)