溫馨提示×

怎么使用Python代碼實現自動發送短信

小億
194
2024-03-01 19:47:17
欄目: 編程語言

要使用Python實現自動發送短信,你可以使用第三方的短信服務提供商的API。其中,Twilio是一個常用的短信服務提供商。以下是一個使用Twilio API發送短信的示例代碼:

  1. 首先,在Twilio官網注冊賬號,并創建一個項目,獲取賬號SID和認證令牌。

  2. 安裝Twilio庫:

pip install twilio
  1. 編寫Python代碼:
from twilio.rest import Client

# Your Twilio account SID and auth token
account_sid = 'YOUR_ACCOUNT_SID'
auth_token = 'YOUR_AUTH_TOKEN'

# Your Twilio phone number
from_number = 'YOUR_TWILIO_PHONE_NUMBER'

# Recipient's phone number
to_number = '+1234567890'

# Message to be sent
message = 'Hello, this is a test message from Twilio!'

# Create Twilio client
client = Client(account_sid, auth_token)

# Send message
message = client.messages.create(
    body=message,
    from_=from_number,
    to=to_number
)

print('Message sent successfully!')

將上述代碼中的YOUR_ACCOUNT_SID、YOUR_AUTH_TOKENYOUR_TWILIO_PHONE_NUMBER替換為你在Twilio官網獲取的信息。然后運行代碼,即可發送短信。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女