class TwilioClient(object): def __init__(self): self.logger = logging.getLogger("botosan.logger") self.account_sid = os.environ["TWILIO_SID"] self.account_token = os.environ["TWILIO_TOKEN"] self.client = Client(self.account_sid, self.account_token) def get_mcc_and_mnc(self, phone_number): """ Gets the Mobile Country Code and Mobile Network code for a given Twilio Number :param phone_number: The phone number, containing the +CC Number, ex: +12345678901 for the US. :return: a tuple containing the mcc and mnc """ number = self.client.lookups.phone_numbers(phone_number).fetch( type="carrier") self.logger.info(number.carrier['mobile_country_code']) self.logger.info(number.carrier['mobile_network_code']) return number.carrier['mobile_country_code'], number.carrier[ 'mobile_network_code'] def get_available_numbers(self): numbers = self.client.available_phone_numbers("GB").local.list( exclude_local_address_required=True) print(numbers.count()) phone_numbers = [] for number in numbers: phone_numbers.append(number.phone_number) return phone_numbers
def buy_number(self): account_sid = "AC45cfdd30d2d97c83313d5234b0a545fd" auth_token = "0a724904ae5059e6ab4d03fba376638e" client = Client(account_sid, auth_token) numbers = client.available_phone_numbers("US") \ .local \ .list() # Purchase the phone number number = client.incoming_phone_numbers \ .create(phone_number=numbers[0].phone_number) address = 'https://api.twilio.com' + number.uri data = { 'SmsUrl': 'http://54ba138f.ngrok.io/sms', } #create webhook response = requests.post(address, data=data, auth=(account_sid, auth_token)) self.twilio_number = number.phone_number
def create_twilio_application(twilio_account_sid, twilio_auth_token): account_sid = twilio_account_sid auth_token = twilio_auth_token client = Client(account_sid, auth_token) friendly_name = 'spoke{}'.format(str(randrange(1, 99))) inbound_request_url = 'https://{}.herokuapp.com/twilio'.format( session['app_name']) status_callback = 'https://{}.herokuapp.com/twilio-message-report'.format( session['app_name']) fallback_url = status_callback service = client.messaging \ .services \ .create(friendly_name=friendly_name, inbound_request_url=inbound_request_url, status_callback=status_callback, fallback_url=fallback_url, ) numbers = client.available_phone_numbers(country) \ .local \ .list() number = client.incoming_phone_numbers \ .create(phone_number=numbers[0].phone_number) phone_number = client.messaging \ .services(sid=service.sid) \ .phone_numbers \ .create(phone_number_sid=number.sid) return service
def buy_new_phone_number(base_uri, message, number_type): client = Client(settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN) try: available = client.available_phone_numbers("US") \ .local.list(in_locality="New York") parsed = phonenumbers.parse(available[0].phone_number, None) formatted = \ phonenumbers.format_number(parsed, phonenumbers.PhoneNumberFormat.NATIONAL) new_number = client.incoming_phone_numbers \ .local.create(phone_number=available[0].phone_number, friendly_name="Garfield {0} Number - {1}" "".format(number_type, formatted), voice_application_sid=settings.TWILIO_APP_SID, sms_application_sid=settings.TWILIO_APP_SID) except TwilioRestException as e: kwargs = { "from_": settings.TWILIO_PHONE_NUMBER, "to": settings.TWILIO_PHONE_NUMBER, "body": "There was an error purchasing " "your phone number: {0}".format(e.msg) } send_whisper.apply_async(kwargs=kwargs) return False sim = Sim.objects.get(sid=message['From'].replace("sim:", "")) phone_number = PhoneNumber(sid=new_number.sid, account_sid=settings.TWILIO_ACCOUNT_SID, service_sid="None", url=new_number.uri, e164=new_number.phone_number, friendly_name="{0} - {1}".format( number_type, formatted), formatted=formatted, country_code="1", number_type=number_type, related_sim=sim) phone_number.save() send_whisper.apply_async( kwargs={ "from_": phone_number.e164, "to": settings.TWILIO_PHONE_NUMBER, "body": "This is your new {0}" " phone number." "".format(number_type) }) return True
def findAndCreateNum(area): account_sid = 'AC789b798bde070df6992f4f0fba84e89a' auth_token = '54c50792327d057f2847007947f11cc3' client = Client(account_sid, auth_token) numbers = client.available_phone_numbers("US").local.list(area_code=area) number = client.incoming_phone_numbers().create( phone_number=numbers[0].phone_number).update( account_sid="AC789b798bde070df6992f4f0fba84e89a", sms_url="https://testprojectdial.herokuapp.com/sms") print(number.friendly_name) return number
def _twilio_add_new_number_to_proxy_pool(): client = Client(settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN) service = client.proxy.services( settings.TWILIO_PASSENGER_AND_DRIVER_SESSION_ID) available_numbers = client.available_phone_numbers('US').local available_number = available_numbers.list( sms_enabled=True, voice_enabled=True, exclude_all_address_required=True, limit=1)[0] new_number = client.incoming_phone_numbers.create( phone_number=available_number.phone_number) service.phone_numbers.create(phone_number=new_number.phone_number)
def callLegislator(legis_id): politician = "+1" + getlegislators.getNumber(legis_id).replace("-", "") client = Client(ACCOUNT_SID, AUTH_TOKEN) numbers = client.available_phone_numbers("US") \ .local \ .list(area_code="646") number = client.incoming_phone_numbers \ .create(phone_number=numbers[0].phone_number) client.calls.create(to=politician, from_=number, url="http://demo.twilio.com/docs/voice.xml")
def buy_number(): payload = request.get_json() print(current_user) account_sid = payload['twilio_sid'] auth_token = payload['twilio_token'] client = Client(account_sid, auth_token) numbers = client.available_phone_numbers("GB") \ .local \ .list(voice_enabled=True, sms_enabled=True) print(numbers[0].phone_number) new_number = numbers[0].phone_number number = client.incoming_phone_numbers \ .create(phone_number=new_number) return numbers[0].phone_number
def create_twilio_application(twilio_account_sid, twilio_auth_token): account_sid = twilio_account_sid auth_token = twilio_auth_token client = Client(account_sid, auth_token) friendly_name = 'spoke{}'.format(str(randrange(1, 99))) inbound_request_url = 'https://{}.herokuapp.com/twilio'.format( session['app_name']) status_callback = 'https://{}.herokuapp.com/twilio-message-report'.format( session['app_name']) fallback_url = status_callback service = client.messaging \ .services \ .create(friendly_name=friendly_name, inbound_request_url=inbound_request_url, status_callback=status_callback, fallback_url=fallback_url, ) found = False while found == False: numbers = client.available_phone_numbers(country) \ .mobile \ .list() numbers = [ phone for phone in numbers if phone.address_requirements == 'none' ] if len(numbers) > 0: found = True number = client.incoming_phone_numbers \ .create(phone_number=numbers[0].phone_number) phone_number = client.messaging \ .services(sid=service.sid) \ .phone_numbers \ .create(phone_number_sid=number.sid) return service
class TwilioSMSClient(SMSClient): def __init__(self, account_sid=None, auth_token=None, from_number=None, *args, **kwargs): super(TwilioSMSClient, self).__init__(*args, **kwargs) self._account_sid = account_sid self._auth_token = auth_token self._from_number = from_number self._client = Client(account_sid, auth_token) def init_app(self, logger, notify_host, callback_username, callback_password, *args, **kwargs): self.logger = logger self.notify_host = notify_host self.callback_username = callback_username self.callback_password = callback_password @property def name(self): return 'twilio' def get_name(self): return self.name def send_sms(self, to, content, reference, sender=None): # could potentially select from potential numbers like this # from_number = secrets.choice(self._client.incoming_phone_numbers.list()).phone_number start_time = monotonic() from_number = sender or self._from_number callback_url = f'{self.notify_host}/notifications/sms/twilio/{reference}' if self.notify_host else "" try: message = self._client.messages.create( to=to, from_=from_number, body=content, status_callback=callback_url, ) self.logger.info( "Twilio send SMS request for {} succeeded: {}".format( reference, message.sid)) except Exception as e: self.logger.error( "Twilio send SMS request for {} failed".format(reference)) raise e finally: elapsed_time = monotonic() - start_time self.logger.info( "Twilio send SMS request for {} finished in {}".format( reference, elapsed_time)) # Avoid circular imports by importing this file later. from app.models import (NOTIFICATION_SENDING) return message.sid, NOTIFICATION_SENDING def buy_available_phone_number(self, country_code, address_sid): mobile = self._client.available_phone_numbers( country_code).mobile.list( sms_enabled=True, limit=1, ) for record in mobile: incoming_phone_number = self._client.incoming_phone_numbers.create( address_sid=address_sid, phone_number=record.phone_number, sms_url=self.incoming_phone_number_sms_url(), ) return incoming_phone_number return None def update_incoming_phone_number(self, sid): self._client.incoming_phone_numbers(sid).update( sms_url=self.incoming_phone_number_sms_url(), ) def incoming_phone_number_sms_url(self): base = self.notify_host if self.notify_host else "" if base: # Set username and password into the base URL. We make the # assumption here that the base URL does not already include # credentials. scheme = 'https://' if base.startswith('https://') else 'http://' base = base.replace( scheme, '{}{}:{}@'.format( scheme, urllib.parse.quote(self.callback_username), urllib.parse.quote(self.callback_password), )) return "{}/notifications/sms/receive/twilio".format(base)
# Download the Python helper library from twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/user/account account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" auth_token = "your_auth_token" client = Client(account_sid, auth_token) numbers = client.available_phone_numbers("GB") \ .local \ .list(contains="4420") number = client.incoming_phone_numbers \ .create(phone_number=numbers[0].phone_number) print(number.sid)
# Download the Python helper library from twilio.com/docs/python/install import os from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/user/account # To set up environmental variables, see http://twil.io/secure account_sid = os.environ['TWILIO_ACCOUNT_SID'] auth_token = os.environ['TWILIO_AUTH_TOKEN'] client = Client(account_sid, auth_token) numbers = client.available_phone_numbers("US") \ .local \ .list(near_lat_long="37.840699,-122.461853", distance="50", contains="555", in_region="CA") number = client.incoming_phone_numbers \ .create(phone_number=numbers[0].phone_number) print(number.sid)
# Download the Python helper library from twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/user/account account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" auth_token = "your_auth_token" client = Client(account_sid, auth_token) numbers = client.available_phone_numbers("US") \ .local \ .list(near_lat_long="37.840699,-122.461853", distance="50", contains="555", in_region="CA") number = client.incoming_phone_numbers \ .create(phone_number=numbers[0].phone_number) print(number.sid)
# Download the Python helper library from twilio.com/docs/python/install import os from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/user/account # To set up environmental variables, see http://twil.io/secure account_sid = os.environ['TWILIO_ACCOUNT_SID'] auth_token = os.environ['TWILIO_AUTH_TOKEN'] client = Client(account_sid, auth_token) numbers = client.available_phone_numbers("US") \ .local \ .list(contains="STORM") number = client.incoming_phone_numbers \ .create(phone_number=numbers[0].phone_number) print(number.sid)
# Download the helper library from https://www.twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/console # DANGER! This is insecure. See http://twil.io/secure account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' auth_token = 'your_auth_token' client = Client(account_sid, auth_token) voip = client.available_phone_numbers('US').voip.list(limit=20) for record in voip: print(record.friendly_name)
# Download the Python helper library from twilio.com/docs/python/install import os from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/user/account # To set up environmental variables, see http://twil.io/secure account_sid = os.environ['TWILIO_ACCOUNT_SID'] auth_token = os.environ['TWILIO_AUTH_TOKEN'] client = Client(account_sid, auth_token) numbers = client.available_phone_numbers("GB") \ .local \ .list(contains="4420") number = client.incoming_phone_numbers \ .create(phone_number=numbers[0].phone_number) print(number.sid)
# Download the helper library from https://www.twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/console account_sid = '"ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"' auth_token = 'your_auth_token' client = Client(account_sid, auth_token) shared_cost = client.available_phone_numbers("US").shared_cost.list() for record in shared_cost: print(record.friendly_name)
# Download the Python helper library from twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/user/account account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" auth_token = "your_auth_token" client = Client(account_sid, auth_token) numbers = client.available_phone_numbers("US") \ .local \ .list(contains="510555****") number = client.incoming_phone_numbers \ .create(phone_number=numbers[0].phone_number) print(number.sid)
# Download the Python helper library from twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/user/account account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" auth_token = "your_auth_token" client = Client(account_sid, auth_token) numbers = client.available_phone_numbers("US") \ .toll_free \ .list(contains="STORM") number = client.incoming_phone_numbers \ .create(phone_number=numbers[0].phone_number) print(number.sid)
# Download the Python helper library from twilio.com/docs/python/install import os from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/user/account # To set up environmental variables, see http://twil.io/secure account_sid = os.environ['TWILIO_ACCOUNT_SID'] auth_token = os.environ['TWILIO_AUTH_TOKEN'] client = Client(account_sid, auth_token) numbers = client.available_phone_numbers("US") \ .local \ .list(area_code="510") # Purchase the phone number number = client.incoming_phone_numbers \ .create(phone_number=numbers[0].phone_number) print(number.sid)
# Download the helper library from https://www.twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/console account_sid = '"ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"' auth_token = 'your_auth_token' client = Client(account_sid, auth_token) voip = client.available_phone_numbers("US").voip.list() for record in voip: print(record.friendly_name)
# Download the Python helper library from twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/user/account account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" auth_token = "your_auth_token" client = Client(account_sid, auth_token) numbers = client.available_phone_numbers("GB") \ .local \ .list(voice_enabled=True) number = client.incoming_phone_numbers \ .create(phone_number=numbers[0].phone_number) print(number.sid)
# Download the Python helper library from twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/user/account account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" auth_token = "your_auth_token" client = Client(account_sid, auth_token) numbers = client.available_phone_numbers("US") \ .local \ .list(in_region="AR") number = client.incoming_phone_numbers \ .create(phone_number=numbers[0].phone_number) print(number.sid)
# Download the helper library from https://www.twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/console # DANGER! This is insecure. See http://twil.io/secure account_sid = 'ACf99d181bee7652734862dd42cb0c7601' auth_token = 'b13582320472c60b71fc7912e7c03ec2' client = Client(account_sid, auth_token) local = client.available_phone_numbers('US').local.list(in_region='NY', limit=20) for record in local: print(record.friendly_name)
# Download the helper library from https://www.twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/console account_sid = '"ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"' auth_token = 'your_auth_token' client = Client(account_sid, auth_token) machine_to_machine = client.available_phone_numbers("US").machine_to_machine \ .list() for record in machine_to_machine: print(record.friendly_name)
# Download the Python helper library from twilio.com/docs/python/install import os from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/user/account # To set up environmental variables, see http://twil.io/secure account_sid = os.environ['TWILIO_ACCOUNT_SID'] auth_token = os.environ['TWILIO_AUTH_TOKEN'] client = Client(account_sid, auth_token) numbers = client.available_phone_numbers("US") \ .local \ .list(contains="510555****") number = client.incoming_phone_numbers \ .create(phone_number=numbers[0].phone_number) print(number.sid)
# Download the helper library from https://www.twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/console # DANGER! This is insecure. See http://twil.io/secure account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' auth_token = 'your_auth_token' client = Client(account_sid, auth_token) national = client.available_phone_numbers('US').national.list(limit=20) for record in national: print(record.friendly_name)
# Download the helper library from https://www.twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/console account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' auth_token = 'your_auth_token' client = Client(account_sid, auth_token) available_phone_number_country = client.available_phone_numbers('US').fetch() print(available_phone_number_country.country_code)
# Download the Python helper library from twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/user/account account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" auth_token = "your_auth_token" client = Client(account_sid, auth_token) numbers = client.available_phone_numbers("US") \ .local \ .list(exclude_local_address_required=True) number = client.incoming_phone_numbers \ .create(phone_number=numbers[0].phone_number) print(number.sid)
# Download the Python helper library from twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/user/account account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" auth_token = "your_auth_token" client = Client(account_sid, auth_token) numbers = client.available_phone_numbers("GB") \ .mobile \ .list() number = client.incoming_phone_numbers \ .create(phone_number=numbers[0].phone_number) print(number.sid)
# Download the Python helper library from twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/user/account account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" auth_token = "your_auth_token" client = Client(account_sid, auth_token) numbers = client.available_phone_numbers("US").toll_free.list() number = client.incoming_phone_numbers.create(phone_number=numbers[0].phone_number) print(number.sid)
# Download the helper library from https://www.twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/console account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' auth_token = 'your_auth_token' client = Client(account_sid, auth_token) machine_to_machine = client.available_phone_numbers('US') \ .machine_to_machine \ .list() for record in machine_to_machine: print(record.friendly_name)
# Download the Python helper library from twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/user/account account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" auth_token = "your_auth_token" client = Client(account_sid, auth_token) numbers = client.available_phone_numbers("US") \ .toll_free \ .list() number = client.incoming_phone_numbers \ .create(phone_number=numbers[0].phone_number) print(number.sid)
# Download the Python helper library from twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/user/account account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" auth_token = "your_auth_token" client = Client(account_sid, auth_token) numbers = client.available_phone_numbers("US") \ .local \ .list(area_code="510") number = client.incoming_phone_numbers \ .create(phone_number=numbers[0].phone_number) print(number.sid)
# Download the Python helper library from twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/user/account account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" auth_token = "your_auth_token" client = Client(account_sid, auth_token) numbers = client.available_phone_numbers("US") \ .local \ .list(contains="STORM") number = client.incoming_phone_numbers \ .create(phone_number=numbers[0].phone_number) print(number.sid)
# Download the helper library from https://www.twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/console # DANGER! This is insecure. See http://twil.io/secure account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' auth_token = 'your_auth_token' client = Client(account_sid, auth_token) shared_cost = client.available_phone_numbers('US').shared_cost.list(limit=20) for record in shared_cost: print(record.friendly_name)
# Download the Python helper library from twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/user/account account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" auth_token = "your_auth_token" client = Client(account_sid, auth_token) numbers = client.available_phone_numbers("US") \ .toll_free \ .list(contains="KYLO", area_code="800") number = client.incoming_phone_numbers \ .create(phone_number=numbers[0].phone_number) print(number.sid)
# Download the Python helper library from twilio.com/docs/python/install from twilio.rest import Client # Your Account Sid and Auth Token from twilio.com/user/account account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" auth_token = "your_auth_token" client = Client(account_sid, auth_token) numbers = client.available_phone_numbers("US").local.list(in_region="AR") number = client.incoming_phone_numbers.create(phone_number=numbers[0].phone_number) print(number.sid)