要使用Python实现自动发送短信,你可以使用第三方的短信服务提供商的API。其中,Twilio是一个常用的短信服务提供商。以下是一个使用Twilio API发送短信的示例代码:
首先,在Twilio官网注册账号,并创建一个项目,获取账号SID和认证令牌。
安装Twilio库:
pip install twilio编写Python代码:from twilio.rest import Client# Your Twilio account SID and auth tokenaccount_sid = 'YOUR_ACCOUNT_SID'auth_token = 'YOUR_AUTH_TOKEN'# Your Twilio phone numberfrom_number = 'YOUR_TWILIO_PHONE_NUMBER'# Recipient's phone numberto_number = '+1234567890'# Message to be sentmessage = 'Hello, this is a test message from Twilio!'# Create Twilio clientclient = Client(account_sid, auth_token)# Send messagemessage = 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官网获取的信息。然后运行代码,即可发送短信。




