def begin(request, response): """ Begin makes sure we have a caller with SOME identification. If we can't detect the number we just ask them for one. """ r = request.REQUEST call_from = r.get('From', None) if request.method == 'POST': call_from = r.get('Digits', None) #Digits from <Gather> log.debug('Initialize Call from: %s' % (call_from or 'Unknown')) if call_from is None: with response.gather(method='POST', timeout=10, numDigits=10) as g: g.say('Enter your ten digit US phone number starting with area code.', voice='woman') else: try: #This block will move into begin() when a valid phone is required call_from = convert_to_e164(call_from).strip('+') #The + causes URL trouble. except NumberParseException: log.warning('Failed to convert phone number %s' % call_from) #response.hangup() #Right now I'm not requiring an ACTUAL phone number. response.redirect(reverse(init, args=[call_from]), method='POST') return response
def lookup(request, response): """ Lookup will find the digits from either the FROM or DIGITS variables and check the new caller to see if they exist in our database and if not direct them to the correct location. """ # in begin() if digits is None: log.debug('No digits passed to lookup') response.hangup() else: try: #This block will move into begin() when a valid phone is required phone_number = convert_to_e164(digits).strip('+') #The + causes URL trouble. except NumberParseException: log.critical('Failed to convert phone number %s' % digits) phone_number = digits #response.hangup() #Right now I'm not requiring an ACTUAL phone number. try: caller = Caller.objects.get(number=phone_number) response.redirect(self.reverse_url('verify', caller.number), method='GET') except Caller.DoesNotExist: response.redirect(reverse(account, phone_number), method='GET') return response
def get(self, *a, **kw): response = twiml.Response() digits = self.get_argument('Digits', self.get_argument('From', None)) if digits is None: logger.debug('No digits passed to lookup') response.hangup() else: try: phone_number = convert_to_e164(digits).strip('+') #The + causes URL trouble. except NumberParseException: logger.debug('Failed to convert phone number %s' % digits) phone_number = digits #response.hangup() caller = db.callers.find_one({'number': phone_number}) if caller is None: response.redirect(self.reverse_url('new', phone_number)) else: response.redirect(self.reverse_url('verify', caller['number']), method='GET') return response