Sending message from python using msg91

Below is tested code which can be used for sending the sms(message) from python application. As a pre-requisite you need to have an account on http://control.msg91.com

import urllib
import urllib.request
import urllib.parse
class sendmessage:
    @staticmethod
    def send_sms(mobile,message):
       authkey = '53535353535dfsfsdaagagaga' // api key created from http://control.msg91.com
       route = '4'                              // 4 is used to send the transational message other type can be used defined by msg91
       sender = 'TSTEST'                        // This is id which will be used in sms sent to users
       mobiles = mobile                         // you can have more than one comma seperated mobile numbers
       # Prepare you post parameters
       values = {
           'authkey': authkey,
           'mobiles': mobiles,
           'message': message,
           'sender': sender,
           'route': route,
           'country':91
       }
       url = "http://api.msg91.com/api/sendhttp.php"  # API URL
       postdata = urllib.parse.urlencode(values)  # URL encoding the data here.
       postdata = postdata.encode('utf-8')
       req = urllib.request.Request(url, postdata)
       response = urllib.request.urlopen(req)
       output = response.read()  # Get Response
       return

Leave a Reply

Your email address will not be published. Required fields are marked *