def send(message, numbers): if numbers == ALL_SUBSCRIBERS: numbers = subscribe.get_phone_numbers() if isinstance(numbers, basestring): numbers = [numbers] clean_numbers = [] for number in numbers: number = ''.join(n for n in number if n in string.digits) if len(number) == 10: # assume at least american.. number = '1' + number clean_numbers.append(number) registry = getUtility(IRegistry) auth_id = registry.get('castle.plivo_auth_id') auth_token = registry.get('castle.plivo_auth_token') src = registry.get('castle.plivo_phone_number') src = ''.join(n for n in src if n in string.digits) params = {'src': src, 'dst': '<'.join(clean_numbers), 'text': message} resp = requests.post('https://api.plivo.com/v1/Account/%s/Message/' % auth_id, data=json.dumps(params), headers={'Content-Type': 'application/json'}, auth=(auth_id, auth_token)) if resp.status_code in (202, 200): try: return 'error' not in resp.json() except: return False else: return False
def test_get_phone_numbers(self): data = subscribe.register('*****@*****.**', { 'foo': 'bar', 'phone_number': '15555555555' }) subscribe.confirm('*****@*****.**', data['code']) subscribe.confirm_phone_number('*****@*****.**', data['code']) self.assertEquals(len(subscribe.get_phone_numbers()), 1)