How to send SMS alert using Python and MSG91 API
Last Updated :
20 Aug, 2020
In our college days we most often forget our daily classes, right? To keep track of classes every day, we can send a Notification (i.e, ) SMS Alert regards their classes to their mobile phones using Python.
We need to use two functionalities: http module and MSG91 API for sending SMS.
Python3
import http.client as ht
conn = ht.HTTPSConnection("api.msg91.com")
Here we are importing the http client function in http module (since we are using our system as client and msg91 api service as server) and using HTTPSConnection function to establish the connection with SMS API Service (MSG91).
While establishing connection we need to send two main parameters in data packet (i.e) Header and Payload.
Header : In the header, We will send the authentication key of our MSG91 API. And of course, context text is nothing but context type that means the type of the payload. We are sending all the payload information in JSON format, so the context type will be JSON.
Python3
headers = {# Paste your MSG91 API Key
'authkey' : "",
'content-type': "application/json"}
You need to create an account in MSG91 and you need to create an API key in MSG91 to send SMS.
Payload : Everyone knows that the payload is the important section through which the data is sent or received. Here we are sending the sender ID, route, country along with the message and a receiver mobile number. Where sender ID is nothing but the sender name. It must be of length 6 and should contain only alpha characters. If you want to send SMS internationally means use 0 as country code otherwise use 91 for India communication.
python3
payload = '''{"sender": "MSGAPI",
"route": "4",
"country": "91",
"sms": [
{
"message": "Welcome X, Today you have PC class",
"to": [
"9090XX8921"
]
},
{
"message": "Welcome Y, Today you have WT Class",
"to": [
"901X83XX01"
]
}
]
}'''
Now we need to send the connection request along with this header and payload. Here we use the POST method to establish the connection. Once the request is sent, the API will send the message to the receivers what we have mentioned as the JSON array. Then the API acknowledges us with the status code as 200 and a message as success.
python3
# importing the module
import http.client as ht
# establishing connection
conn = ht.HTTPSConnection("api.msg91.com")
# determining the payload
payload = '''{"sender": "MSGAPI",
"route": "4",
"country": "91",
"sms": [
{
"message": "Welcome GeeksForGeeks, Today you have PC class",
"to": [
"9090XX8921"
]
},
]
}'''
# creating the header
headers = {
'authkey': "",
'content-type': "application / json"
}
# sending the connection request
conn.request("POST", "/api / v2 / sendsms", payload, headers)
res = conn.getresponse()
data = res.read()
# printing the acknowledgement
print(data.decode("utf-8"))
Similar Reads
Send SMS updates to mobile phone using python If you are running any python script and want to send regular updates from your script to your mobile phone through SMS, you can use SinchSMS API to send SMS.Approach : Create an app on Sinch and get the key and secret of the app and use these credentials in the following script to send SMS to your
2 min read
How to handle alert prompts in Selenium Python ? Seleniumâs Python Module is built to perform automated testing with Python. Alerts are a way to show popups in the browser for either accepting data or displaying data. Selenium provides methods to handle alerts of all kinds. Seleniumâs Python Module is built to perform automated testing with Python
2 min read
Send message to FB friend using Python The power of Python comes because of the large number of modules it has. This time we are going to use one of those. Every one of us, one time or another, has a wish of the message (or spamming -.-) our Facebook friend. This is a program that can do something similar. So without further delay, letâs
3 min read
How to Send Automated Email Messages in Python In this article, we are going to see how to send automated email messages which involve delivering text messages, essential photos, and important files, among other things. in Python. We'll be using two libraries for this: email, and smtplib, as well as the MIMEMultipart object. This object has mul
6 min read
Python | Send SMS using Twilio As we know Python is a cool scripting language and can be used to write scripts to easify day-to-day task. Also, since python has large community support and lots of module/API available, it makes Python more versatile and popular among users. In this article, we will see how to use Twilio API to se
2 min read