Python SMS Automation: Send Texts via Code
To automate sending text messages using Python, you can utilize the API of a third-party SMS service provider. One popular choice is Twilio. Here is an example code using the Twilio API to send text messages:
- First, sign up for an account on the Twilio website, create a project, and obtain your account SID and authentication token.
- Install the Twilio library:
pip install twilio
- Write Python code:
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!')
Replace YOUR_ACCOUNT_SID, YOUR_AUTH_TOKEN, and YOUR_TWILIO_PHONE_NUMBER in the above code with the information you obtained from the Twilio official website. Then run the code to send the message.