Twilio API的文檔可以在此處找到。
可以在此處找到Python庫文檔。
twilio-python
使用修改版本的語義版本來進行所有更改。有關詳細信息,請參見此文檔。
該庫支持以下Python實現:
使用Pip的PYPI安裝Python的軟件包管理器。
pip3 install twilio
如果PIP安裝在Windows上失敗,請檢查目錄的路徑長度。如果是260個字符,則啟用長路徑或選擇其他較短的位置。
沒有安裝PIP?嘗試通過從命令行運行它來安裝它:
curl https://bootstrap.pypa.io/get-pip.py | python
或者,您可以為twilio-python
下載源代碼(ZIP),然後運行:
python3 setup.py install
信息如果命令行給您一條錯誤消息,該錯誤消息表示拒絕,請嘗試使用
sudo
運行上述命令(例如,sudo pip3 install twilio
)。
嘗試向自己發送短信消息。使用文本編輯器將以下代碼示例保存到計算機。請確保使用Twilio帳戶中的值更新account_sid
, auth_token
和from_
電話號碼。 to
電話號碼將是您自己的手機。
from twilio . rest import Client
# Your Account SID and Auth Token from console.twilio.com
account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
auth_token = "your_auth_token"
client = Client ( account_sid , auth_token )
message = client . messages . create (
to = "+15558675309" ,
from_ = "+15017250604" ,
body = "Hello from Python!" )
print ( message . sid )
將文件保存為send_sms.py
。在終端中, cd
到包含您剛保存的文件的目錄,然後運行:
python3 send_sms.py
短暫延遲後,您將在手機上收到短信。
警告本地測試時可以進行硬碼憑證,但是您應該使用環境變量在提交任何代碼或部署到生產之前將其保密。查看如何設置環境變量以獲取更多信息。
Twilio
客戶需要您的Twilio憑據。您可以將它們直接傳遞給構造函數(請參見下面的代碼)或通過環境變量。
用帳戶SID和Auth Token進行身份驗證:
from twilio . rest import Client
account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
auth_token = "your_auth_token"
client = Client ( account_sid , auth_token )
使用API密鑰和API秘密進行身份驗證:
from twilio . rest import Client
api_key = "XXXXXXXXXXXXXXXXX"
api_secret = "YYYYYYYYYYYYYYYYYY"
account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
client = Client ( api_key , api_secret , account_sid )
另外,沒有這些參數的Client
構造函數將在當前環境中尋找TWILIO_ACCOUNT_SID
和TWILIO_AUTH_TOKEN
變量。
我們建議將您的憑據存儲為環境變量。為什麼?您永遠不必擔心承擔憑證並不小心將其發佈在公共場所。
from twilio . rest import Client
client = Client ()
為了利用Twilio的全球基礎架構,為客戶指定目標區域和/或邊緣:
from twilio . rest import Client
client = Client ( region = 'au1' , edge = 'sydney' )
沒有這些參數的Client
構造函數還將在當前環境中查找TWILIO_REGION
和TWILIO_EDGE
變量。
另外,您可以在構建Twilio客戶端后指定邊緣和/或區域:
from twilio . rest import Client
client = Client ()
client . region = 'au1'
client . edge = 'sydney'
這將導致hostname
從api.twilio.com
轉換為api.sydney.au1.twilio.com
。
from twilio . rest import Client
account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
auth_token = "your_auth_token"
client = Client ( account_sid , auth_token )
call = client . calls . create ( to = "9991231234" ,
from_ = "9991231234" ,
url = "http://twimlets.com/holdmusic?Bucket=com.twilio.music.ambient" )
print ( call . sid )
from twilio . rest import Client
account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
auth_token = "your_auth_token"
client = Client ( account_sid , auth_token )
call = client . calls . get ( "CA42ed11f93dc08b952027ffbc406d0868" )
print ( call . to )
該庫自動為您處理分頁。諸如calls
和messages
之類的集合具有引擎蓋下頁面的list
和stream
方式。使用list
和stream
,您可以指定要接收的記錄數( limit
)以及想要每個頁面獲取為( page_size
)的最大大小。然後,庫將為您處理任務。
list
急切地獲取所有記錄並將其返回為列表,而stream
返回迭代器,懶惰地檢索記錄的頁面,因為您迭代了集合。您也可以使用page
方法手動頁面。
list
方法 from twilio . rest import Client
account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
auth_token = "your_auth_token"
client = Client ( account_sid , auth_token )
for sms in client . messages . list ():
print ( sms . to )
默認情況下,Twilio客戶端將向Twilio API提出同步請求。為了允許異步,非阻滯請求,我們提供了一個可選的異步HTTP客戶端。當與客戶端和隨附的*_async
方法一起使用時,對Twilio API的請求將異步執行。
from twilio . http . async_http_client import AsyncTwilioHttpClient
from twilio . rest import Client
async def main ():
account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
auth_token = "your_auth_token"
http_client = AsyncTwilioHttpClient ()
client = Client ( account_sid , auth_token , http_client = http_client )
message = await client . messages . create_async ( to = "+12316851234" , from_ = "+15555555555" ,
body = "Hello there!" )
asyncio . run ( main ())
將API請求和響應數據記錄到控制台:
import logging
client = Client ( account_sid , auth_token )
logging . basicConfig ()
client . http_client . logger . setLevel ( logging . INFO )
將API請求和響應數據記錄到文件:
import logging
client = Client ( account_sid , auth_token )
logging . basicConfig ( filename = './log.txt' )
client . http_client . logger . setLevel ( logging . INFO )
twilio-python
的8.x版本導出了一個異常類,可幫助您處理特定於Twilio方法的異常。要使用它,請導入TwilioRestException
並捕獲異常如下:
from twilio . rest import Client
from twilio . base . exceptions import TwilioRestException
account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
auth_token = "your_auth_token"
client = Client ( account_sid , auth_token )
try :
message = client . messages . create ( to = "+12316851234" , from_ = "+15555555555" ,
body = "Hello there!" )
except TwilioRestException as e :
print ( e )
要控制電話,您的應用程序需要輸出twiml。
使用twilio.twiml.Response
輕鬆創建此類響應。
from twilio . twiml . voice_response import VoiceResponse
r = VoiceResponse ()
r . say ( "Welcome to twilio!" )
print ( str ( r ))
<? xml version = " 1.0 " encoding = " utf-8 " ?>
< Response >< Say >Welcome to twilio!</ Say ></ Response >
Twilio目前僅用於測試目的,目前僅使用該存儲庫中存在的Dockerfile
及其各自的twilio/twilio-python
Docker圖像。
如果您需要安裝或使用圖書館的幫助,請先檢查Twilio支持幫助中心,並在找不到問題的答案時提交支持票。
如果您取而代之的是在庫中找到一個錯誤,或者想添加新功能,請繼續進行問題或對此倉庫提取請求!