-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot_actions.py
1058 lines (897 loc) · 42.9 KB
/
bot_actions.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
from telegram.ext import ConversationHandler, MessageHandler, CommandHandler, Filters, CallbackContext, Updater
from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove, Update
from datetime import datetime
from telegram import Location
from user import User
from campaign import Campaign
from typing import Dict
from Offer import Offer
import Utils
import telegram.ext
import utilities
import telegram
import logging
import matcher
##############################################################################################################################################################################
# Start command
##############################################################################################################################################################################
# This is the Start command (/start).
# In the Start Command, the bot will ask the user about his basic information which include:
# firstname, lastname, birthdate, phonenumber, address, location
# these info will be stored in a Class User and then they will be stored in the database
# Class User
u1 = User()
c1 = Campaign()
offer = Offer()
request = []
# These are the states of the Start command
CHOOSING, PHONE_NUMBER, PHONE_NUMBER_YN, BIRTHDATE, BIRTHDATE_YN, ADDRESS, ADDRESS_YN, LOCATION, CONCLUDE , MENU, WELCOME, ACTIONS, \
OFFER_TYPE, OFFER_DESCRIPTION, OFFER_QUANTITY, OFFER_done, REQUEST_TYPE, REQUEST_DESCRIPTION, REQUEST_QUANTITY, REQCASH_ESTIMATE, \
REQUEST_NOTED, NEW_VOLUNTEER, CHOOSE_VALUES_TO_UPDATE, U_ADDRESS, U_LOCATION, START_DELIVERY, DELIVERY_SUCCESS, DELIVERY_FAILURE, CHOOSE_DONATION, \
CHOOSE_OFFER, SAVE_OFFER, ASK_OFFER_ID, DELIVER_NEEDS, UPDATE_NEED, ASK_NEED_ID,NEW_DONATION_TYPE, ESTIMATING_NEW_DONATION_VALUE, NEW_REQUEST_TYPE, \
ESTIMATING_NEW_NEED_VALUE, GO_MENU, CHOOSE_CONFIRM, RECEIVED_NEED, NOT_RECEIVED_NEED, START_CAMP, CAMP_NAME, CAMP_D, CAMP_L, CONCLUDE_CAMP = \
range(48)
def actions(update: Update, context: CallbackContext) -> int:
reply_keyboard = [['Donate'],['Volunteer'],['Request'],
['Nothing'],['Show Offers'],['Updates on pickups'],['Show Needs'],['Update on Delivered Needs'],['Create Campaign']]
update.message.reply_text(
"Please choose what you want to do.",
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return ACTIONS
def start(update: Update, context: CallbackContext) -> int:
reply_keyboard = [['Yes! All good', 'Cancel']]
user = update.message.from_user
u1.FirstName = user.first_name
u1.LastName = user.last_name
u1.ChatID = update.message.chat.id
u1.UserID = user.id
u1.CreationDate = datetime.now()
# in case user is already registered , do not take him through the registration steps
if Utils.is_registered(user.id, user.first_name) :
return Welcome_back(update, context)
#usual registration process taken
else :
update.message.reply_text(
"Hi! My name is Matcher Bot. I will help you during your crisis.\n"
"First let's see if the information I know about you are correct.\n"
"First Name: " f'{user.first_name}''\n'
"Last Name: " f'{user.last_name}''\n'
"Username: " f'{user.username}''\n'
"If they are not true please press Cancel and change them in the settings of the telegram app",
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return CHOOSING
# function that asks the user about his phone number
def phone_number(update: Update, context: CallbackContext) -> int:
reply_keyboard = [['Cancel']]
update.message.reply_text(
'All right, please send me your phone number',
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return PHONE_NUMBER
# function that stores the phone number of the user and sends a conformation message
def phone_number_text(update: Update, context: CallbackContext) -> int:
reply_keyboard = [['Yes', 'No'],['Cancel']]
text = update.message.text
u1.PhoneNumber = text
update.message.reply_text(f'Your number is: {text.lower()}? Please make sure',
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True), )
return PHONE_NUMBER_YN
# function that asks the user about his birthdate
def birthdate(update: Update, context: CallbackContext) -> int:
reply_keyboard = [['Cancel']]
update.message.reply_text(
'Please give me your birthdate in DD/MM/YY format.',
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return BIRTHDATE
# function that stores the birthdate of the user and sends a conformation message
def birthdate_text(update: Update, context: CallbackContext) -> int:
reply_keyboard = [['Yes', 'No'],['Cancel']]
text = update.message.text
u1.Birthdate = text
update.message.reply_text(f'Your birthdate is: {text.lower()}? Please make sure',
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True), )
return BIRTHDATE_YN
# function that asks the user about his address
def address(update: Update, context: CallbackContext) -> int:
reply_keyboard = [['Cancel']]
update.message.reply_text(
'Alright, please send me your address',
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return ADDRESS
# function that stores the address of the user and sends a conformation message
def address_text(update: Update, context: CallbackContext) -> int:
reply_keyboard = [['Yes', 'No','Cancel']]
text = update.message.text
u1.AddressDescription = text
update.message.reply_text(f'Your address is: {text.lower()}? Please make sure',
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True), )
return ADDRESS_YN
# function that asks the user about his location
def ask_location(update: Update, context: CallbackContext) -> int:
reply_keyboard = [['Cancel']]
update.message.reply_text(
'Now, send me your location please',
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
print("asked for location")
return LOCATION
# function that stores the users location in the user class and sends him a thankyou message
def location(update: Update, context: CallbackContext) -> int:
reply_keyboard = [['Sure!', 'Cancel']]
user = update.message.from_user
user_location = update.message.location
u1.AddressLatitude = user_location.latitude
u1.AddressLongitude = user_location.longitude
update.message.reply_text(
'Thank you! your information will now be saved in the database to help with your needs. Press Cancel to delete everything',
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return CONCLUDE
# Function that sends a reply to the user summarizing the information sent from the user
def received_information(update: Update, context: CallbackContext) -> int:
# add_to_DB(u1) this function adds the user to the database after the user gave us all the information about him
# This fuction is still being constructed
# When finished it will be imported and used in the bot
reply_keyboard = [ ['Ok, Sounds Good!'], ['Later'], ['Cancel']]
update.message.reply_text(
"Thank you for signing up! These are the information you gave us about you: \n"
"First Name: "f'{u1.FirstName}''\n'
"Last Name: "f'{u1.LastName}''\n'
"Birthdate: "f'{u1.Birthdate}''\n'
"Address: "f'{u1.AddressDescription}''\n'
"Phone Number: "f'{u1.PhoneNumber}''\n'
"Address Latitude: "f'{u1.AddressLatitude}''\n'
"Address Longitude: "f'{u1.AddressLongitude}''\n''\n'
"You will now be directed to the main menu to choose what you want to do."'\n'
"You can press 'Later' if you wish to do this later.",
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True)
)
now = datetime.now()
Utils.create_new_user(u1.UserID, u1.FirstName, u1.LastName, u1.Birthdate, u1.PhoneNumber,u1.AddressLatitude,u1.AddressLongitude,u1.AddressDescription, u1.ChatID)
return MENU
# if the user is already in the Database, we will raise an error and inform the user about it
# this will be implemented after we connect the bot to the database
def Welcome_back(update: Update, context: CallbackContext) -> int:
reply_keyboard = [['Menu'], ['Exit']]
update.message.reply_text(
'Nice to see you again, feel free to choose what do you want to do',
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return WELCOME
# function cancel that cancels the process and deletes the information about the user
def cancel(update: Update, context: CallbackContext) -> int:
u1.reset()
update.message.reply_text(
"Until next time!"
)
return ConversationHandler.END
def done(update: Update, context: CallbackContext) -> int:
update.message.reply_text(
"Until next time! stay safe..."'\n'
"Remember, if you want to call me just type '/start' I'll be happy to help!"
)
return ConversationHandler.END
###########################################################################################################################################################
# UPDATE COMMAND
###########################################################################################################################################################
# this command will allow the user to update the information about themselves .
# the user can update the information about himself only if he completed the start phase
def update(update: Update, context: CallbackContext) -> int:
user = update.message.from_user
if not Utils.is_registered(user.id, user.first_name) :
update.message.reply_text(
"You are not registered. Please type '/start' to register yourself"
)
return ConversationHandler.END
else:
reply_keyboard = [['Location', 'Address', 'Nothing']]
update.message.reply_text(
"What info would you like to update? Please choose one:",
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return CHOOSE_VALUES_TO_UPDATE
def update_address_bot(update: Update, context: CallbackContext) -> int:
reply_keyboard=[['Cancel']]
update.message.reply_text(
"Please write your new address.",
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return U_ADDRESS
def update_address_bot_text(update: Update, context: CallbackContext) -> int:
text = update.message.text
user = update.message.from_user
userid = user.id
Utils.update_address(userid, text)
update.message.reply_text(
"Thank you. Your address have been updated to: "f'{text}'
)
return ConversationHandler.END
def update_location_bot(update: Update, context: CallbackContext) -> int:
reply_keyboard=[['Cancel']]
update.message.reply_text(
"Please send me your new location",
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return U_LOCATION
def update_location_bot_text(update: Update, context: CallbackContext) -> int:
user = update.message.from_user
userid = user.id
user_location = update.message.location
lon = str(user_location.longitude)
lat = str(user_location.latitude)
Utils.update_location(userid , lon, lat)
update.message.reply_text(
"Thank you. Your location have been updated to: "f'{lat,lon}'
)
return ConversationHandler.END
def nothing_to_update(Update: Update, context: CallbackContext) -> int:
Update.message.reply_text(
"Feel free to update your info anytime!"
)
return ConversationHandler.END
###########################################################################################################################################################
# RECORDING OFFERs
###########################################################################################################################################################
def donation_type(update: Update, context: CallbackContext) -> int:
# A.Y: 3-1-2020
reply_keyboard = Utils.get_all_types_as_list()
update.message.reply_text(
"God bless you, please choose what type of donation you are willing to offer",
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return OFFER_DESCRIPTION
def donation_description(update: Update, context: CallbackContext)-> int:
reply_keyboard = [['Cancel']]
type1 = update.message.text
print(type1)
offer.type = type1
offer.userID = u1.UserID
# A.Y: 3-1-2020, Changed the logic
if offer.type == 'other':
update.message.reply_text(
'Please provide the name of the donation type and description',
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return NEW_DONATION_TYPE
else:
update.message.reply_text(
'Great! please provide a description of your donation',
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return OFFER_QUANTITY
def new_donation_type(update: Update, context: CallbackContext)-> int:
reply_keyboard=[['Cancel']]
new_donation_name = update.message.text
offer.type = new_donation_name
#print("New offer type is: " + str(offer.type))
# This case happens when the user chooses 'other' and then enter a type that already rxists.
# Example: medical. In this case, we should not create a new type in the database.
if Utils.check_if_type_exists(offer.type):
update.message.reply_text(
'This donation type already exists. Please provide a description of your donation',
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return OFFER_QUANTITY
else:
update.message.reply_text(
'Thanks for donating a new type! Can you first estimate the cash value of this object in thousands LBP ?',
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return ESTIMATING_NEW_DONATION_VALUE
def estimating_new_donation_value(update: Update, context: CallbackContext)-> int:
reply_keyboard=[['Cancel']]
cash_value = update.message.text
try:
estimated_value = int(cash_value)
except:
update.message.reply_text(
'This value is invalid. Please type a number.',
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return ESTIMATING_NEW_DONATION_VALUE
offer.cash_value = estimated_value
Utils.create_new_donationtype(offer.type, estimated_value)
update.message.reply_text(
'Thanks for choosing to donate a new item! Now provide a description for your donation.',
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return OFFER_QUANTITY
def donation_quantity(update: Update, context: CallbackContext)-> int:
desc = update.message.text
offer.description = desc
print(desc)
reply_keyboard = [['1', '5', '15', '20', '50','Cancel']]
update.message.reply_text(
'Thanks, can you specify the quantity/amount(thousands LBP) you will be providing in integers, ex: 5 (unit)',
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True)
)
return OFFER_done
def offer_registered( update: Update, context: CallbackContext)-> int:
qt = update.message.text
offer.QuantityAmount = qt
Utils.add_offer(offer.type, offer.userID, offer.description, offer.QuantityAmount)
update.message.reply_text(
'Hope is never lost with a community such as yours. Thank you for your offer.'
)
return ConversationHandler.END
###########################################################################################################################################################
# RECORDING NEED/REQUEST
###########################################################################################################################################################
def request_type ( update: Update, context: CallbackContext)-> int:
user = update.message.from_user
userid = user.id
request.append(userid)
request.append(userid)
#reply_keyboard = [['Cancel']]
reply_keyboard = Utils.get_all_types_as_list_for_request()
update.message.reply_text(
'We will assist you if god wills, please choose what type of need you want to request...',
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True)
)
return REQUEST_TYPE
def request_description( update: Update, context: CallbackContext)-> int:
reply_keyboard=[['Cancel']]
request_type = update.message.text
request.append(request_type)
if request_type == 'other':
update.message.reply_text(
'please provide us with a name for your need...',
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return NEW_REQUEST_TYPE
update.message.reply_text(
'please provide us with a description of your need...',
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return REQUEST_DESCRIPTION
def new_request_type(update: Update, context: CallbackContext)-> int:
reply_keyboard=[['Cancel']]
new_donation_name = update.message.text
offer.type = new_donation_name
print("New request type is: " + str(offer.type))
# This case happens when the user chooses 'other' and then enter a type that already rxists.
# Example: medical. In this case, we should not create a new type in the database.
if Utils.check_if_type_exists(offer.type):
update.message.reply_text(
'This type already exists. Please provide a description of your need',
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return REQUEST_QUANTITY
else:
update.message.reply_text(
'Your need has been noted. Can you please provide as estimate of the value of your need in thousands LBP?',
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return ESTIMATING_NEW_NEED_VALUE
def estimating_new_need_value(update: Update, context: CallbackContext)-> int:
reply_keyboard=[['Cancel']]
cash_value = update.message.text
try:
estimated_value = int(cash_value)
except:
update.message.reply_text(
'This value is invalid. Please type a number.',
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return ESTIMATING_NEW_NEED_VALUE
offer.cash_value = estimated_value
Utils.create_new_donationtype(offer.type, estimated_value)
update.message.reply_text(
'We know now the value of your need. Now provide a description for your need.',
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return REQUEST_DESCRIPTION
def request_quantity ( update: Update, context: CallbackContext)-> int:
print(request)
description = update.message.text
request.append(description)
reply_keyboard = [['1','10','20','100'],['NA'],['Cancel']]
update.message.reply_text(
'Please provide the quantity or amount needed if your need is quantifiable...',
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True)
)
return REQUEST_QUANTITY
# this stage will be used to estimate cash amount needed for non-monetary needs, exp: medicine,laptops,books...
##################################~~~~~~~~~~~~~~~~~~~~~~~~~~#############################################################
def ReqCash_estimate(update: Update, context: CallbackContext) -> int:
reply_keyboard = [['50', '100','200' ,'500','1000'],[' > 1000'],['Cancel']]
update.message.reply_text(
'can you estimate the amount of cash in thousands LBP necessary to fulfill your need...',
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True)
)
return REQUEST_QUANTITY
##################################~~~~~~~~~~~~~~~~~~~~~~~~~~#############################################################
def request_noted(update: Update, context: CallbackContext)-> int:
quantity = update.message.text
request.append(quantity)
print(request)
reply_keyboard = [['Menu'], ['Exit']]
update.message.reply_text(
'Your request has been well received. Thank You.',
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True)
)
user_id = request[0]
donation_type = request[2]
request_description = request[3]
cash_value = 0
quantity = int(request[4])
Utils.add_need(user_id, donation_type, request_description, cash_value, quantity)
return GO_MENU
###########################################################################################################################################################
# REGISTERING A VOLUNTEER
###########################################################################################################################################################
def new_volunteer( update: Update, context: CallbackContext ) -> int :
user = update.message.from_user
user_id = user.id
if Utils.user_is_volunteer(user_id):
update.message.reply_text(
"You are already a volunteer."
)
return ConversationHandler.END
else:
reply_keyboard = [['Yes', 'No','Cancel']]
update.message.reply_text(
"Are you sure you want to be registered as a volunteer ?",
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True)
)
return NEW_VOLUNTEER
def added_volunteer(update: Update, context: CallbackContext) -> int :
reply_keyboard = [['Menu'],['Exit']]
update.message.reply_text(
"God bless you, you have been added to the list of volunteers. If you want to start delivering items" \
"to people in need, you can press 'Show Offers' in menu to see the offers available for pickup, or press 'Show Needs' in menu to see the needs that should be delivered.",
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True)
)
Utils.add_volunteer(u1.UserID, u1.FirstName, u1.LastName, u1.ChatID)
return GO_MENU
###########################################################################################################################################################
# DELIVERING ITEMS (We will not use this.) A.Y
###########################################################################################################################################################
def deliver(update: Update, context: CallbackContext) -> int:
reply_keyboard = [['I want to deliver items', 'Succeeded in delivering an item', 'Failed in delivering an item'],
['Register as Volunteer'],['Cancel']]
user = update.message.from_user
if Utils.user_is_volunteer(user.id):
update.message.reply_text(
"Welcome back, volunteer! What did you come here for?",
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True)
)
return START_DELIVERY
else:
reply_keyboard1= [['Register as Volunteer']]
update.message.reply_text(
"You cannot deliver any items unless you become a volunteer. If you wish so, Please run the /start " \
"command and choose the 'Register as Volunteer' action in order for you to register.",
reply_markup = ReplyKeyboardMarkup(reply_keyboard1, one_time_keyboard=True)
)
return ACTIONS
def start_delivering(update: Update, context: CallbackContext) -> int:
reply_keyboard=['Cancel']
undelivered_items = Utils.get_all_undelivered_items()
reply = "Below are all the matched items that you can deliver. Choose the Donation ID that mostly suits you:\n"
for undelivered_item in undelivered_items:
reply = reply + utilities.create_text_for_donation(undelivered_item)
update.message.reply_text(reply,reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),)
return CHOOSE_DONATION
def volunteer_chose_donation(update: Update, context: CallbackContext) -> int:
reply_keyboard=[['Cancel']]
donation_id = update.message.text
Utils.set_donation_as_being_delivered(donation_id)
update.message.reply_text(
"Thanks for choosing the Donation " + donation_id + " to deliver. God bless you!",
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return ACTIONS
def delivery_success_id(update: Update, context: CallbackContext) -> int:
reply_keyboard=[['Cancel']]
update.message.reply_text(
"Please enter the delivery_id we provided you to mark it in our DB as successful",
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return DELIVERY_SUCCESS
def delivery_failure_id(update: Update, context: CallbackContext) -> int:
reply_keyboard=[['Cancel']]
update.message.reply_text(
"Please enter the delivery_id we provided you to mark it in our DB as failure. But don't worry, there are "\
"Many good people like you who are willing to help, and they will deliver the items!",
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return DELIVERY_FAILURE
def mark_delivery_as_success(update: Update, context: CallbackContext) -> int:
reply_keyboard=[['Cancel']]
delivery_id = update.message.text
Utils.mark_delivery_as_success(int(delivery_id))
update.message.reply_text(
"Thanks for helping people.. God bless you!",
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return START_DELIVERY
def mark_delivery_as_failure(update: Update, context: CallbackContext) -> int:
reply_keyboard=[['Cancel']]
delivery_id = update.message.text
Utils.mark_delivery_as_failure(int(delivery_id))
update.message.reply_text(
"Thanks for trying. We will reset this delivery so that someone else may do it. Thanks for your time!",
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return START_DELIVERY
###########################################################################################################################################################
# PICKUP
###########################################################################################################################################################
def pickup_offers(update: Update, context: CallbackContext) -> int:
#reply_keyboard1=[['Cancel']]
a = utilities.get_UnpickedUpOffers()
user = update.message.from_user
user_id = user.id
if not Utils.user_is_volunteer(user_id):
update.message.reply_text(
"These are the offers that are available:"
)
update.message.reply_text(
a
)
return ConversationHandler.END
else:
reply_keyboard = [['Yes', 'Cancel']]
update.message.reply_text(
"These are the offers that are available for pickup:"
)
update.message.reply_text(
a
)
update.message.reply_text(
"Do you want to pickup one of the offers?",
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return CHOOSE_OFFER
def choose_offer(update: Update, context: CallbackContext) -> int:
reply_keyboard=[['Cancel']]
update.message.reply_text(
"Please type the offer ID that you want to pickup.",
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return SAVE_OFFER
def save_offer(update: Update, context: CallbackContext) -> int:
user = update.message.from_user
user_id = user.id
text = update.message.text
offerid = int(text)
chatid = Utils.get_donorid_from_offer(offerid)
pn = Utils.get_phonenumber(user_id)
context.bot.send_message(chat_id= chatid,text = "A Volunteer will pickup your offer.")
context.bot.send_message(chat_id= chatid,text = "The Volunteer's information are:")
msg1 = "First name: "+str(user.first_name)
context.bot.send_message(chat_id= chatid,text = msg1)
msg2 = "Last name: "+str(user.last_name)
context.bot.send_message(chat_id= chatid,text = msg2)
msg3 = "Phone number: "+str(pn)
context.bot.send_message(chat_id= chatid,text = msg3)
utilities.update_offer_assigned(offerid, user_id)
donor_details= utilities.details_of_user(chatid)
update.message.reply_text(
"Thankyou! The Donor of the offer have been informed. You should also contact the donor to take the offer from him."'\n'
"These are the donor\'s details: "'\n'
)
update.message.reply_text(
donor_details
)
return ConversationHandler.END
###########################################################################################################################################################
# UPDATE_PICKUP
###########################################################################################################################################################
def ask_for_offerid(update: Update, context: CallbackContext) -> int:
user = update.message.from_user
user_id = user.id
if not Utils.user_is_volunteer(user_id):
reply_keyboard = [['Menu'],['Exit']]
update.message.reply_text(
"You are not registered as a volunteer.",
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return GO_MENU
else:
a = utilities.show_assignedOffers( user_id)
if len(a) > 0:
reply_keyboard=[['Cancel']]
update.message.reply_text(
"If you have picked up an offer, please provide me with it\'s offer ID"
)
update.message.reply_text(
"These are your assigned offers: "
)
update.message.reply_text(
a,
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return ASK_OFFER_ID
else:
update.message.reply_text(
"You do not have an assigned offer."
)
return ConversationHandler.END
def update_pickup(update: Update, context: CallbackContext) -> int:
user = update.message.from_user
user_id = user.id
text = update.message.text
offerid = int(text)
utilities.set_offer_pickedup(offerid, user_id)
update.message.reply_text(
"Thank you! We are now up-to-date with you."
)
return ConversationHandler.END
###########################################################################################################################################################
# NEEDS
###########################################################################################################################################################
def show_needs(update: Update, context: CallbackContext) -> int:
#reply_keyboard1=[['Cancel']]
a = utilities.get_UndeliveredNeeds()
user = update.message.from_user
user_id = user.id
if not Utils.user_is_volunteer(user_id):
update.message.reply_text(
"These are the needs posted:"
)
update.message.reply_text(
a
)
return ConversationHandler.END
else:
reply_keyboard = [['Yes', 'Cancel']]
update.message.reply_text(
"These are the undelivered needs that you can deliver:"
)
update.message.reply_text(
a
)
update.message.reply_text(
"Do you want to deliver one of the needs?",
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return DELIVER_NEEDS
def choose_need(update: Update, context: CallbackContext) -> int:
reply_keyboard=[['Cancel']]
update.message.reply_text(
"Please type the need ID that you want to deliver",
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return UPDATE_NEED
def update_need(update: Update, context: CallbackContext) -> int:
user = update.message.from_user
user_id = user.id
text = update.message.text
needid = int(text)
chatid = Utils.get_victimid_from_need(needid)
pn = Utils.get_phonenumber(user_id)
context.bot.send_message(chat_id= chatid,text = "A Volunteer will deliver to you your need.")
context.bot.send_message(chat_id= chatid,text = "The Volunteer's information are:")
msg1 = "First name: "+str(user.first_name)
context.bot.send_message(chat_id= chatid,text = msg1)
msg2 = "Last name: "+str(user.last_name)
context.bot.send_message(chat_id= chatid,text = msg2)
msg3 = "Phone number: "+str(pn)
context.bot.send_message(chat_id= chatid,text = msg3)
utilities.update_need_assigned(needid, user_id)
victim_details= utilities.details_of_user(chatid)
update.message.reply_text(
"Thankyou! The person in need have been informed. You should also contact this person to deliver his need to him."'\n'
"These are the person\'s details: "'\n'
)
update.message.reply_text(
victim_details
)
update.message.reply_text(
"Thank you! You have been assigned this need."
)
return ConversationHandler.END
###########################################################################################################################################################
# upadtes_Delivered_needs
###########################################################################################################################################################
def ask_for_needid(update: Update, context: CallbackContext) -> int:
user = update.message.from_user
user_id = user.id
if not Utils.user_is_volunteer(user_id):
reply_keyboard = [['Menu'],['Exit']]
update.message.reply_text(
"You are not registered as a volunteer.",
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return GO_MENU
else:
a = utilities.show_assignedNeeds(user_id)
if len(a) > 0:
reply_keyboard=[['Cancel']]
update.message.reply_text(
"If you have delivered a need, please provide me with it\'s need ID"
)
update.message.reply_text(
"These are your assigned needs to deliver: "
)
update.message.reply_text(
a,
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return ASK_NEED_ID
else:
reply_keyboard = [['Menu'],['Exit']]
update.message.reply_text(
"You have no assigned needs to deilver.",
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return GO_MENU
def update_need_pickedup(update: Update, context: CallbackContext) -> int:
user = update.message.from_user
user_id = user.id
text = update.message.text
needid = int(text)
utilities.set_need_delivered(needid, user_id)
pn = Utils.get_phonenumber(user_id)
chatid = Utils.get_victimid_from_need(needid)
context.bot.send_message(chat_id= chatid,text = "Can you please confirm that your need have been delivered.")
msg = "Need ID: " + str(text)
context.bot.send_message(chat_id= chatid,text = msg)
context.bot.send_message(chat_id= chatid,text = "The Volunteer's information are:")
msg1 = "First name: "+str(user.first_name)
context.bot.send_message(chat_id= chatid,text = msg1)
msg2 = "Last name: "+str(user.last_name)
context.bot.send_message(chat_id= chatid,text = msg2)
msg3 = "Phone number: "+str(pn)
context.bot.send_message(chat_id= chatid,text = msg3)
context.bot.send_message(chat_id= chatid,text = "Run the /Confirm_Delivery command to confirm if you have received your need or not.")
update.message.reply_text(
"Thank you! We are now up-to-date with you."
)
return ConversationHandler.END
###########################################################################################################################################################
# Confirm_Delivery
###########################################################################################################################################################
def confirm_delivery(update: Update, context: CallbackContext) -> int:
user = update.message.from_user
user_id = user.id
if not Utils.user_is_victim(user_id):
update.message.reply_text(
"You did not request any need."
)
return ConversationHandler.END
else:
reply_keyboard = [['I received the delivery'],
['I haven\'t received the deilvery'],['Cancel']]
update.message.reply_text(
"Please choose wether you received your need or not.",
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return CHOOSE_CONFIRM
def delivery_received(update: Update, context: CallbackContext) -> int:
user = update.message.from_user
user_id = user.id
reply_keyboard = [['Cancel']]
a = utilities.requested_needs(user_id)
update.message.reply_text(
"Here are the needs you have requested"
)
for x in a:
update.message.reply_text(
"Need ID: " f'{x[0]}''\n'
"Description: " f'{x[1]}'
)
update.message.reply_text(
"Please provide me with the need ID you have received",
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return RECEIVED_NEED
def delivery_received_DB(update: Update, context: CallbackContext) -> int:
user = update.message.from_user
user_id = user.id
text = update.message.text
needid = int(text)
utilities.update_confirmation(user_id,needid , 1)
update.message.reply_text(
"Thank you."
)
return ConversationHandler.END
def delivery_not_received(update: Update, context: CallbackContext) -> int:
user = update.message.from_user
user_id = user.id
reply_keyboard = [['Cancel']]
a = utilities.requested_needs(user_id)
update.message.reply_text(
"Here are the needs you have requested"
)
for x in a:
update.message.reply_text(
"Need ID: " f'{x[0]}''\n'
"Description: " f'{x[1]}'
)
update.message.reply_text(
"Please provide me with the need ID of the need you have not received.",
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return NOT_RECEIVED_NEED
def delivery_not_received_DB(update: Update, context: CallbackContext) -> int:
user = update.message.from_user
user_id = user.id
text = update.message.text
needid = int(text)
utilities.update_confirmation(user_id,needid , 2)
update.message.reply_text(
"We will contact the volunteer to see what is the problem."
)
return ConversationHandler.END
###########################################################################################################################################################
# Start Campaign
###########################################################################################################################################################
def start_campaign(update: Update, context: CallbackContext) -> int:
user = update.message.from_user
user_id = user.id
c1.ownerID = user_id
reply_keyboard = [['Sounds good, let\'s go!'],['Exit']]
update.message.reply_text(
"Hello! You are about to start a Campaign."
"But first, I need to know some information about this Campaign.",
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return START_CAMP
def camp_name(update: Update, context: CallbackContext) -> int:
reply_keyboard = [['Cancel']]
update.message.reply_text(
"Please write the name of your campaign.",
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True),
)
return CAMP_NAME
def camp_name_text(update: Update, context: CallbackContext) -> int:
reply_keyboard = [['Cancel']]
text = update.message.text
c1.name = text
update.message.reply_text(