问题描述
我正在查询可用的电话号码,如下所示:
from twilio.rest import Client
client = Client(settings.TWILIO_ACCOUNT_SID,settings.TWILIO_AUTH_TOKEN)
available = client.available_phone_numbers("US").local.list()
我想遍历所有可用的电话号码作为字典对象,但是据我所知Twilio的Python SDK没有这样的东西。
我将必须执行以下操作:
results = [{
"address_requirements": phone.address_requirements,"beta": phone.beta,"capabilities": phone.capabilities,"MMS": phone.MMS,"SMS": phone.SMS,"fax": phone.fax,"voice": phone.voice,"friendly_name": phone.friendly_name,"iso_country": phone.iso_country,"lata": phone.lata,"locality": phone.locality,"longitude": phone.longitude,"phone_number": phone.phone_number,"postal_code": phone.postal_code,"rate_center": phone.rate_center,"region": phone.region
} for phone in available]
这似乎很愚蠢,因为API返回了我想要的确切格式。我很想抛弃SDK,直接拨打电话。
解决方法
经过大量搜索并深入研究了源代码,我找到了一种访问所需数据的简单方法。它使用了您不应该使用的私有属性,但是如果它们不提供您所需要的,则您必须做必须做的事情。
尽管我仍然必须运行一个额外的不必要的循环,但我可以达到我想要的结果:
from twilio.rest import Client
client = Client(settings.TWILIO_ACCOUNT_SID,settings.TWILIO_AUTH_TOKEN)
available = client.available_phone_numbers("US").local.list()
results = [phone._properties for phone in available]