Send SMS using Python msg91-sms PyPI package
MSG91 is a well-known API provider to send SMS. In this tutorial, I am going to show you how to send SMS with MSG91 API using Python msg91-sms PyPI package.
The msg91-sms library is available on PyPI. First of all, you need to install this library. Below is how to install it using pip:
pip install msg91-sms
Requirements for msg91-sms Python library
Keep it in mind that, request and json library are required to use this Python package. So it should be installed in your system.
After the installation of msg91-sms done successfully, you have to import it:
import msg91_sms as msgsms
After that, you can use it to send SMS. In msg91-sms there are already written the class within the package. You just need to create an object of the class:
msg = msgsms.Cspd_msg91(apikey='YOUR MSG91 API KEY')
Now, replace the “YOUR MSG91 API KEY'” with your own MSG91 API key. You can get this API key from official MSG91 site.
Now I am going to discuss how to send SMS.
Send an SMS
To send a custom SMS using MSG91 API key, below is the example code:
msg.send(route,sender,phone_number,SMS_text)
In the above code, you have to provide the route, phone number and SMS text. For example, below is the complete code that sends SMS with route 4:
import msg91_sms as msgsms msg = msgsms.Cspd_msg91(apikey='YOUR MSG91 API KEY') sms_txt = "This is a text SMS from MSG91" send_sms_resp = msg.send(4,'TXTIN','919999999999',sms_txt) print(send_sms_resp)
Send OTP SMS
Below is the complete code to send OTP SMS:
otp_sms_txt = "Your otp code is ##OTP##" send_otp_sms_resp = msg.send_otp('TXTIN',919999999999,otp_sms_txt) print(send_otp_sms_resp)
The “##OTP##” will contain the OTP code in the SMS text.
Verify OTP
Below is how to verify the OTP received for the phone number:
otp_verify_response = msg.verify_otp(919999999999,OTP_code) print(otp_verify_response)
Resend OTP
Below is how to send OTP using Python and msg-91 PyPI package:
resend_otp_sms_resp = msg.resend_otp(919999999999) print(resend_otp_sms_resp)
I hope, you have understood how to send SMS using Python and MSG91 API with the msg91-sms PyPI library.
Also, read:
- Weather forecasting with darksky api in python
- How to implement KMP String Matching algorithm in Python
How to receive an SMS. Can you
pleass the code for that