-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
149 lines (132 loc) · 6 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
import sys
import json
import numpy as np
from os import path
from user import user
from utility._utility import take_input, read_user_input, idx_2_res, res_spliter, generate_dict, array2dict, regex_checker
from utility._utility import process_needhelp_input, process_needhelp_result, needhelp_message, decode_residx_op
from user_utility.user_utility import validate_email, validate_mobile, check_plasma, save_details, load_file, after_bg_save
from cms_queries.queries import get_request, post_request, get_object
def main(chat_id, txt):
# print(txt, chat_id)
# response messages:
success_message = 'Thank you for your response'
plasma_message = 'Please provide blood groups for provided plasma'
error_message = 'Please enter correct details'
invalid_cityorstate = 'Please enter a valid city or state!'
Reply_from_regex = regex_checker(txt)
if Reply_from_regex == '1':
print("contributor")
url = "https://covid-bot-cms.herokuapp.com/"
chat_id = str(chat_id)
reload_dict = get_object(endpoint='Beta-objects', chat_id=chat_id, url=url)
print(reload_dict)
reload_has_plasma = reload_dict["has_plasma"]
reload_chat_id = reload_dict["chat_id"]
if reload_has_plasma == 'True':
# new user object
print('hasPlasma block')
user_object_Reload = user(reload_dict["Name"], reload_dict["Email"], reload_dict["Mobile"])
user_object_Reload.update_attributes('state', reload_dict["State"])
user_object_Reload.update_attributes('city', reload_dict["City"])
res_list = reload_dict["Resources"].split(',')
print(res_list)
for i in res_list:
user_object_Reload.update_attributes('resources', i)
blood_grps = txt.strip()
# update blood attributes
user_object_Reload.update_attributes('blood_grp', blood_grps)
gen_dict = generate_dict(user_object_Reload.get_details())
print(gen_dict)
after_bg_save(gen_dict, reload_chat_id, False)
# add post method to update database
print(gen_dict)
print("if block")
# a = gen_dict["Resources"]
# stri = ''
# for i in a:
# stri += i + ','
# gen_dict["Resources"] = stri
url = "https://covid-bot-cms.herokuapp.com/"
res = post_request(endpoint="data", body=gen_dict, url=url)
print('here')
print(res)
print(success_message)
return success_message
else:
print('Res block')
name, mobile, email, city, state, res_ids, description = read_user_input(txt)
print(name, mobile, email, city, state, res_ids, description)
idx_2_res_map = idx_2_res()
# print(name, mobile, email, city, state, res_ids, description)
if validate_mobile(mobile) and validate_email(email):
user_obj = user(name, email, mobile)
user_obj.resource_provider()
# return message for invalid inputs reg. city or state.
city = take_input(city, 'city')
# print(type(city))
state = take_input(state, 'state')
# print(state)
if city == 'True' or state == 'True':
print('Invalid City')
return invalid_cityorstate
res_ids = res_spliter(res_ids)
def Lambda_res(x): return idx_2_res_map[int(x)]
resources = [Lambda_res(res_id) for res_id in res_ids]
contains_plasma = check_plasma(resources)
for res in resources:
user_obj.update_attributes('resources', res)
user_obj.update_attributes('state', state)
user_obj.update_attributes('city', city)
details = user_obj.get_details()
gen_dict = generate_dict(details)
# print(gen_dict)
if contains_plasma:
save_details(gen_dict, chat_id, True)
print(plasma_message)
return plasma_message
else:
# add post method to update database
print(gen_dict)
a = gen_dict["Resources"]
stri = ''
for i in a:
stri += i + ','
gen_dict["Resources"] = stri
url = "https://covid-bot-cms.herokuapp.com/"
res = post_request(endpoint="data", body=gen_dict, url=url)
print(res)
print("else block")
print(success_message)
return success_message
print(error_message)
return error_message
elif Reply_from_regex == '2':
print("need help")
# process user input
res_idx, city = process_needhelp_input(txt)
city = take_input(city, 'city')
# return message for invalid inputs reg. city or state.
if city == 'True':
print('Invalid City')
return invalid_cityorstate
idx2res_map = decode_residx_op()
res = idx2res_map[int(res_idx)]
# print(res_idx, city)
print(res, city)
url = "https://covid-bot-cms.herokuapp.com/"
res = get_request(res, endpoint="data", url=url, city=city)
if res == 'raise flag':
return 'Resource not available in entered city please try again later!'
else:
result_list = process_needhelp_result(res)
print(result_list)
return_message = needhelp_message(result_list)
print(return_message)
return return_message
else:
print("Not contributor or need help")
print(error_message)
return error_message
if __name__ == "__main__":
main('1721282209', '')