def update_request_info(*args, **kwargs): r_id = request.form.get('request_id') name = request.form.get('name') mob_number = request.form.get('mob_number') email_id = request.form.get('email_id') age = request.form.get('age') address = request.form.get('address') geoaddress = request.form.get('geoaddress', address) user_request = request.form.get('request') latitude = request.form.get('latitude') longitude = request.form.get('longitude') source = request.form.get('source') status = request.form.get('status') country = request.form.get('country') r_df = request_data_by_id(r_id) if (r_df.shape[0] == 0): return json.dumps({ 'status': False, 'string_response': 'Request ID does not exist.', 'Response': {} }) req_dict = { 'name': name, 'mob_number': mob_number, 'email_id': email_id, 'country': country, 'address': address, 'geoaddress': geoaddress, 'latitude': latitude, 'longitude': longitude, 'source': source, 'age': age, 'request': user_request, 'status': status } if (r_df.shape[0] == 0): return json.dumps({ 'status': False, 'string_response': 'Request does not exist', 'Response': {} }) if (r_id is None): return json.dumps({ 'Response': {}, 'status': False, 'string_response': 'Request ID mandatory' }) r_dict = {x: req_dict[x] for x in req_dict if req_dict[x] is not None} response = json.dumps(update_requests_db({'id': r_id}, r_dict)) return response
def check_contact(mob_number, table='volunteers', url=whatsapp_api_url): try: url = url + "/v1/contacts" authkey = get_auth_key() data = { "blocking": "wait", "contacts": ["{mob_number}".format(mob_number=mob_number)], "force_check": True } headers = { 'Content-type': 'application/json', 'Authorization': "Bearer " + authkey } response = requests.request("POST", url, headers=headers, data=json.dumps(data)).json() if (response.get("errors") is None): print('Contact Verification API Run successfully', flush=True) print('data sent was', data, flush=True) print('response json is ', response, flush=True) if (response.get("contacts")[0].get("status") == 'valid'): wa_id = response.get("contacts")[0].get("wa_id") print('Valid WhatsApp contact', flush=True) elif (response.get("contacts")[0].get("status") == 'processing'): wa_id = 'processing' print('WhatsApp API processing', flush=True) else: wa_id = 'SMS' print('Invalid WhatsApp contact', flush=True) print(wa_id) where_dict = {'mob_number': mob_number[3:]} set_dict = {'whatsapp_id': wa_id} if (table == 'volunteers'): res = update_volunteers_db(where_dict, set_dict) elif (table == 'requests'): res = update_requests_db(where_dict, set_dict) elif (table == 'users'): res = update_users_db(where_dict, set_dict) else: print('Error in the API Call', flush=True) print('data sent was', data, flush=True) print('response json is ', response, flush=True) return None except Exception as e: print('error in check_contact', e, flush=True) return None
def verify_request(*args, **kwargs): uuid = request.form.get('uuid') what = request.form.get('what') why = request.form.get('why') financial_assistance = request.form.get('financial_assistance', 0) verification_status = request.form.get('verification_status') verified_by = kwargs.get('user_id', 0) r_id = request.form.get('r_id') name = request.form.get('name') where = request.form.get('geoaddress') mob_number = request.form.get('mob_number') urgent_status = request.form.get('urgent', 'no') source = request.form.get('source', 'covidsos') volunteers_reqd = request.form.get('volunteer_count', 1) current_time = dt.datetime.utcnow() + dt.timedelta(minutes=330) if (verification_status is None): return json.dumps({ 'Response': {}, 'status': False, 'string_response': 'Please send verification status' }) if ((r_id is None) or (uuid is None)): return json.dumps({ 'Response': {}, 'status': False, 'string_response': 'Please send UUID/request ID' }) r_df = request_data_by_uuid(uuid) if (r_df.shape[0] == 0): return json.dumps({ 'Response': {}, 'status': False, 'string_response': 'Invalid UUID/request ID' }) if (r_df.loc[0, 'source'] != source): response_0 = update_requests_db({'uuid': uuid}, {'source': source}) if (r_df.loc[0, 'status'] == 'received'): r_v_dict = { 'r_id': [r_id], 'why': [why], 'what': [what], 'where': [where], 'verification_status': [verification_status], 'verified_by': [verified_by], 'timestamp': [current_time], 'financial_assistance': [financial_assistance], 'urgent': [urgent_status] } df = pd.DataFrame(r_v_dict) expected_columns = [ 'timestamp', 'r_id', 'what', 'why', 'where', 'verification_status', 'verified_by', 'financial_assistance', 'urgent' ] response_2 = update_requests_db({'uuid': uuid}, { 'status': verification_status, 'volunteers_reqd': volunteers_reqd }) print('updated the status') past_id, past_status = check_past_verification(str(r_id)) if (past_status == True): r_v_dict = { 'r_id': r_id, 'why': why, 'what': what, 'where': where, 'verification_status': verification_status, 'verified_by': verified_by, 'timestamp': current_time, 'financial_assistance': financial_assistance, 'urgent': urgent_status } rv_dict = {x: r_v_dict[x] for x in r_v_dict} update_request_v_db({'id': (past_id)}, rv_dict) else: x, y = add_request_verification_db(df) if (verification_status == 'verified'): #Move to message_templates.py file requestor_text = '[COVIDSOS] Your request has been verified. We will look for volunteers in your neighbourhood.' send_sms(requestor_text, sms_to=int(mob_number), sms_type='transactional', send=True) message_all_volunteers(uuid, neighbourhood_radius, search_radius) else: #Move to message_templates.py file requestor_text = '[COVIDSOS] Your request has been cancelled/rejected. If you still need help, please submit request again.' send_sms(requestor_text, sms_to=int(mob_number), sms_type='transactional', send=True) return json.dumps({ 'Response': {}, 'status': response_2['status'], 'string_response': response_2['string_response'] }) else: return json.dumps({ 'Response': {}, 'status': False, 'string_response': 'Request already verified/rejected' })
def auto_assign_volunteer(): v_id = request.form.get('volunteer_id') uuid = request.form.get('uuid') matching_by = 'autoassigned' task_action = request.form.get('task_action') r_df = request_data_by_uuid(uuid) v_df = volunteer_data_by_id(v_id) if (r_df.shape[0] == 0): return json.dumps({ 'status': False, 'string_response': 'Request ID does not exist.', 'Response': {} }) if (v_df.shape[0] == 0): return json.dumps({ 'status': False, 'string_response': 'Volunteer does not exist', 'Response': {} }) else: r_id = r_df.loc[0, 'r_id'] if (((r_df.loc[0, 'status'] == 'received') or (r_df.loc[0, 'status'] == 'verified') or (r_df.loc[0, 'status'] == 'pending')) & (task_action == 'accepted')): current_time = dt.datetime.utcnow() + dt.timedelta(minutes=330) req_dict = { 'volunteer_id': [v_id], 'request_id': [r_id], 'matching_by': [matching_by], 'timestamp': [current_time] } df = pd.DataFrame(req_dict) response = request_matching(df) response_2 = update_requests_db({'id': r_id}, {'status': 'matched'}) response_3 = update_nearby_volunteers_db({'r_id': r_id}, {'status': 'expired'}) #Move to message_templates.py file #Send to Volunteer v_sms_text = '[COVID SOS] Thank you agreeing to help. Name:' + r_df.loc[ 0, 'name'] + ' Mob:' + str( r_df.loc[0, 'mob_number']) + ' Request:' + r_df.loc[ 0, 'request'] + ' Address:' + r_df.loc[0, 'geoaddress'] send_sms(v_sms_text, int(v_df.loc[0, 'mob_number']), sms_type='transactional', send=True) #Send to Requestor v_sms_text = '[COVID SOS] Volunteer ' + v_df.loc[ 0, 'name'] + ' will help you. Mob: ' + str( v_df.loc[0, 'mob_number']) send_sms(v_sms_text, int(r_df.loc[0, 'mob_number']), sms_type='transactional', send=True) return json.dumps(response) elif ((r_df.loc[0, 'status'] == 'received') or (r_df.loc[0, 'status'] == 'verified') or (r_df.loc[0, 'status'] == 'pending')): response_3 = update_nearby_volunteers_db( { 'r_id': r_id, 'v_id': v_id }, {'status': 'expired'}) return json.dumps({ 'status': True, 'string_response': 'Request rejected', 'Response': {} }) else: return json.dumps({ 'status': False, 'string_response': 'Request already assigned', 'Response': {} })
def assign_request_to_volunteer(volunteer_id, request_id, matched_by): r_df = request_data_by_id(request_id) v_df = volunteer_data_by_id(volunteer_id) if (r_df.shape[0] == 0): return { 'status': False, 'string_response': 'Request ID does not exist.', 'Response': {} } if (v_df.shape[0] == 0): return { 'status': False, 'string_response': 'Volunteer does not exist', 'Response': {} } else: if (r_df.loc[0, 'status'] in ['received', 'verified', 'pending']): current_time = dt.datetime.utcnow() + dt.timedelta(minutes=330) req_dict = { 'volunteer_id': [volunteer_id], 'request_id': [r_df.loc[0, 'r_id']], 'matching_by': [matched_by], 'timestamp': [current_time] } df = pd.DataFrame(req_dict) #Add entry in request_matching table response = request_matching(df) #Update request status as matched if response['status'] == True: volunteers_assigned = get_volunteers_assigned_to_request( request_id) if r_df.loc[0, 'volunteers_reqd'] == volunteers_assigned: response_2 = update_requests_db({'id': request_id}, {'status': 'matched'}) response_3 = update_nearby_volunteers_db( {'r_id': request_id}, {'status': 'expired'}) #Move to message_templates.py file #Send to Volunteer v_sms_text = '[COVID SOS] Thank you agreeing to help. Name:' + r_df.loc[ 0, 'name'] + ' Mob:' + str( r_df.loc[0, 'mob_number']) + ' Request:' + r_df.loc[ 0, 'request'] + ' Address:' + r_df.loc[0, 'geoaddress'] send_sms(v_sms_text, int(v_df.loc[0, 'mob_number']), sms_type='transactional', send=True) #Send to Requestor r_sms_text = '[COVID SOS] Volunteer ' + v_df.loc[ 0, 'name'] + ' will help you. Mob: ' + str( v_df.loc[0, 'mob_number']) send_sms(r_sms_text, int(r_df.loc[0, 'mob_number']), sms_type='transactional', send=True) #Send to Moderator m_sms_text = '[COVID SOS] Volunteer ' + v_df.loc[ 0, 'name'] + ' Mob: ' + str( v_df.loc[0, 'mob_number']) + ' assigned to ' + r_df.loc[ 0, 'name'] + ' Mob:' + str(r_df.loc[0, 'mob_number']) moderator_list = get_moderator_list() for i_number in moderator_list: send_sms(m_sms_text, int(i_number), sms_type='transactional', send=True) else: return { 'status': False, 'string_response': 'Request already assigned/closed/completed', 'Response': {} } return response