def begin(): uid = request.form.get("CallSid") caller = request.form.get('Caller') conversation = Conversation.from_uid(uid) conversation.set_param("intent", "resetPassword") url = '/api/reset-password/gather-portal-id?ntry=1' payload = None entities = list(filter(lambda x: 'luis' in x[0], request.args.items())) entities = {x[0]: x[1] for x in entities} if entities.items(): payload = urllib.parse.urlencode(entities) for entity in entities.items(): conversation.set_param(entity[0], entity[1]) if entities.get('luis_portal_id'): resp = twilio_response.Redirect() resp.text = "Sure! I can help you with that. Let me check your portal ID against active directory data" url = '/api/reset-password/check-portal-id?portalId={}'.format( entities.get('luis_portal_id')) url = url + '&' + payload resp.url = url log(caller, 'bot', resp.text, uid) return str(resp.twiml) else: text = "Sure! I can help you with password reset. What is your 6 digit portal ID." resp = twilio_response.GatherSpeech() resp.url = url resp.text = text log(caller, 'bot', resp.text, uid) return str(resp.twiml)
def check_portal_id(): uid = request.form.get("CallSid") caller = request.form.get('Caller') entities = list(filter(lambda x: 'luis' in x[0], request.args.items())) portalId = int(request.args.get('portalId', '0')) ntry = int(request.args.get('ntry', '2')) payload = None error = False skip_list = {} session = Conversation.from_uid(uid) entities = {x[0]: x[1] for x in entities} ad = Graph(portalId) try: user_data = ad.getUserData() user_data = {'u_' + k: v for k, v in user_data.items()} print(user_data) user_data['u_portal_id'] = portalId url_user_data = urllib.parse.urlencode(user_data) payload = {**entities, **user_data} for item in user_data.items(): key, val = item session.set_param(key, val) except Exception as e: error = True text = str(e) print(text) pass if error: url = '/api/reset-password/gather-portal-id?ntry={}&ad_error=True'.format( str(ntry)) resp = twilio_response.Redirect(url=url) session.del_param('luis_portal_id') return str(resp.twiml) if entities.get('luis_city'): resp = twilio_response.Redirect() resp.text = "Okay! {} your portal ID is correct. Let me validate your city".format( user_data.get('u_name')) url = '/api/reset-password/check-city?'.format(entities['luis_city']) payload['city'] = payload['luis_city'] payload = urllib.parse.urlencode(payload) if payload: url = url + payload resp.url = url log(caller, 'bot', resp.text, uid) return str(resp.twiml) resp = twilio_response.GatherSpeech() resp.text = "Okay! {} your portal ID is correct. Please tell me the city where you live?".format( user_data.get('u_name')) payload = urllib.parse.urlencode(payload) resp.url = '/api/reset-password/gather-city?{}'.format(payload) log(caller, 'bot', resp.text, uid) return str(resp.twiml)
def gather_postal_code(): uid = request.form.get('CallSid') caller = request.form.get('Caller') user_speech = request.form.get('SpeechResult',None) input_digits = request.form.get('Digits', None) data = input_digits or user_speech session = Conversation.from_uid(uid) payload = request.args.items() payload = { x[0]:x[1] for x in payload} u_postal_code = payload.get('u_postal_code',None) u_postal_code = u_postal_code if u_postal_code else session.get_param('u_postal_code').decode() print("u_postal_code = "+ str(u_postal_code)) ntry = int(request.args.get('ntry','0')) if ntry > 4: text = "Maximum Number of Retries reached. Contecting to Service Desk. Please Wait" resp = twilio_response.Hangup(text=text) return str(resp.twiml) ntry += 1 if payload.get('ntry'): del payload['ntry'] if not data: text = "I didn't quite catch that. Please enter your 6 digit postal or zip code again." url = '/api/reset-password/gather-postal-code?ntry={}'.format(str(ntry)) resp = twilio_response.GatherSpeech(text, url) return str(resp.twiml) if data: print("data = " + data) log(caller,caller, data, uid) gather_postal_code, text = parseContact(data) print("gather_postal_code = "+ str(gather_postal_code)) if gather_postal_code is None: url = '/api/reset-password/gather-postal-code?ntry={}'.format(str(ntry)) resp = twilio_response.GatherSpeech(text, url) log(caller,'bot', resp.text, uid) return str(resp.twiml) if str(gather_postal_code).lower() == u_postal_code.lower(): resp = twilio_response.GatherSpeech() resp.text = "You postal code, , {} is correct".format(u_postal_code) resp.url = '/api/reset-password/reset-ad-password' log(caller,'bot', resp.text, uid) return str(resp.twiml) else: text = "Your postal-code did not match with active directory data. Please enter it again" url = '/api/reset-password/gather-postal-code?ntry={}'.format(str(ntry)) resp = twilio_response.GatherSpeech(text, url) log(caller,'bot', resp.text, uid) return str(resp.twiml)
def gatherIntent(): caller = request.form.get('Caller') uid = request.form.get('CallSid') speech = request.form.get('SpeechResult', None) if speech: log(caller, 'caller', speech, uid) speech = re.sub(r'(\d)\s+(\d)', r'\1\2', speech) route, entities = luis.getRoute(speech) print(speech) print(entities) if route is None: resp = twilio_response.Hangup() resp.text = "No intent found" log(caller, 'bot', resp.text, uid, event="endConversation") return str(resp.twiml) if route: resp = twilio_response.Redirect() if entities and len(entities.items()) != 0: luis_entities = urllib.parse.urlencode(entities) print(luis_entities) route = route + '?' + luis_entities resp.url = route log(caller, 'bot', resp.text, uid) return str(resp.twiml) ntry = int(request.args.get('ntry', '1')) if ntry > 4: resp = twilio_response.Hangup() resp.text = "You have reached your retry limit. Routing to service desk" log(caller, 'bot', resp.text, uid, event="endConversation") return str(resp.twiml) reprompt = request.args.get( "reprompt", "I am sorry. I didn't quite catch that. Please try again!") resp = twilio_response.GatherSpeech() resp.text = reprompt resp.say = ntry > 1 ntry += 1 data = {'reprompt': resp.text, 'ntry': ntry} data_url = urllib.parse.urlencode(data) resp.url = '/gatherIntent?{}'.format(data_url) log(caller, 'bot', resp.text, uid) return str(resp.twiml)
def answer(): print(request.form) uid = request.form.get('CallSid') print("uid = " + uid) caller = request.form.get('Caller') status = 'on' conv = Conversation(uid, caller, status) conv.save() resp = twilio_response.Redirect() resp.text = "Hello! welcome to NTT service desk. How may I help?" data = {'ntry': 1, 'reprompt': resp.text} data_url = urllib.parse.urlencode(data) resp.url = "/gatherIntent?{}".format(data_url) log(caller, 'bot', resp.text, uid, event="startConversation") return str(resp.twiml)
def check_phone_number(): uid = request.form.get("CallSid") caller = request.form.get('Caller') payload = request.args.items() payload = { x[0]:x[1] for x in payload} session = Conversation.from_uid(uid) phone_number = payload.get('phone_number') u_phone_number = payload.get('u_phone_number', None) u_phone_number = u_phone_number if u_phone_number else session.get_param(u_phone_number).decode() if not phone_number: resp = twilio_response.GatherSpeech() resp.text = "I could not recognize any contact number. Please try again" url = '/api/reset-password/gather-phone-number' if payload: url = url + '?' + urllib.parse.urlencode(payload) resp.url = url log(caller,'bot', resp.text, uid) return str(resp.twiml) if phone_number == u_phone_number: if payload.get('luis_postal_code'): postal_code = payload.get('luis_postal_code') resp = twilio_response.Redirect() resp.text = "your contact number matches with active directory data. Now let me validate your postal code." url = '/api/reset-password/check-postal-code?postal_code={}'.format(postal_code) if payload: url = url + '&' + urllib.parse.urlencode(payload) resp.url = url log(caller,'bot', resp.text, uid) return str(resp.twiml) else: #gahter zip Number resp = twilio_response.GatherSpeech() resp.text = "your contact number matches with active directory data. What is your postal Code?" url = '/api/reset-password/gather-postal-code' if payload: url = url + '?' + urllib.parse.urlencode(payload) resp.url = url log(caller,'bot', resp.text, uid) return str(resp.twiml) else: #Gather phone number space_phone_number = [ i + ' ' for i in phone_number] resp = twilio_response.GatherSpeech() resp.text = "Your phone number {} does not match with active directory data. Please try again".format(space_phone_number) url = '/api/reset-password/gather-phone-number' if payload: url = url + '?' + urllib.parse.urlencode(payload) resp.url = url log(caller,'bot', resp.text, uid) return str(resp.twiml)
def check_city(): uid = request.form.get("CallSid") caller = request.form.get('Caller') payload = request.args.items() payload = { x[0]:x[1] for x in payload} session = Conversation.from_uid(uid) city = payload.get('city') u_city = payload.get('u_city',None) u_city = u_city if u_city else session.get_param(u_city).decode() if not city: resp = twilio_response.GatherSpeech() resp.text = "I could not recognize any city. Please try again" url = '/api/reset-password/gather-city' if payload: url = url + '?' + urllib.parse.urlencode(payload) resp.url = url log(caller,'bot', resp.text, uid) return str(resp.twiml) if city.lower() == u_city.lower(): if payload.get('luis_phone_number'): # redirect to check_phone_number phone_number = payload.get('luis_phone_number') resp = twilio_response.Redirect() resp.text = "Okay! Your city {} also matches with active directory data. Now let me validate your phone number." url = '/api/reset-password/check-phone-number?phone_number={}'.format(phone_number) if payload: url = url + '&' + urllib.parse.urlencode(payload) resp.url = url log(caller,'bot', resp.text, uid) return str(resp.twiml) else: #gahter phone Number resp = twilio_response.GatherSpeech() resp.text = "Okay! Your city {} also matches with active directory data. what is your contact number?" url = '/api/reset-password/gather-phone-number' if payload: url = url + '?' + urllib.parse.urlencode(payload) resp.url = url log(caller,'bot', resp.text, uid) return str(resp.twiml) else: resp = twilio_response.GatherSpeech() resp.text = "Your city {} does not match with active directory data. Please try again".format(city) url = '/api/reset-password/gather-city' if payload: url = url + '?' + urllib.parse.urlencode(payload) resp.url = url log(caller,'bot', resp.text, uid) return str(resp.twiml)
def check_postal_code(): uid = request.form.get("CallSid") caller = request.form.get('Caller') payload = request.args.items() payload = {x[0]: x[1] for x in payload} session = Conversation.from_uid(uid) postal_code = payload.get('postal_code') u_postal_code = payload.get('u_postal_code', None) u_postal_code = u_postal_code if u_postal_code else session.get_param( u_postal_code).decode() if not u_postal_code: #gather Postal Code resp = twilio_response.GatherSpeech() resp.text = "I could not recognize any postal code. Please try again" url = '/api/reset-password/gather-postal_code' if payload: url = url + '?' + urllib.parse.urlencode(payload) resp.url = url log(caller, 'bot', resp.text, uid) return str(resp.twiml) spaced_postal_code = ''.join([i + ' ' for i in u_postal_code]) if postal_code == u_postal_code: # redirect to reset-ad-password resp = twilio_response.Redirect() resp.text = "Yaaaay! your portal code {} is correct".format( spaced_postal_code) url = '/api/reset-password/reset-ad-password' if payload: url = url + '?' + urllib.parse.urlencode(payload) resp.url = url log(caller, 'bot', resp.text, uid) return str(resp.twiml) else: resp = twilio_response.GatherSpeech() resp.text = "Your postal code {} does not match with active directory data. Please try again".format( spaced_postal_code) url = '/api/reset-password/gather-postal-code' if payload: url = url + '?' + urllib.parse.urlencode(payload) resp.url = url log(caller, 'bot', resp.text, uid) return str(resp.twiml)
def gather_city(): data = request.form.get('SpeechResult',None) caller = request.form.get('Caller') uid = request.form.get('CallSid') payload = request.args.items() payload = { x[0]:x[1] for x in payload} session = Conversation.from_uid(uid) u_city = payload.get('u_city',None) u_city = u_city if u_city else session.get_param('u_city').decode() print("AD city = "+str(u_city)) ntry = int(request.args.get('ntry','0')) if ntry > 4: text = "Maximum Number of Retries reached. Contecting to Service Desk. Please Wait" resp = twilio_response.Hangup(text=text) return str(resp.twiml) ntry += 1 if payload.get('ntry'): del payload['ntry'] if not data: text = "I didn't quite catch that. Please enter your city again." url = '/api/reset-password/gather-city?ntry={}'.format(str(ntry)) if payload: url = url + '&' + urllib.parse.urlencode(payload) resp = twilio_response.GatherSpeech(text, url) log(caller,'bot', resp.text, uid) return str(resp.twiml) if data: print("data = " + data) log(caller,caller, data, uid) gather_city, text = parseCity(data) print("parsed city = "+str(gather_city)) if gather_city is None: url = '/api/reset-password/gather-city?ntry={}'.format(str(ntry)) if payload: url = url + '&' + urllib.parse.urlencode(payload) resp = twilio_response.GatherSpeech(text, url) log(caller,'bot', resp.text, uid) return str(resp.twiml) if gather_city.lower() == u_city.lower(): if payload.get('luis_phone_number'): resp = twilio_response.Redirect() resp.text = "You city, {} is correct. Now I will be validating your phone number".format(u_city) url = '/api/reset-passord/check-phone-number?phone_number={}'.format(payload.get('luis_phone_number')) if payload: url = url + '&' + urllib.parse.urlencode(payload) resp.url = url log(caller,'bot', resp.text, uid) return str(resp.twiml) else: resp = twilio_response.GatherSpeech() resp.text = "You city, , {} is correct. Please tell me your 10 digit contact Number".format(u_city) url = '/api/reset-password/gather-phone-number' if payload: url = url + '?' + urllib.parse.urlencode(payload) resp.url = url log(caller,'bot', resp.text, uid) return str(resp.twiml) else: text = "Your city did not match with active directory data. Please enter you city again" url = '/api/reset-password/gather-city?ntry={}'.format(str(ntry)) if payload: url = url + '&' + urllib.parse.urlencode(payload) resp = twilio_response.GatherSpeech(text, url) log(caller,'bot', resp.text, uid) return str(resp.twiml)
def gather_contact_number(): user_speech = request.form.get('SpeechResult',None) caller = request.form.get('Caller') input_digits = request.form.get('Digits', None) data = input_digits or user_speech uid = request.form.get('CallSid') payload = request.args.items() payload = { x[0]:x[1] for x in payload} session = Conversation.from_uid(uid) u_phone_number = payload.get('u_phone_number', None) u_phone_number = u_phone_number if u_phone_number else session.get_param('u_phone_number').decode() print("u_phone_number = "+ str(u_phone_number)) ntry = int(request.args.get('ntry','0')) if ntry > 4: text = "Maximum Number of Retries reached. Contecting to Service Desk. Please Wait" resp = twilio_response.Hangup(text=text) return str(resp.twiml) ntry += 1 if payload.get('ntry'): del payload['ntry'] if not data: text = "I didn't quite catch that. Please enter your 10 digit phone number again." url = '/api/reset-password/gather-phone-number?ntry={}'.format(str(ntry)) if payload: url = url + '&' + urllib.parse.urlencode(payload) resp = twilio_response.GatherSpeech(text, url) log(caller,'bot', resp.text, uid) return str(resp.twiml) if data: print("data = " + data) log(caller,'caller', data, uid) gather_contact, text = parseContact(data) print("gather_contact = "+ str(gather_contact)) if gather_contact is None: url = '/api/reset-password/gather-phone-number?ntry={}'.format(str(ntry)) if payload: url += '&' + urllib.parse.urlencode(payload) resp = twilio_response.GatherSpeech(text, url) log(caller,'bot', resp.text, uid) return str(resp.twiml) spaced_contact_number = ''.join([i + ' ' for i in u_phone_number]) if str(gather_contact).lower() == u_phone_number.lower(): if payload.get('luis_postal_code'): resp = twilio_response.Redirect() spaced_contact_number = ''.join([i + ' ' for i in u_phone_number]) resp.text = "You contact number, {} is correct. Now I will be validating postal code".format(spaced_contact_number) url = '/api/reset-password/check-postal-code?postal_code={}'.format(payload.get('luis_postal_code')) if payload: url = url + '&' + urllib.parse.urlencode(payload) resp.url = url log(caller,'bot', resp.text, uid) return str(resp.twiml) else: resp = twilio_response.GatherSpeech() resp.text = "You contact number {} is correct. Please tell me your postal code".format(spaced_contact_number) url = '/api/reset-password/gather-postal-code' if payload: url = url + '?' + urllib.parse.urlencode(payload) resp.url = url log(caller,'bot', resp.text, uid) return str(resp.twiml) else: text = "Your phone number {} did not match with active directory data. Please enter it again".format(spaced_contact_number) url = '/api/reset-password/gather-phone-number?ntry={}'.format(str(ntry)) if payload: url = url + '&' + urllib.parse.urlencode(payload) resp = twilio_response.GatherSpeech(text, url) log(caller,'bot', resp.text, uid) return str(resp.twiml)
def gather_portal_id(): uid = request.form.get("CallSid") caller = request.form.get('Caller') session = Conversation.from_uid(uid) user_speech = request.form.get('SpeechResult', None) input_digits = request.form.get('Digits', None) data = input_digits or user_speech ad_error = request.args.get('ad_error', False) ntry = int(request.args.get('ntry', '0')) invalid = request.args.get('invalid', False) if ntry > 4: text = "Maximum Number of Retries reached. Contecting to Service Desk. Please Wait" resp = twilio_response.Hangup(text=text) return str(resp.twiml) ntry += 1 luis_portal_id = session.get_param("luis_portal_id") if luis_portal_id: luis_portal_id = luis_portal_id.decode() resp = twilio_response.Redirect() resp.url = '/api/reset-password/check-portal-id?portalId={}&ntry={}'.format( str(luis_portal_id), str(ntry)) return str(resp.twiml) if ad_error: text = request.args.get( 'error_text', 'Something went wrong while feteching data from AD.') text += 'Please enter your 6 digit portal id again!' url = '/api/reset-password/gather-portal-id?ntry={}'.format(str(ntry)) resp = twilio_response.GatherSpeech(text, url) log(caller, 'bot', resp.text, uid) return str(resp.twiml) if not data: text = "I didn't quite catch that. Please enter your portal ID again." url = '/api/reset-password/gather-portal-id?ntry={}'.format(str(ntry)) resp = twilio_response.GatherSpeech(text, url) log(caller, 'bot', resp.text, uid) return str(resp.twiml) if data: print("data = " + data) log(caller, caller, data, uid) portalId, text = parsePortalId(data) print(portalId) if portalId is None: url = '/api/reset-password/gather-portal-id?ntry={}'.format( str(ntry)) resp = twilio_response.GatherSpeech(text, url) log(caller, 'bot', resp.text, uid) return str(resp.twiml) resp = twilio_response.Redirect(text=text) url = '/api/reset-password/check-portal-id?portalId={}&ntry={}'.format( str(portalId), str(ntry)) resp.url = url log(caller, 'bot', resp.text, uid) return str(resp.twiml)