Send SMS updates to mobile phone using python

PythonServer Side ProgrammingProgramming

In this tutorial, we are going to learn how to send SMS in Python. We will be using Twilio for sending SMS.

First of all, go the Twilio and create an account. You will get free trial account. And finish settings.

We have to install a module called twilio to work with Twilio client. Install it with the following command.

pip install twilio

Now, go to the Twilio Documentation on how to send SMS using Twilio client. Or follow the steps to send the SMS.

  • Import the twilio Client from twilio.rest.
  • Get and store the account_sid and auth_token from the your Twilio account.
  • Make instance of the Client by passing account_sid and auth_token.
  • Now, get the from number from your Twilio account.
  • Now, send the message using client.messages.create(body=message, from_=from_number, to=verified_number).
  • You will get the message.

Example

# importing the client from the twilio
from twilio.rest import Client
# Your Account Sid and Auth Token from twilio account
account_sid = [ACCOUNT_SID]
auth_token = [AUTH_TOKEN]
# instantiating the Client
client = Client(account_sid, auth_token)
# sending message
message = client.messages.create(body='Hi there! How are you?', from_=[FROM_NUM
, to=[VERIFIED_NUMBER])
# printing the sid after success
print(message.sid)

If the message is sent successfully, then you will get the hash code as follows without any

SMd954e170ca2545248075c3bc2623aabd

Conclusion

If you have any doubts in the tutorial, mention them in the comment section.

raja
Published on 11-Jul-2020 10:18:07
Advertisements