共計 803 個字符,預計需要花費 3 分鐘才能閱讀完成。
要使用 Python 實現自動發送短信,你可以使用第三方的短信服務提供商的 API。其中,Twilio 是一個常用的短信服務提供商。以下是一個使用 Twilio API 發送短信的示例代碼:
-
首先,在 Twilio 官網注冊賬號,并創建一個項目,獲取賬號 SID 和認證令牌。
-
安裝 Twilio 庫:
pip install twilio
- 編寫 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_TOKEN
和YOUR_TWILIO_PHONE_NUMBER
替換為你在 Twilio 官網獲取的信息。然后運行代碼,即可發送短信。
丸趣 TV 網 – 提供最優質的資源集合!
正文完