def post(self): user = json.loads(self.request.body) if not phonenumberutils.validateNumber(user['phoneNumber']): raise errors.ValidationError("Invalid phone number.") #return validationError(self.response, 'Invalid number ' + user['phoneNumber']) if not user['name']: raise errors.ValidationError("Name is required.") # Make sure we're not duplicating another contact existingContact = Contact.getByName(user['name']) if existingContact: raise errors.ValidationError('User already exists with name ' + user['name']) existingContact = Contact.getByPhoneNumber(user['phoneNumber']) if existingContact: raise errors.ValidationError('User ' + existingContact.name + ' already exists with number ' + existingContact.phoneNumber) logging.info("Creating contact " + user["name"]) contact = Contact( name = user['name'].lower(), phoneNumber = phonenumberutils.toPrettyNumber(user['phoneNumber']), normalizedPhoneNumber = phonenumberutils.toNormalizedNumber(user['phoneNumber'])) Contact.update(contact) xmppVoiceMail.sendXmppInvite(contact.name) self.response.headers['Content-Type'] = 'application/json' self.response.write(json.dumps(contact.toDict()))
def _getNumberAndBody(self, contact, body): """Get the destination contact and the message body from a message. The message will be either to the default sender, in which case the body will be of the format "number:message", or else the message will be to a nickname, and the body will just be the message. This returns the tuple (toNumber, body), where toNumber is the SMS number to send this message to, and body is the message content. Raises InvalidParametersException if there are any errors in the input. """ if not contact.isDefaultSender(): toNumber = contact.normalizedPhoneNumber else: # Parse the phone number and body out of the message match = self._messageRegex.match(body) if not match: raise InvalidParametersException("Use 'number:message' to send an SMS.") toNumber = match.group(1) if not validateNumber(toNumber): raise InvalidParametersException("Invalid number: " + match.group(1)) toNumber = toNormalizedNumber(toNumber) body = match.group(2).strip() return (toNumber, body)
def createContact(self, subscribed): # Create a known contact c = Contact( name="mrtest", phoneNumber=self.contactNumber, normalizedPhoneNumber=phonenumberutils.toNormalizedNumber(self.contactNumber), subscribed=subscribed, ) Contact.update(c)
def getByPhoneNumber(phoneNumber): normalizedNumber = phonenumberutils.toNormalizedNumber(phoneNumber) # First try to get from memcache answer = _memcache.get(_CONTACT_BY_NUMBER_MEMCACHE_KEY + normalizedNumber) if not answer: # Fall back to the DB q = db.GqlQuery("SELECT * FROM Contact WHERE normalizedPhoneNumber = :1", normalizedNumber) answer = q.get() if answer: answer._addToMemcache() return answer
def post(self): data = json.loads(self.request.body) if not data['message']: raise errors.ValidationError("Please enter a message.") toNumber = None contact = Contact.getByName(data['to']) if contact: toNumber = contact.normalizedPhoneNumber else: if not phonenumberutils.validateNumber(data['to']): raise errors.ValidationError("Invalid phone number.") else: toNumber = phonenumberutils.toNormalizedNumber(data['to']) xmppVoiceMail.sendSMS(contact, toNumber, data['message']) self.response.headers['Content-Type'] = 'application/json' self.response.out.write(json.dumps({'message': 'sent'}))
def update(contact): """ Update or create a Contact in the datastore. """ if contact.is_saved() and contact.isDefaultSender(): _memcache.set(key=_DEFAULT_SENDER_MEMCACHE_KEY, value=contact) # Update the contact in the DB contact.put() else: # Fetch the old contact from the DB oldContact = None if contact.is_saved(): oldContact = Contact.get(contact.key()) # Update the contact in the DB contact.normalizedPhoneNumber = phonenumberutils.toNormalizedNumber(contact.phoneNumber) contact.put() # Remove the old contact from memcache if oldContact: oldContact._removeFromMemcache() # Put the new contact into memcache contact._addToMemcache()
def sendEmailMessageToOwner(self, subject, body=None, fromContact=None, fromNumber=None): if not body: body = "" if not fromContact: fromContact = Contact.getDefaultSender() fromName = fromContact.name if fromNumber: fromAddress = stripNumber(toNormalizedNumber(fromNumber)) elif not fromContact.isDefaultSender(): fromContact.normalizedPhoneNumber else: fromAddress = fromName logging.debug("Sending eMail message to " + self._owner.emailAddress + ": " + subject) fromAddress = '"' + fromName + '" <' + fromAddress + "@" + self._APP_ID + ".appspotmail.com>" self._communications.sendMail( sender=fromAddress, to=self._owner.emailAddress, subject=subject, body=body)