def answer_verification( question_id, answer, company_id, domain_id, ): chat_questions = db.get_question_chat(company_id, domain_id)['questions'] actual_answers = [] mapped_question_id = 0 is_db_provider = False for index, value in enumerate(chat_questions): if value['question_id'] == question_id: actual_answers = value['answers'] mapped_question_id = value['next_question_id'][0] is_db_provider = value["is_db_provider"] break if is_db_provider: return True, mapped_question_id else: if len(actual_answers) == 0: return True, mapped_question_id else: for index, actual_answer in enumerate(actual_answers): if answer in actual_answer["answer"]: return True, actual_answer["mapped_question_id"] return False, 0
def email_sending(chat_session_id, company_id, domain_id): try: now = datetime.datetime.now() chat_json = (db.get_value_with_session_id(chat_session_id)) chat_history = chat_json['chat_flow'] chat_questions = db.get_question_chat(company_id, domain_id)['questions'] questions = [] answers = [] for index, value in enumerate(chat_history): question_id = int(value["question_id"]) answer = value["answer"] for index_1, value_1 in enumerate(chat_questions): if value_1["question_id"] == question_id: questions.append(value_1["question"]) answers.append(answer) break html = "<div> Question : {} </div> <div> Answer : {} </div>" final_html = "" for index, value in enumerate(questions): string = html.format(value.replace("\n", ""), answers[index]) final_html = final_html + string + " " mail_html_content = html_string.format(final_html) msg = MIMEMultipart() msg['Subject'] = "REG : Chat history..." msg['From'] = me msg['To'] = ",".join(to) ctype = None if ctype is None or encoding is not None: ctype = "application/octet-stream" maintype, subtype = ctype.split("/", 1) text = MIMEText(mail_html_content, 'html') msg.attach(text) s = smtplib.SMTP('smtp.zoho.com', 587) context = ssl.create_default_context() s.starttls(context=context) s.login(me, my_password) s.sendmail(me, to, msg.as_string()) s.quit() return True except Exception as ex: print("Error == ", ex) logf = open("Email_error.log", "w") logf.write("Error == {0} : {1}\n".format( now.strftime("%Y-%m-%d %H:%M:%S"), str(ex.strerror))) logf.close() return False
def email_sending(chat_session_id, company_id, domain_id): chat_json = (db.get_value_with_session_id(chat_session_id)) chat_history = chat_json['chat_flow'] chat_questions = db.get_question_chat(company_id, domain_id)['questions'] questions = [] answers = [] for index,value in enumerate(chat_history): question_id = int(value["question_id"]) answer = value["answer"] for index_1,value_1 in enumerate(chat_questions): if value_1["question_id"] == question_id: questions.append(value_1["question"]) answers.append(answer) break html = "<div> Question : {} </div> <div> Answer : {} </div>" final_html = "" for index,value in enumerate(questions): string = html.format(value.replace("\n",""),answers[index]) final_html = final_html + string +" " mail_html_content = html_string.format(final_html) msg = MIMEMultipart() msg['Subject'] = "REG : Chat history..." msg['From'] = me msg['To'] = ",".join(to) ctype = None if ctype is None or encoding is not None: ctype = "application/octet-stream" maintype, subtype = ctype.split("/", 1) text = MIMEText(mail_html_content, 'html') msg.attach(text) s = smtplib.SMTP('smtp.zoho.com', 587,) s.starttls() s.login(me, my_password) s.sendmail(me, to, msg.as_string()) s.quit() return True
def question_generation(question_id, company_id, domain_id, answer): chat_responces = { "question": "", "question_id": "", "input_type": "", "flow_end": "", "link": "" } chat_questions = db.get_question_chat(company_id, domain_id)['questions'] chat_responce = question_flow_identification(chat_questions, question_id) if chat_responce['is_db_provider']: answers = another_request_provider(chat_responce, answer) if chat_responce['flow_end'] == True: if company_id == 1 and domain_id == 1: HotelCode = answer[2]['answer'] CheckinDate = str(answer[3]['answer'].split("~")[0]).replace( "/", "-") CheckoutDate = str(answer[3]['answer'].split("~")[1]).replace( "/", "-") chat_responces["question"] = chat_responce['question'] chat_responces["link"] = str(chat_responce['link']).format( HotelCode, CheckinDate, CheckoutDate) elif company_id == 2 and domain_id == 1: HotelCode = "1110" CheckinDate = str(answer[3]['answer'].split("~")[0]).replace( "/", "-") CheckoutDate = str(answer[3]['answer'].split("~")[1]).replace( "/", "-") chat_responces["question"] = str( chat_responce['question']).format(answers) chat_responces["link"] = str(chat_responce['link']).format( HotelCode, CheckinDate, CheckoutDate) else: chat_responces["question"] = chat_responce['question'] chat_responces["link"] = chat_responce['link'] chat_responces["question_id"] = chat_responce['question_id'] chat_responces["input_type"] = chat_responce['input_type'] chat_responces["flow_end"] = chat_responce['flow_end'] chat_responces["option_answers"] = answers else: if chat_responce['flow_end'] == True: if company_id == 1 and domain_id == 1: HotelCode = answer[2]['answer'] CheckinDate = str(answer[3]['answer'].split("~")[0]).replace( "/", "-") CheckoutDate = str(answer[3]['answer'].split("~")[1]).replace( "/", "-") chat_responces["question"] = chat_responce['question'] chat_responces["link"] = str(chat_responce['link']).format( HotelCode, CheckinDate, CheckoutDate) elif company_id == 2 and domain_id == 1: HotelCode = "1110" CheckinDate = str(answer[3]['answer'].split("~")[0]).replace( "/", "-") CheckoutDate = str(answer[3]['answer'].split("~")[1]).replace( "/", "-") chat_responces["question"] = str( chat_responce['question']).format( chat_responce["option_answers"]) chat_responces["link"] = str(chat_responce['link']).format( HotelCode, CheckinDate, CheckoutDate) else: chat_responces["question"] = chat_responce['question'] chat_responces["link"] = chat_responce['link'] chat_responces["question_id"] = chat_responce['question_id'] chat_responces["input_type"] = chat_responce['input_type'] chat_responces["flow_end"] = chat_responce['flow_end'] chat_responces["option_answers"] = chat_responce['option_answers'] return chat_responces
def question_generation(question_id, company_id, domain_id, answer): chat_responces = {"question": "", "question_id": "", "input_type": "", "flow_end": "","link":"","error_msg":""} chat_questions = db.get_question_chat( company_id, domain_id)['questions'] chat_responce = question_flow_identification( chat_questions, question_id) if chat_responce['is_db_provider']: room_type, answers, availability_check, after_provided_answer_check = another_request_provider( chat_responce, answer) if after_provided_answer_check: is_answer_checked, next_question = answer_verification( question_id, availability_check, company_id, domain_id) chat_responce = question_flow_identification( chat_questions, next_question) chat_responces["question_id"] = chat_responce['question_id'] chat_responces["question"] = chat_responce['question'].format(room_type,answers) chat_responces["input_type"] = chat_responce['input_type'] chat_responces["flow_end"] = chat_responce['flow_end'] chat_responces["option_answers"] = room_type chat_responces["error_msg"] = chat_responce['error_msg'] return chat_responces if chat_responce['flow_end'] == True: if company_id == 1 and domain_id == 1: HotelCode = answer[2]['answer'] CheckinDate = str(answer[3]['answer'].split("~")[ 0]).replace("/", "-") CheckoutDate = str(answer[3]['answer'].split("~")[ 1]).replace("/", "-") chat_responces["question"] = chat_responce['question'] chat_responces["link"] = str(chat_responce['link']).format(HotelCode, CheckinDate, CheckoutDate) elif company_id == 2 and domain_id == 1: HotelCode = "1110" CheckinDate = str(answer[3]['answer'].split("~")[ 0]).replace("/", "-") CheckoutDate = str(answer[3]['answer'].split("~")[ 1]).replace("/", "-") chat_responces["question"] = str(chat_responce['question']).format( answers) chat_responces["link"] = str(chat_responce['link']).format(HotelCode, CheckinDate, CheckoutDate) else: if answers != None: try: chat_responces["question"] = chat_responce['question'].format(answers) except: chat_responces["question"] = chat_responce['question'] else: chat_responces["question"] = chat_responce['question'] chat_responces["link"] = chat_responce['link'] chat_responces["question_id"] = chat_responce['question_id'] chat_responces["input_type"] = chat_responce['input_type'] chat_responces["flow_end"] = chat_responce['flow_end'] chat_responces["option_answers"] = answers else: if chat_responce['flow_end'] == True: if company_id == 1 and domain_id == 1: HotelCode = answer[2]['answer'] CheckinDate = str(answer[3]['answer'].split("~")[ 0]).replace("/", "-") CheckoutDate = str(answer[3]['answer'].split("~")[ 1]).replace("/", "-") chat_responces["question"] = chat_responce['question'] chat_responces["link"] = str(chat_responce['link']).format(HotelCode, CheckinDate, CheckoutDate) elif company_id == 2 and domain_id == 1: HotelCode = "1110" CheckinDate = str(answer[3]['answer'].split("~")[ 0]).replace("/", "-") CheckoutDate = str(answer[3]['answer'].split("~")[ 1]).replace("/", "-") chat_responces["question"] = str(chat_responce['question']).format( chat_responce["option_answers"]) chat_responces["link"] = str(chat_responce['link']).format(HotelCode, CheckinDate, CheckoutDate) else: chat_responces["question"] = chat_responce['question'] chat_responces["link"] = chat_responce['link'] chat_responces["question_id"] = chat_responce['question_id'] chat_responces["question"] = chat_responce['question'] chat_responces["link"] = chat_responce['link'] chat_responces["input_type"] = chat_responce['input_type'] chat_responces["flow_end"] = chat_responce['flow_end'] chat_responces["option_answers"] = chat_responce['option_answers'] chat_responces["error_msg"] = chat_responce['error_msg'] return chat_responces