The python port of libphonenumber is pretty complete, but is a little short on examples and sample code. Here are some simple examples for some common uses of this fantastic library.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<Response> | |
<Say>This call may be recording for Quality Assurance purposes.</Say> | |
<Dial record="true"> | |
<Number url="message.xml">+18085551234</Number> | |
</Dial> | |
</Response> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* ### Subaccount Circuit Breaker Script ### | |
* This script is designed to make a subaccount suspend itself in response to | |
* a Usage Trigger firing a callback. You can learn about Usage Triggers here: | |
* https://www.twilio.com/docs/api/rest/usage-triggers | |
* | |
* Why would you want to do this? In some cases suspending a subaccount based on high, | |
* unexpected usage can be a good thing. Coding errors and fraud can both cause spikes | |
* in undesired usage. Setting this script up might help you to sleep better at night. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Download the twilio-php helper library from: | |
* https://github.com/twilio/twilio-php | |
* Copy the "Services" folder into the directory containing this file. | |
*/ | |
require('Services/Twilio.php'); | |
$response = new Services_Twilio_Twiml; | |
$dial = $response->dial(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
header("content-type: text/xml"); | |
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; | |
?> | |
<Response> | |
<Gather action="make-call.php" timeout="30"> | |
<Say>Please enter the phone number you wish to dial</Say> | |
</Gather> | |
</Response> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This section ensures that Twilio gets a response. | |
*/ | |
header('Content-type: text/xml'); | |
echo '<?xml version="1.0" encoding="UTF-8"?>'; | |
echo '<Response></Response>'; //Place the desired response (if any) here | |
/** | |
* This section actually sends the email. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Example of a function to determine the country code of a phone number. | |
Based on the python port of Google's libphonenumber project: | |
https://github.com/daviddrysdale/python-phonenumbers | |
""" | |
import phonenumbers as ph | |
def get_country_code(phone_number): | |
"""Returns the ISO 3166-1 alpha-2 code for a phone number |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import phonenumbers as ph | |
def japan_format(phone_number, country_code='JP'): | |
""" | |
Converts local Japanese formatted number to E.164 format | |
Arguments: | |
phone_number -- A Japanese phone number in any format as a string | |
>>> japan_format("080-1234-5678") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use com\google\i18n\phonenumbers\PhoneNumberUtil; | |
use com\google\i18n\phonenumbers\PhoneNumberFormat; | |
use com\google\i18n\phonenumbers\NumberParseException; | |
require_once 'libphonenumber-for-PHP/PhoneNumberUtil.php'; | |
function sanitize_number ($phone_number, $iso_code) { | |
$pn = PhoneNumberUtil::getInstance(); | |
try { |