| 17349 |
manish.sha |
1 |
import json
|
|
|
2 |
from dtr.storage import DataService
|
|
|
3 |
from dtr.storage.DataService import notification_campaigns, Users
|
|
|
4 |
import MySQLdb
|
|
|
5 |
import pymongo
|
|
|
6 |
from elixir import *
|
|
|
7 |
from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
|
|
|
8 |
from sqlalchemy.sql import func
|
|
|
9 |
from sqlalchemy.sql.expression import and_, or_, desc, not_, distinct, cast, \
|
|
|
10 |
between
|
|
|
11 |
from datetime import datetime
|
|
|
12 |
from datetime import timedelta
|
|
|
13 |
import urllib
|
|
|
14 |
import urllib2
|
|
|
15 |
import json
|
|
|
16 |
import time
|
|
|
17 |
import traceback
|
| 17398 |
manish.sha |
18 |
import logging
|
| 18199 |
manas |
19 |
from dtr.utils.utils import num_encode
|
| 17349 |
manish.sha |
20 |
|
| 17400 |
manish.sha |
21 |
PUSH_NOTIFICATIONS_DETAILS_URL="http://localhost:3001/getPushNotificationDetailsByType/?type=%s¬ification_campaign_id=%d&status=%s"
|
| 17349 |
manish.sha |
22 |
PROMOTIONAL_SMS_SEND_URL = "http://103.15.179.45:8085/MessagingGateway/SendTransSMS?"
|
| 22758 |
amit.gupta |
23 |
TRANSACTIONAL_SMS_SEND_URL = "http://103.15.179.45:8085/MessagingGateway/SendTransSMS?"
|
| 17349 |
manish.sha |
24 |
#ID is: PRO02135496 Mobile Number is: 918512809859 PRO02135496#SMS Sent successfully to : 918512809859
|
|
|
25 |
#http://103.15.179.45:8085/MessagingGateway/http/querymsg?user=srlsaholicP&password=srp8oct&apimsgid=PRO02257518
|
|
|
26 |
#MessageID: PRO02135496 STATUS: DELIVRD ErrorCode: 0 DR Time: 2015-10-20 18:05:11
|
|
|
27 |
|
| 17352 |
manish.sha |
28 |
DataService.initialize(db_hostname="192.168.158.89")
|
| 17349 |
manish.sha |
29 |
|
| 17398 |
manish.sha |
30 |
logging.basicConfig(level=logging.DEBUG)
|
|
|
31 |
|
| 17349 |
manish.sha |
32 |
def main():
|
| 17452 |
manish.sha |
33 |
logging.debug('Starting SMS Blaster Process.....'+str(datetime.now()))
|
| 17349 |
manish.sha |
34 |
timeCondition = datetime.now()-timedelta(hours=2)
|
|
|
35 |
expireTimeCondition = datetime.now()+timedelta(hours=2)
|
| 18961 |
manish.sha |
36 |
eligibleSmsNotficationCampaigns = notification_campaigns.query.filter(notification_campaigns.sendsms==True).filter(or_(notification_campaigns.smsprocessed==False, notification_campaigns.smsprocessed==None)).filter(notification_campaigns.created<timeCondition).filter(notification_campaigns.expiresat>expireTimeCondition).all()
|
| 17349 |
manish.sha |
37 |
#eligibleSmsNotficationCampaigns = notification_campaigns.query.filter(notification_campaigns.id==5417).all()
|
|
|
38 |
|
|
|
39 |
for notificationCampaign in eligibleSmsNotficationCampaigns:
|
| 17398 |
manish.sha |
40 |
logging.debug('Notification_Campign_Id:- '+str(notificationCampaign.id))
|
| 18285 |
manish.sha |
41 |
notificationCampaign.smsprocessed = True
|
|
|
42 |
session.commit()
|
| 17349 |
manish.sha |
43 |
sentFailedresponse = urllib2.urlopen(PUSH_NOTIFICATIONS_DETAILS_URL%('sent',notificationCampaign.id,0)).read()
|
|
|
44 |
jsonList = json.loads(sentFailedresponse)
|
|
|
45 |
userList = []
|
|
|
46 |
for obj in jsonList:
|
|
|
47 |
userList.append(long(obj.get('user_id')))
|
| 17398 |
manish.sha |
48 |
logging.debug('Failed User List...'+str(len(userList)))
|
| 17349 |
manish.sha |
49 |
|
|
|
50 |
sentSuccessResponse = urllib2.urlopen(PUSH_NOTIFICATIONS_DETAILS_URL%('sent',notificationCampaign.id,1)).read()
|
|
|
51 |
jsonList = json.loads(sentSuccessResponse)
|
|
|
52 |
for obj in jsonList:
|
|
|
53 |
userList.append(long(obj.get('user_id')))
|
|
|
54 |
|
|
|
55 |
userList = list(set(userList))
|
|
|
56 |
|
|
|
57 |
receivedSuccessResponse = urllib2.urlopen(PUSH_NOTIFICATIONS_DETAILS_URL%('recieved',notificationCampaign.id,1)).read()
|
|
|
58 |
jsonList = json.loads(receivedSuccessResponse)
|
| 17398 |
manish.sha |
59 |
logging.debug('User List Before...'+str(len(userList)))
|
| 17349 |
manish.sha |
60 |
for obj in jsonList:
|
|
|
61 |
if obj.get('user_id') in userList:
|
|
|
62 |
userList.remove(long(obj.get('user_id')))
|
| 17398 |
manish.sha |
63 |
logging.debug('User List After...'+str(len(userList)))
|
| 17349 |
manish.sha |
64 |
|
|
|
65 |
allUsers = Users.query.filter(Users.id.in_(tuple(userList))).all()
|
|
|
66 |
#print allUsers
|
|
|
67 |
|
|
|
68 |
sentSmsUsers = []
|
| 18217 |
manish.sha |
69 |
proSmsResponseIdMap = {}
|
|
|
70 |
tranSmsResponseIdMap = {}
|
| 17349 |
manish.sha |
71 |
payloadList = []
|
|
|
72 |
|
| 18199 |
manas |
73 |
#smstext = str(notificationCampaign.messagetext) + ' http://bit.ly/1LBesWK'
|
| 17349 |
manish.sha |
74 |
for user in allUsers:
|
|
|
75 |
if user.mobile_number is not None and len(user.mobile_number)==10 and user.mobile_number not in sentSmsUsers:
|
| 18210 |
amit.gupta |
76 |
smsUrl='http://pm1.in/A/' + num_encode(notificationCampaign.id) + '/' + num_encode(user.id)
|
| 18199 |
manas |
77 |
smstext = str(notificationCampaign.messagetext) + ' ' + smsUrl
|
| 17349 |
manish.sha |
78 |
#url_params = { 'Mobile' : '91'+user.mobile_number, 'Username' : 'srlsaholicP', 'Password' : 'srp8oct' , 'MessageType' : 'txt', 'SenderID' : '090000', 'Message' : notificationCampaign.messagetext }
|
| 18217 |
manish.sha |
79 |
url_params = ''
|
|
|
80 |
if "TRAN_SMS" in smstext:
|
| 22758 |
amit.gupta |
81 |
#url_params = { 'ani' : '91'+user.mobile_number, 'uname' : 'srlsaholic', 'passwd' : 'sr18mar' , 'cli' : 'PROFTM', 'message' : smstext.replace("TRAN_SMS", "").strip() }
|
|
|
82 |
url_params = { 'Mobile' : '91'+user.mobile_number, 'Username' : 'srlsaholic', 'Password' : 'sr18mar' , 'MessageType' : 'txt', 'SenderID' : 'PROFTM', 'Message' : smstext }
|
| 18217 |
manish.sha |
83 |
else:
|
|
|
84 |
url_params = { 'Mobile' : '91'+user.mobile_number, 'Username' : 'srlsaholicP', 'Password' : 'srp8oct' , 'MessageType' : 'txt', 'SenderID' : '090000', 'Message' : smstext }
|
| 17349 |
manish.sha |
85 |
encoded_url_params = urllib.urlencode(url_params)
|
| 17398 |
manish.sha |
86 |
logging.debug('User Id:- '+str(user.id)+' Notification_Campaign_Id:- '+ str(notificationCampaign.id)+' Url Params:- '+str(encoded_url_params))
|
| 18217 |
manish.sha |
87 |
url = ''
|
|
|
88 |
if "TRAN_SMS" in smstext:
|
|
|
89 |
url = TRANSACTIONAL_SMS_SEND_URL + encoded_url_params
|
|
|
90 |
else:
|
|
|
91 |
url = PROMOTIONAL_SMS_SEND_URL + encoded_url_params
|
| 17349 |
manish.sha |
92 |
response_str = None
|
|
|
93 |
try:
|
|
|
94 |
req = urllib2.Request(url)
|
|
|
95 |
response = urllib2.urlopen(req)
|
|
|
96 |
response_str = response.read()
|
|
|
97 |
except Exception as e:
|
| 17398 |
manish.sha |
98 |
logging.debug('Error while getting response for user id:- '+ str(user.id) +' Mobile Number:- '+str(user.mobile_number))
|
| 17349 |
manish.sha |
99 |
traceback.print_exc()
|
|
|
100 |
continue
|
|
|
101 |
|
| 17398 |
manish.sha |
102 |
logging.debug('User Id:- '+str(user.id)+' Notification_Campaign_Id:- '+ str(notificationCampaign.id)+' Sent Response:- '+ str(response_str))
|
| 17349 |
manish.sha |
103 |
|
|
|
104 |
|
|
|
105 |
payload = {}
|
|
|
106 |
payload["user_id"]= user.id
|
|
|
107 |
payload["type"]="smssent"
|
|
|
108 |
payload["notification_campaign_id"]= notificationCampaign.id
|
|
|
109 |
payload["status"]=1
|
|
|
110 |
|
|
|
111 |
if len(payloadList)==5000:
|
|
|
112 |
jsonObj = json.dumps([dict(pn) for pn in payloadList])
|
| 17400 |
manish.sha |
113 |
pushpostrequest = urllib2.Request("http://localhost:3001/addPushNotification")
|
| 17349 |
manish.sha |
114 |
pushpostrequest.add_header('Content-Type', 'application/json')
|
|
|
115 |
response = urllib2.urlopen(pushpostrequest, jsonObj).read()
|
|
|
116 |
payloadList = []
|
|
|
117 |
|
|
|
118 |
payloadList.append(payload)
|
|
|
119 |
|
| 18217 |
manish.sha |
120 |
if "TRAN_SMS" in smstext:
|
|
|
121 |
if "Message sent successfully to " in response_str:
|
|
|
122 |
response_str_vals = response_str.split('#')
|
|
|
123 |
payload = {}
|
|
|
124 |
payload["user_id"]= user.id
|
|
|
125 |
payload["type"]="smsprocessed"
|
|
|
126 |
payload["notification_campaign_id"]= notificationCampaign.id
|
|
|
127 |
payload["status"]=1
|
|
|
128 |
payload["sms_id"]=response_str_vals[0]
|
|
|
129 |
payloadList.append(payload)
|
|
|
130 |
tranSmsResponseIdMap[user.id] = response_str_vals[0]
|
|
|
131 |
else:
|
|
|
132 |
payload = {}
|
|
|
133 |
payload["user_id"]= user.id
|
|
|
134 |
payload["type"]="smsrejected"
|
|
|
135 |
payload["notification_campaign_id"]= notificationCampaign.id
|
|
|
136 |
payload["status"]=1
|
|
|
137 |
payloadList.append(payload)
|
| 17349 |
manish.sha |
138 |
else:
|
| 18217 |
manish.sha |
139 |
if "mobile no in DND" in response_str:
|
|
|
140 |
payload = {}
|
|
|
141 |
payload["user_id"]= user.id
|
|
|
142 |
payload["type"]="smsrejected"
|
|
|
143 |
payload["notification_campaign_id"]= notificationCampaign.id
|
|
|
144 |
payload["status"]=1
|
|
|
145 |
payloadList.append(payload)
|
|
|
146 |
else:
|
|
|
147 |
payload = {}
|
|
|
148 |
payload["user_id"]= user.id
|
|
|
149 |
payload["type"]="smsprocessed"
|
|
|
150 |
payload["notification_campaign_id"]= notificationCampaign.id
|
|
|
151 |
payload["status"]=1
|
|
|
152 |
|
|
|
153 |
|
|
|
154 |
response_vals = response_str.split('\n')
|
|
|
155 |
logging.debug('Desired Response String:- '+str(response_vals[2]))
|
|
|
156 |
if len(response_vals)==4 and 'SMS Sent successfully to' in response_vals[2]:
|
|
|
157 |
if response_vals[2].split('#')[0]!='null':
|
|
|
158 |
proSmsResponseIdMap[user.id] = response_vals[2].split('#')[0]
|
|
|
159 |
payload["sms_id"]=response_vals[2].split('#')[0]
|
|
|
160 |
payloadList.append(payload)
|
| 17349 |
manish.sha |
161 |
sentSmsUsers.append(user.mobile_number)
|
|
|
162 |
|
|
|
163 |
time.sleep(120)
|
|
|
164 |
|
| 18217 |
manish.sha |
165 |
for userId, msgId in proSmsResponseIdMap.items():
|
| 17349 |
manish.sha |
166 |
#http://103.15.179.45:8085/MessagingGateway/http/querymsg?user=srlsaholicP&password=srp8oct&apimsgid=PRO02257518
|
|
|
167 |
del_det_params = {'user' : 'srlsaholicP', 'password' : 'srp8oct' , 'apimsgid' : msgId}
|
|
|
168 |
encoded_del_det_params = urllib.urlencode(del_det_params)
|
|
|
169 |
del_det_url = 'http://103.15.179.45:8085/MessagingGateway/http/querymsg?' + encoded_del_det_params
|
| 17398 |
manish.sha |
170 |
logging.debug('Delivery Report Url:- '+del_det_url)
|
| 17349 |
manish.sha |
171 |
try:
|
|
|
172 |
del_req = urllib2.Request(del_det_url)
|
|
|
173 |
del_response = urllib2.urlopen(del_req)
|
|
|
174 |
del_response_str = del_response.read()
|
| 17398 |
manish.sha |
175 |
logging.debug('Delivery Report Response:- '+str(del_response_str))
|
| 17349 |
manish.sha |
176 |
if 'STATUS' in del_response_str:
|
|
|
177 |
status = del_response_str.split(' ')[3]
|
|
|
178 |
if status=='DELIVRD':
|
|
|
179 |
status = 'SMS_DELIVRD'
|
|
|
180 |
elif status=='SENT':
|
|
|
181 |
status = 'SMS_SENT_OPERATOR'
|
|
|
182 |
elif status=='REJECTD' or status== 'FAILED':
|
|
|
183 |
status = 'SMS_FAILED'
|
|
|
184 |
elif 'EXP_' in status:
|
|
|
185 |
status = 'SMS_EXPIRED'
|
|
|
186 |
else:
|
|
|
187 |
status = 'SMS_'+status
|
|
|
188 |
|
|
|
189 |
payload = {}
|
|
|
190 |
payload["user_id"]= userId
|
|
|
191 |
payload["type"]=status
|
|
|
192 |
payload["notification_campaign_id"]= notificationCampaign.id
|
|
|
193 |
payload["status"]=1
|
| 17377 |
manish.sha |
194 |
if status == 'SMS_SENT_OPERATOR':
|
|
|
195 |
payload["sms_id"] = msgId
|
| 17349 |
manish.sha |
196 |
|
|
|
197 |
if len(payloadList)==5000:
|
|
|
198 |
jsonObj = json.dumps([dict(pn) for pn in payloadList])
|
| 17400 |
manish.sha |
199 |
pushpostrequest = urllib2.Request("http://localhost:3001/addPushNotification")
|
| 17349 |
manish.sha |
200 |
pushpostrequest.add_header('Content-Type', 'application/json')
|
|
|
201 |
response = urllib2.urlopen(pushpostrequest, jsonObj).read()
|
|
|
202 |
payloadList = []
|
|
|
203 |
payloadList.append(payload)
|
|
|
204 |
|
|
|
205 |
except Exception as e:
|
| 17398 |
manish.sha |
206 |
logging.debug('Error while getting response for message id:- '+ msgId)
|
| 17349 |
manish.sha |
207 |
traceback.print_exc()
|
| 17398 |
manish.sha |
208 |
continue
|
| 18217 |
manish.sha |
209 |
|
|
|
210 |
for userId, msgId in tranSmsResponseIdMap.items():
|
|
|
211 |
if msgId == "No_Response":
|
|
|
212 |
status = 'SMS_FAILED'
|
|
|
213 |
payload = {}
|
|
|
214 |
payload["user_id"]= userId
|
|
|
215 |
payload["type"]=status
|
|
|
216 |
payload["notification_campaign_id"]= notificationCampaign.id
|
|
|
217 |
payload["status"]=1
|
|
|
218 |
if len(payloadList)==5000:
|
|
|
219 |
jsonObj = json.dumps([dict(pn) for pn in payloadList])
|
|
|
220 |
pushpostrequest = urllib2.Request("http://localhost:3001/addPushNotification")
|
|
|
221 |
pushpostrequest.add_header('Content-Type', 'application/json')
|
|
|
222 |
response = urllib2.urlopen(pushpostrequest, jsonObj).read()
|
|
|
223 |
payloadList = []
|
|
|
224 |
payloadList.append(payload)
|
|
|
225 |
else:
|
|
|
226 |
del_det_params = {'uname' : 'srlsaholic', 'passwd' : 'sr18mar' , 'messageid' : msgId}
|
|
|
227 |
encoded_del_det_params = urllib.urlencode(del_det_params)
|
|
|
228 |
del_det_url = 'http://103.15.179.45:8085/SMSGateway/getApiReport?' + encoded_del_det_params
|
|
|
229 |
logging.debug('Delivery Report Url:- '+del_det_url)
|
|
|
230 |
try:
|
|
|
231 |
del_req = urllib2.Request(del_det_url)
|
|
|
232 |
del_response = urllib2.urlopen(del_req)
|
|
|
233 |
del_response_str = del_response.read()
|
|
|
234 |
logging.debug('Delivery Report Response:- '+str(del_response_str))
|
|
|
235 |
|
|
|
236 |
del_response_vals = del_response_str.split(' ')
|
|
|
237 |
|
|
|
238 |
status = ''
|
|
|
239 |
if len(del_response_vals) == 8 and del_response_vals[7][:-2] == '0':
|
|
|
240 |
status = 'SMS_DELIVRD'
|
|
|
241 |
elif "Submitted to SMSC" in del_response_str:
|
|
|
242 |
status = 'SMS_INPROCESS'
|
|
|
243 |
else:
|
|
|
244 |
status = 'SMS_FAILED'
|
|
|
245 |
payload = {}
|
|
|
246 |
payload["user_id"]= userId
|
|
|
247 |
payload["type"]=status
|
|
|
248 |
payload["notification_campaign_id"]= notificationCampaign.id
|
| 18221 |
manish.sha |
249 |
if status == 'SMS_INPROCESS':
|
|
|
250 |
payload["sms_id"] = msgId
|
| 18217 |
manish.sha |
251 |
payload["status"]=1
|
|
|
252 |
if len(payloadList)==5000:
|
|
|
253 |
jsonObj = json.dumps([dict(pn) for pn in payloadList])
|
|
|
254 |
pushpostrequest = urllib2.Request("http://localhost:3001/addPushNotification")
|
|
|
255 |
pushpostrequest.add_header('Content-Type', 'application/json')
|
|
|
256 |
response = urllib2.urlopen(pushpostrequest, jsonObj).read()
|
|
|
257 |
payloadList = []
|
|
|
258 |
payloadList.append(payload)
|
|
|
259 |
|
|
|
260 |
except Exception as e:
|
|
|
261 |
logging.debug('Error while getting response for message id:- '+ msgId)
|
|
|
262 |
traceback.print_exc()
|
|
|
263 |
continue
|
|
|
264 |
|
|
|
265 |
|
| 17349 |
manish.sha |
266 |
|
|
|
267 |
jsonObj = json.dumps([dict(pn) for pn in payloadList])
|
| 17400 |
manish.sha |
268 |
pushpostrequest = urllib2.Request("http://localhost:3001/addPushNotification")
|
| 17349 |
manish.sha |
269 |
pushpostrequest.add_header('Content-Type', 'application/json')
|
|
|
270 |
response = urllib2.urlopen(pushpostrequest, jsonObj).read()
|
|
|
271 |
|
| 17398 |
manish.sha |
272 |
logging.debug('Total Sms Sent:- '+str(len(sentSmsUsers)))
|
| 17349 |
manish.sha |
273 |
|
|
|
274 |
notificationCampaign.smsprocessed = True
|
|
|
275 |
session.commit()
|
| 17398 |
manish.sha |
276 |
|
| 17452 |
manish.sha |
277 |
logging.debug('SMS Blaster Process Completed.....'+str(datetime.now()))
|
| 17349 |
manish.sha |
278 |
|
|
|
279 |
|
|
|
280 |
if __name__=='__main__':
|
|
|
281 |
main()
|
|
|
282 |
try:
|
|
|
283 |
session.close()
|
|
|
284 |
except:
|
|
|
285 |
print 'Error while closing session'
|