def claim_number(self, user, phone_number, country, role): auth_id = self.request.session.get(Channel.CONFIG_PLIVO_AUTH_ID, None) auth_token = self.request.session.get(Channel.CONFIG_PLIVO_AUTH_TOKEN, None) org = user.get_org() plivo_uuid = generate_uuid() callback_domain = org.get_brand_domain() app_name = "%s/%s" % (callback_domain.lower(), plivo_uuid) message_url = "https://" + callback_domain + "%s" % reverse('handlers.plivo_handler', args=['receive', plivo_uuid]) answer_url = "https://" + settings.AWS_BUCKET_DOMAIN + "/plivo_voice_unavailable.xml" headers = http_headers(extra={'Content-Type': "application/json"}) create_app_url = "https://api.plivo.com/v1/Account/%s/Application/" % auth_id response = requests.post(create_app_url, json=dict(app_name=app_name, answer_url=answer_url, message_url=message_url), headers=headers, auth=(auth_id, auth_token)) if response.status_code in [201, 200, 202]: plivo_app_id = response.json()['app_id'] else: # pragma: no cover plivo_app_id = None plivo_config = {Channel.CONFIG_PLIVO_AUTH_ID: auth_id, Channel.CONFIG_PLIVO_AUTH_TOKEN: auth_token, Channel.CONFIG_PLIVO_APP_ID: plivo_app_id, Channel.CONFIG_CALLBACK_DOMAIN: org.get_brand_domain()} plivo_number = phone_number.strip('+ ').replace(' ', '') response = requests.get("https://api.plivo.com/v1/Account/%s/Number/%s/" % (auth_id, plivo_number), headers=headers, auth=(auth_id, auth_token)) if response.status_code != 200: response = requests.post("https://api.plivo.com/v1/Account/%s/PhoneNumber/%s/" % (auth_id, plivo_number), headers=headers, auth=(auth_id, auth_token)) if response.status_code != 201: # pragma: no cover raise Exception(_("There was a problem claiming that number, please check the balance on your account.")) response = requests.get("https://api.plivo.com/v1/Account/%s/Number/%s/" % (auth_id, plivo_number), headers=headers, auth=(auth_id, auth_token)) if response.status_code == 200: response = requests.post("https://api.plivo.com/v1/Account/%s/Number/%s/" % (auth_id, plivo_number), json=dict(app_id=plivo_app_id), headers=headers, auth=(auth_id, auth_token)) if response.status_code != 202: # pragma: no cover raise Exception(_("There was a problem updating that number, please try again.")) phone_number = '+' + plivo_number phone = phonenumbers.format_number(phonenumbers.parse(phone_number, None), phonenumbers.PhoneNumberFormat.NATIONAL) channel = Channel.create(org, user, country, 'PL', name=phone, address=phone_number, config=plivo_config, uuid=plivo_uuid) analytics.track(user.username, 'temba.channel_claim_plivo', dict(number=phone_number)) return channel
def claim_number(self, user, phone_number, country, role): auth_id = self.request.session.get(Channel.CONFIG_PLIVO_AUTH_ID, None) auth_token = self.request.session.get(Channel.CONFIG_PLIVO_AUTH_TOKEN, None) org = user.get_org() plivo_uuid = generate_uuid() app_name = "%s/%s" % (settings.TEMBA_HOST.lower(), plivo_uuid) client = plivo.RestAPI(auth_id, auth_token) message_url = "https://" + settings.TEMBA_HOST + "%s" % reverse('handlers.plivo_handler', args=['receive', plivo_uuid]) answer_url = "https://" + settings.AWS_BUCKET_DOMAIN + "/plivo_voice_unavailable.xml" plivo_response_status, plivo_response = client.create_application(params=dict(app_name=app_name, answer_url=answer_url, message_url=message_url)) if plivo_response_status in [201, 200, 202]: plivo_app_id = plivo_response['app_id'] else: # pragma: no cover plivo_app_id = None plivo_config = {Channel.CONFIG_PLIVO_AUTH_ID: auth_id, Channel.CONFIG_PLIVO_AUTH_TOKEN: auth_token, Channel.CONFIG_PLIVO_APP_ID: plivo_app_id} plivo_number = phone_number.strip('+ ').replace(' ', '') plivo_response_status, plivo_response = client.get_number(params=dict(number=plivo_number)) if plivo_response_status != 200: plivo_response_status, plivo_response = client.buy_phone_number(params=dict(number=plivo_number)) if plivo_response_status != 201: # pragma: no cover raise Exception(_("There was a problem claiming that number, please check the balance on your account.")) plivo_response_status, plivo_response = client.get_number(params=dict(number=plivo_number)) if plivo_response_status == 200: plivo_response_status, plivo_response = client.modify_number(params=dict(number=plivo_number, app_id=plivo_app_id)) if plivo_response_status != 202: # pragma: no cover raise Exception(_("There was a problem updating that number, please try again.")) phone_number = '+' + plivo_number phone = phonenumbers.format_number(phonenumbers.parse(phone_number, None), phonenumbers.PhoneNumberFormat.NATIONAL) channel = Channel.create(org, user, country, 'PL', name=phone, address=phone_number, config=plivo_config, uuid=plivo_uuid) analytics.track(user.username, 'temba.channel_claim_plivo', dict(number=phone_number)) return channel
def claim_number(self, user, phone_number, country, role): auth_id = self.request.session.get(Channel.CONFIG_PLIVO_AUTH_ID, None) auth_token = self.request.session.get(Channel.CONFIG_PLIVO_AUTH_TOKEN, None) org = user.get_org() plivo_uuid = generate_uuid() callback_domain = org.get_brand_domain() app_name = "%s/%s" % (callback_domain.lower(), plivo_uuid) message_url = f"https://{callback_domain}{reverse('courier.pl', args=[plivo_uuid, 'receive'])}" answer_url = f"{settings.STORAGE_URL}/plivo_voice_unavailable.xml" headers = http_headers(extra={"Content-Type": "application/json"}) create_app_url = "https://api.plivo.com/v1/Account/%s/Application/" % auth_id response = requests.post( create_app_url, json=dict(app_name=app_name, answer_url=answer_url, message_url=message_url), headers=headers, auth=(auth_id, auth_token), ) if response.status_code in [201, 200, 202]: plivo_app_id = response.json()["app_id"] else: # pragma: no cover plivo_app_id = None plivo_config = { Channel.CONFIG_PLIVO_AUTH_ID: auth_id, Channel.CONFIG_PLIVO_AUTH_TOKEN: auth_token, Channel.CONFIG_PLIVO_APP_ID: plivo_app_id, Channel.CONFIG_CALLBACK_DOMAIN: org.get_brand_domain(), } plivo_number = phone_number.strip("+ ").replace(" ", "") response = requests.get( "https://api.plivo.com/v1/Account/%s/Number/%s/" % (auth_id, plivo_number), headers=headers, auth=(auth_id, auth_token), ) if response.status_code != 200: response = requests.post( "https://api.plivo.com/v1/Account/%s/PhoneNumber/%s/" % (auth_id, plivo_number), headers=headers, auth=(auth_id, auth_token), ) if response.status_code != 201: # pragma: no cover raise Exception( _("There was a problem claiming that number, please check the balance on your account.") ) response = requests.get( "https://api.plivo.com/v1/Account/%s/Number/%s/" % (auth_id, plivo_number), headers=headers, auth=(auth_id, auth_token), ) if response.status_code == 200: response = requests.post( "https://api.plivo.com/v1/Account/%s/Number/%s/" % (auth_id, plivo_number), json=dict(app_id=plivo_app_id), headers=headers, auth=(auth_id, auth_token), ) if response.status_code != 202: # pragma: no cover raise Exception(_("There was a problem updating that number, please try again.")) phone_number = "+" + plivo_number phone = phonenumbers.format_number( phonenumbers.parse(phone_number, None), phonenumbers.PhoneNumberFormat.NATIONAL ) channel = Channel.create( org, user, country, "PL", name=phone, address=phone_number, config=plivo_config, uuid=plivo_uuid ) return channel
def populate_uuid(apps, schema_editor): ExportContactsTask = apps.get_model('contacts', 'ExportContactsTask') for task in ExportContactsTask.objects.filter(uuid=None): task.uuid = generate_uuid() task.save(update_fields=('uuid', ))
def claim_number(self, user, phone_number, country, role): org = user.get_org() client = org.get_nexmo_client() org_config = org.config nexmo_phones = client.get_numbers(phone_number) is_shortcode = False # try it with just the national code (for short codes) if not nexmo_phones: parsed = phonenumbers.parse(phone_number, None) shortcode = str(parsed.national_number) nexmo_phones = client.get_numbers(shortcode) if nexmo_phones: is_shortcode = True phone_number = shortcode # buy the number if we have to if not nexmo_phones: try: client.buy_number(country, phone_number) nexmo_phones = client.get_numbers(phone_number) except Exception as e: raise Exception( _("There was a problem claiming that number, please check the balance on your account. " "Note that you can only claim numbers after adding credit to your Nexmo account." ) + "\n" + str(e)) # what does this number support? features = [elt.upper() for elt in nexmo_phones[0]["features"]] supports_msgs = "SMS" in features supports_voice = "VOICE" in features role = "" if supports_msgs: role += Channel.ROLE_SEND + Channel.ROLE_RECEIVE if supports_voice: role += Channel.ROLE_ANSWER + Channel.ROLE_CALL channel_uuid = generate_uuid() callback_domain = org.get_brand_domain() receive_url = "https://" + callback_domain + reverse( "courier.nx", args=[channel_uuid, "receive"]) # if it supports voice, create new Nexmo voice app for this number if supports_voice: app_id, app_private_key = client.create_application( org.get_brand_domain(), channel_uuid) else: app_id = None app_private_key = None # update the delivery URLs for it try: client.update_number(country, phone_number, receive_url, app_id) except Exception as e: # pragma: no cover # shortcodes don't seem to claim right on nexmo, move forward anyways if not is_shortcode: raise Exception( _("There was a problem claiming that number, please check the balance on your account." ) + "\n" + str(e)) if is_shortcode: phone = phone_number nexmo_phone_number = phone_number else: parsed = phonenumbers.parse(phone_number, None) phone = phonenumbers.format_number( parsed, phonenumbers.PhoneNumberFormat.INTERNATIONAL) # nexmo ships numbers around as E164 without the leading + nexmo_phone_number = phonenumbers.format_number( parsed, phonenumbers.PhoneNumberFormat.E164).strip("+") config = { Channel.CONFIG_NEXMO_APP_ID: app_id, Channel.CONFIG_NEXMO_APP_PRIVATE_KEY: app_private_key, Channel.CONFIG_NEXMO_API_KEY: org_config[Org.CONFIG_NEXMO_KEY], Channel.CONFIG_NEXMO_API_SECRET: org_config[Org.CONFIG_NEXMO_SECRET], Channel.CONFIG_CALLBACK_DOMAIN: callback_domain, } channel = Channel.create( org, user, country, "NX", name=phone, address=phone_number, role=role, config=config, bod=nexmo_phone_number, uuid=channel_uuid, tps=1, ) analytics.track(user.username, "temba.channel_claim_nexmo", dict(number=phone_number)) return channel
def claim_number(self, user, phone_number, country, role): org = user.get_org() client = org.get_nexmo_client() org_config = org.config app_id = org_config.get(NEXMO_APP_ID) nexmo_phones = client.get_numbers(phone_number) is_shortcode = False # try it with just the national code (for short codes) if not nexmo_phones: parsed = phonenumbers.parse(phone_number, None) shortcode = str(parsed.national_number) nexmo_phones = client.get_numbers(shortcode) if nexmo_phones: is_shortcode = True phone_number = shortcode # buy the number if we have to if not nexmo_phones: try: client.buy_nexmo_number(country, phone_number) except Exception as e: raise Exception( _("There was a problem claiming that number, " "please check the balance on your account. " + "Note that you can only claim numbers after " "adding credit to your Nexmo account.") + "\n" + str(e)) channel_uuid = generate_uuid() callback_domain = org.get_brand_domain() new_receive_url = "https://" + callback_domain + reverse( 'courier.nx', args=[channel_uuid, 'receive']) nexmo_phones = client.get_numbers(phone_number) features = [elt.upper() for elt in nexmo_phones[0]['features']] role = '' if 'SMS' in features: role += Channel.ROLE_SEND + Channel.ROLE_RECEIVE if 'VOICE' in features: role += Channel.ROLE_ANSWER + Channel.ROLE_CALL # update the delivery URLs for it try: client.update_nexmo_number(country, phone_number, new_receive_url, app_id) except Exception as e: # pragma: no cover # shortcodes don't seem to claim right on nexmo, move forward anyways if not is_shortcode: raise Exception( _("There was a problem claiming that number, please check the balance on your account." ) + "\n" + str(e)) if is_shortcode: phone = phone_number nexmo_phone_number = phone_number else: parsed = phonenumbers.parse(phone_number, None) phone = phonenumbers.format_number( parsed, phonenumbers.PhoneNumberFormat.INTERNATIONAL) # nexmo ships numbers around as E164 without the leading + nexmo_phone_number = phonenumbers.format_number( parsed, phonenumbers.PhoneNumberFormat.E164).strip('+') config = { Channel.CONFIG_NEXMO_APP_ID: app_id, Channel.CONFIG_NEXMO_APP_PRIVATE_KEY: org_config[NEXMO_APP_PRIVATE_KEY], Channel.CONFIG_NEXMO_API_KEY: org_config[NEXMO_KEY], Channel.CONFIG_NEXMO_API_SECRET: org_config[NEXMO_SECRET], Channel.CONFIG_CALLBACK_DOMAIN: callback_domain } channel = Channel.create(org, user, country, 'NX', name=phone, address=phone_number, role=role, config=config, bod=nexmo_phone_number, uuid=channel_uuid, tps=1) analytics.track(user.username, 'temba.channel_claim_nexmo', dict(number=phone_number)) return channel
def claim_number(self, user, phone_number, country, role): auth_id = self.request.session.get(Channel.CONFIG_PLIVO_AUTH_ID, None) auth_token = self.request.session.get(Channel.CONFIG_PLIVO_AUTH_TOKEN, None) org = user.get_org() plivo_uuid = generate_uuid() callback_domain = org.get_brand_domain() app_name = "%s/%s" % (callback_domain.lower(), plivo_uuid) message_url = "https://" + callback_domain + "%s" % reverse("courier.pl", args=[plivo_uuid, "receive"]) answer_url = "https://" + settings.TEMBA_HOST+settings.MEDIA_URL+ "plivo_voice_unavailable.xml" headers = http_headers(extra={"Content-Type": "application/json"}) create_app_url = "https://api.plivo.com/v1/Account/%s/Application/" % auth_id response = requests.post( create_app_url, json=dict(app_name=app_name, answer_url=answer_url, message_url=message_url), headers=headers, auth=(auth_id, auth_token), ) if response.status_code in [201, 200, 202]: plivo_app_id = response.json()["app_id"] else: # pragma: no cover plivo_app_id = None plivo_config = { Channel.CONFIG_PLIVO_AUTH_ID: auth_id, Channel.CONFIG_PLIVO_AUTH_TOKEN: auth_token, Channel.CONFIG_PLIVO_APP_ID: plivo_app_id, Channel.CONFIG_CALLBACK_DOMAIN: org.get_brand_domain(), } plivo_number = phone_number.strip("+ ").replace(" ", "") response = requests.get( "https://api.plivo.com/v1/Account/%s/Number/%s/" % (auth_id, plivo_number), headers=headers, auth=(auth_id, auth_token), ) if response.status_code != 200: response = requests.post( "https://api.plivo.com/v1/Account/%s/PhoneNumber/%s/" % (auth_id, plivo_number), headers=headers, auth=(auth_id, auth_token), ) if response.status_code != 201: # pragma: no cover raise Exception( _("There was a problem claiming that number, please check the balance on your account.") ) response = requests.get( "https://api.plivo.com/v1/Account/%s/Number/%s/" % (auth_id, plivo_number), headers=headers, auth=(auth_id, auth_token), ) if response.status_code == 200: response = requests.post( "https://api.plivo.com/v1/Account/%s/Number/%s/" % (auth_id, plivo_number), json=dict(app_id=plivo_app_id), headers=headers, auth=(auth_id, auth_token), ) if response.status_code != 202: # pragma: no cover raise Exception(_("There was a problem updating that number, please try again.")) phone_number = "+" + plivo_number phone = phonenumbers.format_number( phonenumbers.parse(phone_number, None), phonenumbers.PhoneNumberFormat.NATIONAL ) channel = Channel.create( org, user, country, "PL", name=phone, address=phone_number, config=plivo_config, uuid=plivo_uuid ) analytics.track(user.username, "temba.channel_claim_plivo", dict(number=phone_number)) return channel
def populate_uuid(apps, schema_editor): ExportMessagesTask = apps.get_model("msgs", "ExportMessagesTask") for task in ExportMessagesTask.objects.filter(uuid=None): task.uuid = generate_uuid() task.save(update_fields=("uuid", ))
def populate_campaign_uuid(apps, schema_editor): model = apps.get_model("campaigns", "Campaign") for obj in model.objects.all(): obj.uuid = generate_uuid() obj.save(update_fields=('uuid',))
def claim_number(self, user, phone_number, country, role): org = user.get_org() client = org.get_nexmo_client() org_config = org.config app_id = org_config.get(NEXMO_APP_ID) nexmo_phones = client.get_numbers(phone_number) is_shortcode = False # try it with just the national code (for short codes) if not nexmo_phones: parsed = phonenumbers.parse(phone_number, None) shortcode = str(parsed.national_number) nexmo_phones = client.get_numbers(shortcode) if nexmo_phones: is_shortcode = True phone_number = shortcode # buy the number if we have to if not nexmo_phones: try: client.buy_nexmo_number(country, phone_number) except Exception as e: raise Exception( _( "There was a problem claiming that number, " "please check the balance on your account. " + "Note that you can only claim numbers after " "adding credit to your Nexmo account." ) + "\n" + str(e) ) channel_uuid = generate_uuid() callback_domain = org.get_brand_domain() new_receive_url = "https://" + callback_domain + reverse("courier.nx", args=[channel_uuid, "receive"]) nexmo_phones = client.get_numbers(phone_number) features = [elt.upper() for elt in nexmo_phones[0]["features"]] role = "" if "SMS" in features: role += Channel.ROLE_SEND + Channel.ROLE_RECEIVE if "VOICE" in features: role += Channel.ROLE_ANSWER + Channel.ROLE_CALL # update the delivery URLs for it try: client.update_nexmo_number(country, phone_number, new_receive_url, app_id) except Exception as e: # pragma: no cover # shortcodes don't seem to claim right on nexmo, move forward anyways if not is_shortcode: raise Exception( _("There was a problem claiming that number, please check the balance on your account.") + "\n" + str(e) ) if is_shortcode: phone = phone_number nexmo_phone_number = phone_number else: parsed = phonenumbers.parse(phone_number, None) phone = phonenumbers.format_number(parsed, phonenumbers.PhoneNumberFormat.INTERNATIONAL) # nexmo ships numbers around as E164 without the leading + nexmo_phone_number = phonenumbers.format_number(parsed, phonenumbers.PhoneNumberFormat.E164).strip("+") config = { Channel.CONFIG_NEXMO_APP_ID: app_id, Channel.CONFIG_NEXMO_APP_PRIVATE_KEY: org_config[NEXMO_APP_PRIVATE_KEY], Channel.CONFIG_NEXMO_API_KEY: org_config[NEXMO_KEY], Channel.CONFIG_NEXMO_API_SECRET: org_config[NEXMO_SECRET], Channel.CONFIG_CALLBACK_DOMAIN: callback_domain, } channel = Channel.create( org, user, country, "NX", name=phone, address=phone_number, role=role, config=config, bod=nexmo_phone_number, uuid=channel_uuid, tps=1, ) analytics.track(user.username, "temba.channel_claim_nexmo", dict(number=phone_number)) return channel
def populate_campaign_uuid(apps, schema_editor): model = apps.get_model("campaigns", "Campaign") for obj in model.objects.all(): obj.uuid = generate_uuid() obj.save(update_fields=('uuid', ))
def populate_flowlabel_uuid(apps, schema_editor): FlowLabel = apps.get_model('flows', 'FlowLabel') for label in FlowLabel.objects.all(): label.uuid = generate_uuid() label.save(update_fields=('uuid',))
def claim_number(self, user, phone_number, country, role): org = user.get_org() client = org.get_nexmo_client() org_config = org.config_json() org_uuid = org_config.get(NEXMO_UUID) app_id = org_config.get(NEXMO_APP_ID) nexmo_phones = client.get_numbers(phone_number) is_shortcode = False # try it with just the national code (for short codes) if not nexmo_phones: parsed = phonenumbers.parse(phone_number, None) shortcode = str(parsed.national_number) nexmo_phones = client.get_numbers(shortcode) if nexmo_phones: is_shortcode = True phone_number = shortcode # buy the number if we have to if not nexmo_phones: try: client.buy_nexmo_number(country, phone_number) except Exception as e: raise Exception(_("There was a problem claiming that number, " "please check the balance on your account. " + "Note that you can only claim numbers after " "adding credit to your Nexmo account.") + "\n" + str(e)) mo_path = reverse('handlers.nexmo_handler', args=['receive', org_uuid]) channel_uuid = generate_uuid() nexmo_phones = client.get_numbers(phone_number) features = [elt.upper() for elt in nexmo_phones[0]['features']] role = '' if 'SMS' in features: role += Channel.ROLE_SEND + Channel.ROLE_RECEIVE if 'VOICE' in features: role += Channel.ROLE_ANSWER + Channel.ROLE_CALL # update the delivery URLs for it from temba.settings import TEMBA_HOST try: client.update_nexmo_number(country, phone_number, 'https://%s%s' % (TEMBA_HOST, mo_path), app_id) except Exception as e: # pragma: no cover # shortcodes don't seem to claim right on nexmo, move forward anyways if not is_shortcode: raise Exception(_("There was a problem claiming that number, please check the balance on your account.") + "\n" + str(e)) if is_shortcode: phone = phone_number nexmo_phone_number = phone_number else: parsed = phonenumbers.parse(phone_number, None) phone = phonenumbers.format_number(parsed, phonenumbers.PhoneNumberFormat.INTERNATIONAL) # nexmo ships numbers around as E164 without the leading + nexmo_phone_number = phonenumbers.format_number(parsed, phonenumbers.PhoneNumberFormat.E164).strip('+') channel = Channel.create(org, user, country, 'NX', name=phone, address=phone_number, role=role, bod=nexmo_phone_number, uuid=channel_uuid) analytics.track(user.username, 'temba.channel_claim_nexmo', dict(number=phone_number)) return channel