def default_welcome(): cursor = mysql_conn.cursor() if request.method == 'POST': dialogflow_response = DialogflowResponse( "Welcome to Flook.Your Smart flight reservation agent") dialogflow_response.add( SimpleResponse( "Welcome to Flook.Your Smart flight reservation agent", "Welcome to Flook.Your Smart flight reservation agent")) response_text = dialogflow_response.get_final_response() dialogflow_request = DialogflowRequest(request.data) req = json.loads((request.data).decode('utf-8')) if dialogflow_request.get_intent_displayName( ) == "Default Welcome Intent": dialogflow_response = DialogflowResponse( "Welcome to Flook.Your Smart flight reservation agent") dialogflow_response.add( SimpleResponse("Welcome to my test dialogflow webhook", "Welcome to my test dialogflow webhook")) response_text = dialogflow_response.get_final_response() if dialogflow_request.get_intent_displayName() == "book_flight": params = dialogflow_request.get_paramters() if 'source' in params and 'destination' in params and 'dateofjourney' in params: if params['source'] == '': src = '' fulfillment_text = req['queryResult']['fulfillmentText'] dialogflow_response = DialogflowResponse(fulfillment_text) dialogflow_response.add( SimpleResponse(fulfillment_text, fulfillment_text)) response_text = dialogflow_response.get_final_response() if type(params['source']) == dict: if params['source']['city'] != '': src = params['source']['city'] if params['source']['city'] == '' and params['source'][ 'admin-area'] == '' and params['source'][ 'country'] != '': src = params['source']['country'] if params['source']['city'] == '' and params['source'][ 'admin-area'] != '' and params['source'][ 'country'] == '': src = params['source']['admin-area'] if params['destination'] == '': dstn = '' fulfillment_text = req['queryResult']['fulfillmentText'] dialogflow_response = DialogflowResponse(fulfillment_text) dialogflow_response.add( SimpleResponse(fulfillment_text, fulfillment_text)) response_text = dialogflow_response.get_final_response() if type(params['destination']) == dict: if params['destination']['city'] != '': dstn = params['destination']['city'] if params['destination']['city'] == '' and params[ 'destination']['admin-area'] == '' and params[ 'destination']['country'] != '': dstn = params['destination']['country'] if params['destination']['city'] == '' and params[ 'destination']['admin-area'] != '' and params[ 'destination']['country'] == '': dstn = params['destination']['admin-area'] if dstn == 'Bengaluru': dstn = dstn.replace('Bengaluru', 'Bangalore') dt = params['dateofjourney'] dt_modified = [] if len(dt) == 0: date_of_journey = '' fulfillment_text = req['queryResult']['fulfillmentText'] dialogflow_response = DialogflowResponse(fulfillment_text) dialogflow_response.add( SimpleResponse(fulfillment_text, fulfillment_text)) response_text = dialogflow_response.get_final_response() if len(dt) == 1: dot = re.search(r'\d{4}-\d{2}-\d{2}', dt[0]) date_of_journey = dot.group() + ' ' + '00:00:00' if len(dt) == 2: for i in dt: dot = re.search(r'\d{4}-\d{2}-\d{2}', i) date_of_journey = dot.group() + ' ' + '00:00:00' dt_modified.append(date_of_journey) if src != '' and dstn != '' and len(dt) != 0: print(src, dstn, date_of_journey) if len(dt) == 1: if params['qualifier'] == '': query = 'SELECT * from flights where source=%s AND destination=%s AND date_of_travel=%s' cursor.execute(query, (src, dstn, date_of_journey)) results = cursor.fetchall() if params['qualifier'] != '': qualifier = params['qualifier'] if qualifier == 'direct': query = 'SELECT * from flights where source=%s AND destination=%s AND date_of_travel=%s AND connection=%s' cursor.execute( query, (src, dstn, date_of_journey, 'false')) results = cursor.fetchall() if qualifier == 'cheapest': query = 'SELECT * from flights where source=%s AND destination=%s AND date_of_travel=%s AND connection=%s' cursor.execute( query, (src, dstn, date_of_journey, 'true')) results = cursor.fetchall() if len(dt) == 2: query = 'SELECT * from flights where source=%s AND destination=%s AND date_of_travel between %s and %s' cursor.execute( query, (src, dstn, dt_modified[0], dt_modified[1])) results = cursor.fetchall() app.logger.info(len(results)) app.logger.info(results) if len(results) == 0: dialogflow_response = DialogflowResponse( "No flights available") dialogflow_response.add( SimpleResponse("No flights available", "No flights available")) response_text = dialogflow_response.get_final_response( ) if len(results) == 1: dialogflow_response = DialogflowResponse( "Your ticket has been booked") dialogflow_response.add( SimpleResponse("Your ticket has been booked", "Your ticket has been booked")) response_text = dialogflow_response.get_final_response( ) if len(results) > 1: fulfillment_text = str(results) dialogflow_response = DialogflowResponse( fulfillment_text) dialogflow_response.add( SimpleResponse(fulfillment_text, fulfillment_text)) response_text = dialogflow_response.get_final_response( ) dialogflow_response = DialogflowResponse( "Please select your flight") dialogflow_response.add( SimpleResponse("Please select your flight", "Please select your flight")) response_text = dialogflow_response.get_final_response( ) dialogflow_response = DialogflowResponse() dialogflow_response.expect_user_response = True if dialogflow_request.get_intent_displayName( ) == "book_flight - select.number": params = dialogflow_request.get_single_ouputcontext( 'book_flight-followup')['parameters'] src = params['source.original'] dstn = params['destination.original'] dt = params['dateofjourney'] dt_modified = [] if len(dt) == 1: dot = re.search(r'\d{4}-\d{2}-\d{2}', dt[0]) date_of_journey = dot.group() + ' ' + '00:00:00' if params['qualifier'] == '': query = 'SELECT * from flights where source=%s AND destination=%s AND date_of_travel=%s' cursor.execute(query, (src, dstn, date_of_journey)) results = cursor.fetchall() selection = int(params['number'][0]) if selection <= len(results): dialogflow_response = DialogflowResponse( "Your ticket has been booked") dialogflow_response.add( SimpleResponse("Your ticket has been booked", "Your ticket has been booked")) response_text = dialogflow_response.get_final_response( ) if selection > len(results): dialogflow_response = DialogflowResponse( "Invalid selection,booking cancelled") dialogflow_response.add( SimpleResponse( "Invalid selection,booking cancelled", "Invalid selection,booking cancelled")) response_text = dialogflow_response.get_final_response( ) if params['qualifier'] != '': qualifier = params['qualifier'] if qualifier == 'direct': query = 'SELECT * from flights where source=%s AND destination=%s AND date_of_travel=%s AND connection=%s' cursor.execute(query, (src, dstn, date_of_journey, 'false')) results = cursor.fetchall() selection = int(params['number'][0]) if selection <= len(results): dialogflow_response = DialogflowResponse( "Your ticket has been booked") dialogflow_response.add( SimpleResponse("Your ticket has been booked", "Your ticket has been booked")) response_text = dialogflow_response.get_final_response( ) if selection > len(results): dialogflow_response = DialogflowResponse( "Invalid selection,booking cancelled") dialogflow_response.add( SimpleResponse( "Invalid selection,booking cancelled", "Invalid selection,booking cancelled")) response_text = dialogflow_response.get_final_response( ) if qualifier == 'cheapest': query = 'SELECT * from flights where source=%s AND destination=%s AND date_of_travel=%s AND connection=%s' cursor.execute(query, (src, dstn, date_of_journey, 'true')) results = cursor.fetchall() selection = int(params['number'][0]) if selection <= len(results): dialogflow_response = DialogflowResponse( "Your ticket has been booked") dialogflow_response.add( SimpleResponse("Your ticket has been booked", "Your ticket has been booked")) response_text = dialogflow_response.get_final_response( ) if selection > len(results): dialogflow_response = DialogflowResponse( "Invalid selection,booking cancelled") dialogflow_response.add( SimpleResponse( "Invalid selection,booking cancelled", "Invalid selection,booking cancelled")) response_text = dialogflow_response.get_final_response( ) if len(dt) == 2: for i in dt: dot = re.search(r'\d{4}-\d{2}-\d{2}', i) date_of_journey = dot.group() + ' ' + '00:00:00' dt_modified.append(date_of_journey) query = 'SELECT * from flights where source=%s AND destination=%s AND date_of_travel between %s and %s' cursor.execute(query, (src, dstn, dt_modified[0], dt_modified[1])) results = cursor.fetchall() selection = int(params['number'][0]) if selection <= len(results): dialogflow_response = DialogflowResponse( "Your ticket has been booked") dialogflow_response.add( SimpleResponse("Your ticket has been booked", "Your ticket has been booked")) response_text = dialogflow_response.get_final_response( ) if selection > len(results): dialogflow_response = DialogflowResponse( "Invalid selection,booking cancelled") dialogflow_response.add( SimpleResponse( "Invalid selection,booking cancelled", "Invalid selection,booking cancelled")) response_text = dialogflow_response.get_final_response( ) return response_text else: abort(404)
def get_platform_response(self): dialogflow_response = DialogflowResponse() dialogflow_response.expect_user_response = False dialogflow_response.add( SimpleResponse(self.get_text_message(), self.get_ssml_message())) return dialogflow_response.get_final_response()
def web_hooks(request): dialogflow_request = DialogflowRequest(request.data) session_id = dialogflow_request.get_session().split("/")[-1] if dialogflow_request.get_intent_displayName( ) == "Create Appointment - User": _raw_user_name = dialogflow_request.get_paramter("person") str_raw_user_name = str(_raw_user_name) str_user_name = (str_raw_user_name.strip("{").strip("}").strip( "[").strip("]").strip(":").strip("'")) temp_ref.document(session_id).set({"Patient": str_user_name}) dialogflow_response = DialogflowResponse( "Hi {}! Did I get your name right?".format(str_user_name)) elif dialogflow_request.get_intent_displayName( ) == "Create Appointment - Phone": phone_number = dialogflow_request.get_paramter("phone-number") temp_ref.document(session_id).update({"phone_number": phone_number}) dialogflow_response = DialogflowResponse( "Your phone number is {}. Is that correct?".format(phone_number)) elif dialogflow_request.get_intent_displayName( ) == "Create Appointment - Purpose": purpose = dialogflow_request.get_paramter("purpose") temp_ref.document(session_id).update({"Symptoms": purpose}) dialogflow_response = DialogflowResponse( "Thank you. What date would you like to make this appointment on?") elif dialogflow_request.get_intent_displayName( ) == "Create Appointment - Date": intend_date = dialogflow_request.get_paramter("date") if isinstance(intend_date, str): intend_date = datetime.datetime.fromisoformat(intend_date) intend_date = DatetimeWithNanoseconds( intend_date.year, intend_date.month, intend_date.day, intend_date.hour, intend_date.minute, ) temp_ref.document(session_id).update({"intend_date": intend_date}) dialogflow_response = DialogflowResponse( "Ok, so the date is {}. Is that right?".format(intend_date)) elif (dialogflow_request.get_intent_displayName() == "Create Appointment - Date - yes"): docu_dict = temp_ref.document(session_id).get().to_dict() intend_date = docu_dict["intend_date"] if isinstance(intend_date, str): intend_date = datetime.datetime.fromisoformat(intend_date) intend_date = DatetimeWithNanoseconds( intend_date.year, intend_date.month, intend_date.day, intend_date.hour, intend_date.minute, ) if check_date_validity(intend_date): dialogflow_response = DialogflowResponse( "The date you specified is available. Please indicate a time interval (30 minutes) to book." ) dialogflow_response.expect_user_response = False dialogflow_response.add( OutputContexts("pintox-app", session_id, "Create Appointment - Time", 200, {})) dialogflow_response.add(SystemIntent("Create Appointment - Time")) else: dialogflow_response = DialogflowResponse( "Sorry, the date you selected is full. Please select another date." ) dialogflow_response.expect_user_response = False dialogflow_response.add( OutputContexts("pintox-app", session_id, "Create Appointment - Date", 200, {})) dialogflow_response.add(SystemIntent("Create Appointment - Date")) elif dialogflow_request.get_intent_displayName( ) == "Create Appointment - Time": time_period = dialogflow_request.get_paramter("time-period") start_time, end_time = time_period["startTime"], time_period["endTime"] if isinstance(start_time, str): start_time = datetime.datetime.fromisoformat(start_time) start_time = DatetimeWithNanoseconds( start_time.year, start_time.month, start_time.day, start_time.hour, start_time.minute, ) if isinstance(end_time, str): end_time = datetime.datetime.fromisoformat(end_time) end_time = DatetimeWithNanoseconds( end_time.year, end_time.month, end_time.day, end_time.hour, end_time.minute, ) temp_ref.document(session_id).update({ "startTime": start_time, "endTime": end_time }) dialogflow_response = DialogflowResponse( "Your time is from {} to {}. Is that correct?".format( start_time, end_time)) elif (dialogflow_request.get_intent_displayName() == "Create Appointment - Time - yes"): docu_dict = temp_ref.document(session_id).get().to_dict() start_time, end_time = docu_dict["startTime"], docu_dict["endTime"] if isinstance(start_time, str): start_time = datetime.datetime.fromisoformat(start_time) start_time = DatetimeWithNanoseconds( start_time.year, start_time.month, start_time.day, start_time.hour, start_time.minute, ) if isinstance(end_time, str): end_time = datetime.datetime.fromisoformat(end_time) end_time = DatetimeWithNanoseconds( end_time.year, end_time.month, end_time.day, end_time.hour, end_time.minute, ) res, msg = check_time_validity(start_time, end_time) if res: dialogflow_response = DialogflowResponse( "The appointment has been booked. Would you like to book another?" ) appointments_ref.document().set(docu_dict) patients_ref.document(docu_dict["phone_number"]).set( {"name": docu_dict["Patient"]}) else: if msg == "invalid time": dialogflow_response = DialogflowResponse( "The time you specified is out of the working hour. Please select another time." ) dialogflow_response.expect_user_response = False dialogflow_response.add( SystemIntent("Create Appointment - Time - hours")) else: dialogflow_response = DialogflowResponse( "Sorry, the time you selected is unavailable. Please select another time." ) dialogflow_response.expect_user_response = False dialogflow_response.add( SystemIntent("Create Appointment - Time - failure")) else: dialogflow_response = DialogflowResponse( "This is a text response from webhook.") response = app.response_class( response=dialogflow_response.get_final_response(), mimetype="application/json") return response