今回は REST API を使って電話をかけたいと思います。
REST API の使い方は公式のドキュメントを参考にしたました。
言語は Python でやります。
では、早速コードを見ていきましょう。
ライブラリインストール
まずは、Twilio ライブラリをインストールします。手順はこちらにも書いてあります。https://www.twilio.com/docs/python/install
何通りか方法がありますが、pip か eazy_install で twilio をインストールすればいいと思います。
eazy_install twilio
電話をかけるサンプル
以下は公式サンプルのコードそのまんまです。1 2 3 4 5 6 7 8 9 10 11 12 |
# Download the Python helper library from twilio.com/docs/python/install from twilio.rest import TwilioRestClient # Your Account Sid and Auth Token from twilio.com/user/account account_sid = "AC3094732a3c49700934481addd5ce1659" auth_token = "{{ auth_token }}" client = TwilioRestClient(account_sid, auth_token) to = "+14155551212" , from_ = "+14158675309" ) print call.sid |
最初にライブラリから twilio.rest をインポートしてます。
次に、account_sid と auth_token の設定をします。
これはアカウントセッティングページから確認できます。(Live の方)
この2つを使って TwilioRestClient を作成。 client.calls.create で発信をします。
create には、TwiML の URL と相手の電話番号(to)と自分の電話番号(from_)を指定します。
設定に問題がなければこれで電話がかかります。
iutest に組み込んでみた
早速、REST API を使ってテストが失敗したら電話掛けられるようなものを作ってみました。https://github.com/srz-zumix/iutest
https://github.com/srz-zumix/iutest/blob/master/tools/twilio/iutwilio.py
0 件のコメント:
コメントを投稿