2
1

More than 1 year has passed since last update.

Twilioやーる(Python 3.9)

Last updated at Posted at 2022-12-22

はじめに

Twilioやっていきまーす

開発環境

  • Windows 10 PC
  • Python 3.9

導入

1.Twilioでアカウント作成

2.コンソールからSIDとTokenをメモ

image.png

3.Trialの残高

image.png

4.Get a Twilio phone numberをクリックして電話番号を取得

5.Twilioのライブラリをインストール

pip install twilio

6.PythonからSMSを送ってみる

send_sms.py
from twilio.rest import Client

# Your Account SID from twilio.com/console
account_sid = "Your Account SID"
# Your Auth Token from twilio.com/console
auth_token  = "Your Auth Token"

client = Client(account_sid, auth_token)

message = client.messages.create(
    to="My phone number", 
    from_="Twilio phone number",
    body="Hello from Python!")

print(message.sid)

7.実行するとSMSが届く!

image.png

8.200円くらい使ったみたい?
image.png

9.次は自分に電話をかけてみる

new_record.py
from twilio.rest import Client

account_sid = "Your Account SID"
auth_token = "Your Auth Token"
client = Client(account_sid, auth_token)

call = client.calls.create(
    to="My phone number", 
    from_="Twilio phone number",
    url="http://demo.twilio.com/docs/voice.xml"
)

print(call.sid)

10.かかってきた!
image.png

11.26円くらいかかった?

image.png

12.ちなみに http://demo.twilio.com/docs/voice.xml の中身

<Response>
  <Say voice="alice">Thanks for trying our documentation. Enjoy!</Say>
  <Play>http://demo.twilio.com/docs/classic.mp3</Play>
</Response>

なんか違うこと言ってた気がするけどまあいいや

13.電話履歴を確認

existing_record.py
from twilio.rest import Client

account_sid = "Your Account SID"
auth_token = "Your Auth Token"
client = Client(account_sid, auth_token)

call = client.calls.get("call.id")
print(call.to)

先ほどcallした時にコマンドラインに表示されたcall.sidを入れるがエラー
AttributeError: 'CallContext' object has no attribute 'to'

14.SMSの履歴を確認

iterate_through_records.py
from twilio.rest import Client

account_sid = "Your Account SID"
auth_token = "Your Auth Token"
client = Client(account_sid, auth_token)

for sms in client.messages.list():
  print(sms.to)

SMSを送った電話番号が表示された

お疲れ様でした

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1