def send_instant_msg(benef_number, msg_id): ret = {"status":0, "msg":""} sms_msg = '' try: sms_msg = SMSMessages.objects.get(msg_identifier=msg_id).msg except ObjectDoesNotExist: print 'ERROR: SMS msg not found for the id: '+msg_id ret["status"] = 1 ret["msg"] = "Welcome message for given ID does not exist: "+msg_id return ret sms_msg_hexlified = toHex(sms_msg) #send sms print 'sending instant sms for :'+msg_id sent_code = SendSMSUnicode(recNum=benef_number, msgtxt=sms_msg_hexlified) if 'Status=1' in sent_code: ret["status"] = 1 ret["msg"] = sent_code return ret elif 'Status=0' in sent_code: ret["status"] = 0 ret["msg"] = "Welcome message successfully sent" return ret
def send_reminders(): timezone = 'Asia/Kolkata' tz = pytz.timezone(timezone) today_utc = utcnow_aware() today = today_utc.astimezone(tz) print 'Executing send_reminders scheduled function' today_date = today.date() reminders = VaccineReminder.objects.filter( Q(state=2)|Q(state=1), Q(dueDate__lte=today_date)) for reminder in reminders: language = reminder.vaccine_reference.Language vaccine_name = reminder.vaccine_reference.Vaccine.VaccineName benef_name = reminder.vaccination_beneficiary.ChildName benef_number = reminder.vaccination_beneficiary.NotifyNumber gaurdian_name = reminder.vaccination_beneficiary.Gaurdian_name hw = reminder.vaccination_beneficiary.health_worker if hw and hw.first_name: hw_name = hw.first_name+' '+hw.last_name else: hw_name = '' vacc_date = '{0.month}/{0.day}/{0.year}'.format(reminder.vaccDate) #TODO: Accomodate for IVR, and email later sms_id = reminder.vaccine_reference.sms_message sms_temp = '' try: sms_temp = SMSMessages.objects.get(msg_identifier=sms_id).msg except ObjectDoesNotExist: print 'ERROR: SMS msg not found for the id: '+sms_id continue sms_temp = sms_temp.replace(u'childName', unicode(benef_name)) sms_temp = sms_temp.replace(u'vaccineName', unicode(vaccine_name)) sms_temp = sms_temp.replace(u'date', unicode(vacc_date)) sms_temp = sms_temp.replace(u'hwName', unicode(hw_name)) sms_msg = sms_temp #sms_msg_hexlified = binascii.hexlify(sms_msg.encode('utf-8')) sms_msg_hexlified = toHex(sms_msg) #send sms print 'sending sms' sent_code = SendSMSUnicode(recNum=benef_number, msgtxt=sms_msg_hexlified) if 'Status=1' in sent_code: reminder.state = 1 reminder.errorCode = sent_code reminder.save() elif 'Status=0' in sent_code: reminder.state = 0 reminder.save()
def SendSchSMS(role=StaticData.SCHEDULE_MSG, job=StaticData.SENDER, reach = StaticData.REACHONLYSELF, month=None, year=None): #TODO pick timezone from DB timezone = 'Asia/Kolkata' tz = pytz.timezone(timezone) now_utc = utcnow_aware() now = now_utc.astimezone(tz) if not month: month = now.month if not year: year = now.year #if category == StaticData.SCHEDULE_MSG: sms_template = u"prelude name की event category इस महीने होनी आवश्यक है. अविलंब आशा बहनजी asha से संपर्क करे. धन्यवाद!" _allBenefs = Beneficiary.objects.all() i=0 f = open('SMSsendLog', 'w') for benef in _allBenefs: cds = ContentDelivered.objects.filter(benefeciary=benef) if cds.count() > 0: print 'already sent' continue if not benef.notify_number or benef.notify_number_type != Beneficiary.NUMBER_TYPE.SELF: continue benef_number = benef.notify_number if len(benef_number) == 11: benef_number = benef_number[1:len(benef_number)] if int(benef_number) == 0: continue # #Find all due and unhandled services this month. _allDueCurrentMonth = DueEvents.objects.filter(date__year = year, date__month = month, beneficiary= benef) event = None _numEvents = 0 for _dueService in _allDueCurrentMonth: if not event: event = _dueService.event.val else: event = event +', '+_dueService.event.val _numEvents = _numEvents + 1 if not event: continue if len(event) > 10: event = unicode(event[0:10]) + u" आदि" +unicode(" ("+str(_numEvents)+")") else: event = unicode(event) if ANCBenef.objects.filter(id=benef.id).count() > 0: prelude = u"श्रीमती" category = u"जाँच/टीकाकरण" elif IMMBenef.objects.filter(id=benef.id).count() > 0: prelude = u"बच्चे" category = u"टीकाकरण" name = unicode(benef.first_name) asha_name = u'' if benef.caregiver: asha_name = unicode(benef.caregiver.first_name) #print unicodedata.normalize('NFKD', asha_name).encode('ascii', 'ignore') sms_text = sms_template.replace(u"prelude", prelude).replace(u"name", name).replace(u"category", category).replace(u"event", event).replace(u"asha", asha_name) #print sms_text sms_msg_hexlified = toHex(sms_text) print 'sending Voice Call:' sms_indicator = "#"+str(i)+": "+benef_number+" : "+ unicodedata.normalize('NFKD', event).encode('ascii', 'ignore') print sms_indicator print unicodedata.normalize('NFKD', sms_text).encode('ascii', 'ignore') #f.write(sms_indicator) #f.write(unicodedata.normalize('NFKD', sms_text).encode('ascii', 'ignore')) #f.write('\n') #sent_code = SendSMSUnicode(recNum=benef_number, msgtxt=sms_msg_hexlified) sent_code = 'Status=0' #print sent_code #f.write(sent_code) #f.write('#################################\n') i = i +1 if 'Status=1' in sent_code: ContentDelivered.objects.create(msg= sms_text, medium=ContentDelivered.SMS, timestamp = utcnow_aware(), status=1, benefeciary = benef) elif 'Status=0' in sent_code: ContentDelivered.objects.create(msg= sms_text, medium=ContentDelivered.SMS, timestamp = utcnow_aware(), status=0, benefeciary = benef) f.close() return HttpResponse('Succefully sent the SMS', mimetype="application/json")