|
| 1 | +import json |
| 2 | +import requests |
| 3 | +from random import randint |
| 4 | + |
| 5 | + |
| 6 | +class sendotp: |
| 7 | + |
| 8 | + def __init__(self,key,msg) : |
| 9 | + |
| 10 | + self.baseUrl = "http://control.msg91.com" |
| 11 | + self.authkey = key |
| 12 | + |
| 13 | + try: |
| 14 | + msg |
| 15 | + except NameError: |
| 16 | + self.msg = "Your otp is {{otp}}. Please do not share it with anybody" |
| 17 | + else: |
| 18 | + self.msg = msg |
| 19 | + |
| 20 | + def actionURLBuilder(self,actionurl) : |
| 21 | + # print self.baseUrl + '/api/' +str(actionurl) |
| 22 | + print actionurl |
| 23 | + return self.baseUrl + '/api/' +str(actionurl) |
| 24 | + |
| 25 | + |
| 26 | + def generateOtp(self) : |
| 27 | + return randint(1000,9999) |
| 28 | + |
| 29 | + def send(self,contactNumber,senderId,otp) : |
| 30 | + values = { |
| 31 | + 'authkey' : self.authkey, |
| 32 | + 'mobile' : contactNumber, |
| 33 | + 'message' : self.msg.replace("{{otp}}", str(otp)), |
| 34 | + 'sender' : senderId, |
| 35 | + 'otp' : otp |
| 36 | + } |
| 37 | + print self.call('sendotp.php',values) |
| 38 | + return otp |
| 39 | + |
| 40 | + def retry(self,contactNumber,retrytype='voice') : |
| 41 | + values = { |
| 42 | + 'authkey' : self.authkey, |
| 43 | + 'mobile' : contactNumber, |
| 44 | + 'retrytype' : retrytype |
| 45 | + } |
| 46 | + print values |
| 47 | + # response = self.call('retryotp.php',values) |
| 48 | + return; |
| 49 | + |
| 50 | + |
| 51 | + def verify(self,contactNumber,otp) : |
| 52 | + values = { |
| 53 | + 'authkey' : self.authkey, |
| 54 | + 'mobile' : contactNumber, |
| 55 | + 'otp' : otp |
| 56 | + } |
| 57 | + response = self.call('verifyRequestOTP.php',values) |
| 58 | + return response; |
| 59 | + |
| 60 | + def call(self,actionurl,args) : |
| 61 | + url = self.actionURLBuilder(actionurl) |
| 62 | + print url |
| 63 | + payload = (args) |
| 64 | + |
| 65 | + response = requests.post(url,data=payload, verify=False) |
| 66 | + print response.text |
| 67 | + return response.status_code |
0 commit comments