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 _forwardToSms(self, to, messageBody): toName = to.split("@")[0] contact = Contact.getByName(toName) if not contact: raise InvalidParametersException("Unknown contact " + toName) toNumber, body = self._getNumberAndBody(contact, messageBody) self._communications.sendSMS(self._owner.phoneNumber, toNumber, body) self._log(LogItem.FROM_OWNER, contact, body)
def post(self, subscriptionType): sender = self.request.get('from').split('/')[0] to = self.request.get('to').split('/')[0] contactName = to.split('@')[0] contact = Contact.getByName(contactName) if not contact: logging.error("Got subscription type " + subscriptionType + " for unknown contact " + to) else: logging.info("Got subscription type " + subscriptionType + " from " + sender + " to " + to) if subscriptionType.startswith("un"): contact.subscribed = False else: contact.subscribed = True Contact.update(contact)
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'}))