def check_retry_alarm(alarm_request_id): obj_alarmreq = AlarmRequest.objects.get(id=alarm_request_id) if obj_alarmreq.alarm.maxretry >= obj_alarmreq.alarm.num_attempt: obj_alarmreq.update_status(ALARMREQUEST_STATUS.RETRY) obj_alarmreq.alarm.retry_alarm() else: obj_alarmreq.update_status(ALARMREQUEST_STATUS.FAILURE) #Check phonenumber_sms_failure if obj_alarmreq.alarm.phonenumber_sms_failure: # TODO: Use template SMS key (failure_reach) with this text as default failure_sms = "we haven't been able to reach '" \ + str(obj_alarmreq.alarm.alarm_phonenumber) \ + "' after trying " + str(obj_alarmreq.alarm.num_attempt) \ + " times" sms_obj = Message.objects.create( content=failure_sms, recipient_number=obj_alarmreq.alarm.phonenumber_sms_failure, sender=obj_alarmreq.alarm.survey.user, content_type=ContentType.objects.get(model='alarmrequest'), object_id=obj_alarmreq.id, ) try: calendar_user = obj_alarmreq.alarm.event.calendar.user calendar_setting = CalendarUserProfile.objects.get( user=calendar_user).calendar_setting SendMessage.delay(sms_obj.id, calendar_setting.sms_gateway_id) except: SendMessage.delay(sms_obj.id) print "Sent SMS Failure alarm : %s" % str( obj_alarmreq.alarm.alarm_phonenumber)
def check_retry_alarm(alarm_request_id): obj_alarmreq = AlarmRequest.objects.get(id=alarm_request_id) if obj_alarmreq.alarm.maxretry >= obj_alarmreq.alarm.num_attempt: obj_alarmreq.update_status(ALARMREQUEST_STATUS.RETRY) obj_alarmreq.alarm.retry_alarm() else: obj_alarmreq.update_status(ALARMREQUEST_STATUS.FAILURE) #Check phonenumber_sms_failure if obj_alarmreq.alarm.phonenumber_sms_failure: # TODO: Use template SMS key (failure_reach) with this text as default failure_sms = "we haven't been able to reach '" \ + str(obj_alarmreq.alarm.alarm_phonenumber) \ + "' after trying " + str(obj_alarmreq.alarm.num_attempt) \ + " times" sms_obj = Message.objects.create( content=failure_sms, recipient_number=obj_alarmreq.alarm.phonenumber_sms_failure, sender=obj_alarmreq.alarm.survey.user, content_type=ContentType.objects.get(model='alarmrequest'), object_id=obj_alarmreq.id, ) try: calendar_user = obj_alarmreq.alarm.event.calendar.user calendar_setting = CalendarUserProfile.objects.get(user=calendar_user).calendar_setting SendMessage.delay(sms_obj.id, calendar_setting.sms_gateway_id) except: SendMessage.delay(sms_obj.id) print "Sent SMS Failure alarm : %s" % str(obj_alarmreq.alarm.alarm_phonenumber)
def run(self, **kwargs): #logger = self.get_logger(**kwargs) logger.warning("[SMS_TASK] TASK :: RESEND sms") for sms_campaign in SMSCampaign.objects.get_running_sms_campaign(): logger.info("[SMS_TASK] => SMS Campaign name %s (id:%s)" % (sms_campaign.name, sms_campaign.id)) sms_maxretry = get_sms_maxretry(sms_campaign) limit = 1000 list_subscriber = SMSCampaignSubscriber.objects.filter( sms_campaign=sms_campaign, status=SMS_SUBSCRIBER_STATUS.IN_PROCESS)[:limit] if not list_subscriber: #Go to the next campaign logger.info("[SMS_TASK] No subscribers in this campaign (id:%s)" % (sms_campaign.id)) continue for subscriber in list_subscriber: if not subscriber.message: logger.error("[SMS_TASK] => SMS with No Message") subscriber.status = SMS_SUBSCRIBER_STATUS.FAIL # 'FAIL' subscriber.save() continue logger.warning("[SMS_TASK] => SMS Message Status = %s" % subscriber.message.status) if subscriber.message.status == 'Failed': # check sms_maxretry if subscriber.count_attempt >= sms_maxretry: subscriber.status = SMS_SUBSCRIBER_STATUS.FAIL # 'FAIL' subscriber.save() else: text_message = subscriber.contact.replace_tag(subscriber.sms_campaign.text_message) logger.info("[SMS_TASK] SendMessage text_message:%s" % text_message) # Create Message object msg_obj = SMSMessage.objects.create( content=text_message, recipient_number=subscriber.contact.contact, sender=subscriber.sms_campaign.user, sender_number=subscriber.sms_campaign.callerid, status='Unsent', content_type=ContentType.objects.get(model='smscampaignsubscriber', app_label='sms_module'), object_id=subscriber.id, sms_campaign=sms_campaign, ) # Send sms SendMessage.delay(msg_obj.id, subscriber.sms_campaign.sms_gateway_id) subscriber.message = msg_obj subscriber.last_attempt = datetime.utcnow().replace(tzinfo=utc) subscriber.count_attempt += 1 subscriber.save() if subscriber.message.status == 'Sent' or subscriber.message.status == 'Delivered': subscriber.status = SMS_SUBSCRIBER_STATUS.COMPLETE # 'COMPLETE' subscriber.save()
def perform_alarm(obj_event, obj_alarm): """ Task to perform the alarm, this will send the alarms via several mean such as Call, SMS and Email """ logger.info("TASK :: perform_alarm -> %d-%s" % (obj_alarm.id, obj_alarm.method)) if obj_alarm.method == ALARM_METHOD.CALL: # send alarm via CALL AlarmRequest.objects.create(alarm=obj_alarm, date=datetime.utcnow().replace(tzinfo=utc)) elif obj_alarm.method == ALARM_METHOD.SMS: # send alarm via SMS try: caluser_profile = CalendarUserProfile.objects.get( user=obj_alarm.event.creator) except CalendarUserProfile.DoesNotExist: logger.error( "Error retrieving CalendarUserProfile (ALARM_METHOD.SMS)") return False callerid = caluser_profile.calendar_setting.callerid sms_gateway = caluser_profile.calendar_setting.sms_gateway # Create Message object msg_obj = SMSMessage.objects.create( content=obj_alarm.sms_template.sms_text, recipient_number=obj_alarm.alarm_phonenumber, sender=obj_alarm.event.creator, sender_number=callerid, status='Unsent', content_type=ContentType.objects.get(model='alarm', app_label='appointment'), object_id=obj_alarm.id, sms_gateway=sms_gateway, ) # Send sms logger.warning( "[perform_alarm - SendMessage] Call msg_obj id:%d - gateway_id:%d" % (msg_obj.id, sms_gateway.id)) SendMessage.delay(msg_obj.id, sms_gateway.id) # Mark the Alarm as SUCCESS obj_alarm.status = ALARM_STATUS.SUCCESS obj_alarm.save() elif obj_alarm.method == ALARM_METHOD.EMAIL: # send alarm via EMAIL if obj_alarm.alarm_email and obj_alarm.mail_template: # create MailSpooler object MailSpooler.objects.create(mailtemplate=obj_alarm.mail_template, contact_email=obj_alarm.alarm_email) # Mark the Alarm as SUCCESS obj_alarm.status = ALARM_STATUS.SUCCESS obj_alarm.save()
def perform_alarm(obj_event, obj_alarm): """ Task to perform the alarm, this will send the alarms via several mean such as Call, SMS and Email """ logger.info("TASK :: perform_alarm -> %d-%s" % (obj_alarm.id, obj_alarm.method)) if obj_alarm.method == ALARM_METHOD.CALL: # send alarm via CALL AlarmRequest.objects.create( alarm=obj_alarm, date=datetime.utcnow().replace(tzinfo=utc) ) elif obj_alarm.method == ALARM_METHOD.SMS: # send alarm via SMS try: caluser_profile = CalendarUserProfile.objects.get(user=obj_alarm.event.creator) except CalendarUserProfile.DoesNotExist: logger.error("Error retrieving CalendarUserProfile (ALARM_METHOD.SMS)") return False callerid = caluser_profile.calendar_setting.callerid sms_gateway = caluser_profile.calendar_setting.sms_gateway # Create Message object msg_obj = SMSMessage.objects.create( content=obj_alarm.sms_template.sms_text, recipient_number=obj_alarm.alarm_phonenumber, sender=obj_alarm.event.creator, sender_number=callerid, status='Unsent', content_type=ContentType.objects.get(model='alarm', app_label='appointment'), object_id=obj_alarm.id, sms_gateway=sms_gateway, ) # Send sms logger.warning("[perform_alarm - SendMessage] Call msg_obj id:%d - gateway_id:%d" % (msg_obj.id, sms_gateway.id)) SendMessage.delay(msg_obj.id, sms_gateway.id) # Mark the Alarm as SUCCESS obj_alarm.status = ALARM_STATUS.SUCCESS obj_alarm.save() elif obj_alarm.method == ALARM_METHOD.EMAIL: # send alarm via EMAIL if obj_alarm.alarm_email and obj_alarm.mail_template: # create MailSpooler object MailSpooler.objects.create( mailtemplate=obj_alarm.mail_template, contact_email=obj_alarm.alarm_email ) # Mark the Alarm as SUCCESS obj_alarm.status = ALARM_STATUS.SUCCESS obj_alarm.save()
def run(self, **kwargs): # start_from = datetime.utcnow().replace(tzinfo=utc) # list_sms = SMSMessage.objects.filter(delivery_date__lte=start_from, status='Unsent', sms_campaign__isnull=True) list_sms = SMSMessage.objects.filter(status='Unsent', sms_campaign__isnull=True) logger.warning("[SMS_TASK] TASK :: Check spool_sms_nocampaign -> COUNT SMS (%d)" % list_sms.count()) for sms in list_sms: logger.debug("[SMS_TASK] => SMS Message (id:%d - phonenumber:%s)" % (sms.id, sms.sender_number)) # Send SMS SendMessage.delay(sms.id, sms.sms_gateway_id)
def run(self, **kwargs): #logger = self.get_logger(**kwargs) #start_from = datetime.utcnow().replace(tzinfo=utc) #list_sms = SMSMessage.objects.filter(delivery_date__lte=start_from, status='Unsent', sms_campaign__isnull=True) list_sms = SMSMessage.objects.filter(status='Unsent', sms_campaign__isnull=True) logger.warning("[SMS_TASK] TASK :: Check spool_sms_nocampaign -> COUNT SMS (%d)" % list_sms.count()) for sms in list_sms: logger.debug("[SMS_TASK] => SMS Message (id:%d - phonenumber:%s)" % (sms.id, sms.sender_number)) # Send SMS SendMessage.delay(sms.id, sms.sms_gateway_id)
def init_smsrequest(obj_subscriber, obj_sms_campaign): """This task outbounds the call **Attributes**: * ``obj_subscriber`` - SMSCampaignSubscriber object * ``user`` - User object """ logger.warning("init_smsrequest contact:%s" % obj_subscriber.contact.contact) maxretry = get_sms_maxretry(obj_sms_campaign) # Update SMSCampaignSubscriber if obj_subscriber.count_attempt <= maxretry: if obj_subscriber.count_attempt is None or not obj_subscriber.count_attempt >= 0: obj_subscriber.count_attempt = 1 else: obj_subscriber.count_attempt += 1 text_message = obj_subscriber.contact.replace_tag( obj_subscriber.sms_campaign.text_message) # Create Message object msg_obj = SMSMessage.objects.create( content=text_message, recipient_number=obj_subscriber.contact.contact, sender=obj_subscriber.sms_campaign.user, sender_number=obj_subscriber.sms_campaign.callerid, status='Unsent', content_type=ContentType.objects.get( model='smscampaignsubscriber'), object_id=obj_subscriber.id, sms_campaign=obj_sms_campaign, ) obj_subscriber.message = msg_obj obj_subscriber.last_attempt = datetime.utcnow().replace(tzinfo=utc) obj_subscriber.save() # Send sms logger.warning("Call msg_obj id:%d - gateway_id:%d" % (msg_obj.id, obj_sms_campaign.sms_gateway_id)) SendMessage.delay(msg_obj.id, obj_sms_campaign.sms_gateway_id) else: logger.error("Max retry exceeded, sub_id:%s" % obj_subscriber.id) return True
def check_retry_alarm(alarm_request_id): obj_alarmreq = AlarmRequest.objects.get(id=alarm_request_id) if obj_alarmreq.alarm.maxretry >= obj_alarmreq.alarm.num_attempt: logger.info('Alarm maxretry >= num_attempt') obj_alarmreq.update_status(ALARMREQUEST_STATUS.RETRY) obj_alarmreq.alarm.retry_alarm() else: logger.info('Alarm maxretry Not >= num_attempt') obj_alarmreq.update_status(ALARMREQUEST_STATUS.FAILURE) # Check phonenumber_sms_failure if obj_alarmreq.alarm.phonenumber_sms_failure: if obj_alarmreq.alarm.event.data and 'sms_failure' in obj_alarmreq.alarm.event.data: # Custom SMS Failure failure_sms = obj_alarmreq.alarm.event.data['sms_failure'] failure_sms = failure_sms.replace( '#alarm_phonenumber#', str(obj_alarmreq.alarm.alarm_phonenumber)) failure_sms = failure_sms.replace( '#num_attempt#', str(obj_alarmreq.alarm.num_attempt)) else: # Default SMS Failure failure_sms = "we haven't been able to reach '" \ + str(obj_alarmreq.alarm.alarm_phonenumber) \ + "' after trying " + str(obj_alarmreq.alarm.num_attempt) \ + " times" sms_obj = Message.objects.create( content=failure_sms, recipient_number=obj_alarmreq.alarm.phonenumber_sms_failure, sender=obj_alarmreq.alarm.survey.user, content_type=ContentType.objects.get(model='alarmrequest'), object_id=obj_alarmreq.id, ) try: calendar_user = obj_alarmreq.alarm.event.calendar.user calendar_setting = CalendarUserProfile.objects.get( user=calendar_user).calendar_setting SendMessage.delay(sms_obj.id, calendar_setting.sms_gateway_id) except: SendMessage.delay(sms_obj.id) print "Sent SMS Failure alarm : %s" % str( obj_alarmreq.alarm.alarm_phonenumber)
def init_smsrequest(obj_subscriber, obj_sms_campaign): """This task outbounds the call **Attributes**: * ``obj_subscriber`` - SMSCampaignSubscriber object * ``user`` - User object """ logger.info("[SMS_TASK] init_smsrequest contact:%s" % obj_subscriber.contact.contact) maxretry = get_sms_maxretry(obj_sms_campaign) # Update SMSCampaignSubscriber if obj_subscriber.count_attempt <= maxretry: if obj_subscriber.count_attempt is None or not obj_subscriber.count_attempt >= 0: obj_subscriber.count_attempt = 1 else: obj_subscriber.count_attempt += 1 text_message = obj_subscriber.contact.replace_tag(obj_subscriber.sms_campaign.text_message) # Create Message object msg_obj = SMSMessage.objects.create( content=text_message, recipient_number=obj_subscriber.contact.contact, sender=obj_subscriber.sms_campaign.user, sender_number=obj_subscriber.sms_campaign.callerid, status='Unsent', content_type=ContentType.objects.get(model='smscampaignsubscriber', app_label='sms_module'), object_id=obj_subscriber.id, sms_campaign=obj_sms_campaign, ) obj_subscriber.message = msg_obj obj_subscriber.last_attempt = datetime.utcnow().replace(tzinfo=utc) obj_subscriber.save() # Send sms logger.warning("[SMS_TASK] Call msg_obj id:%d - gateway_id:%d" % (msg_obj.id, obj_sms_campaign.sms_gateway_id)) SendMessage.delay(msg_obj.id, obj_sms_campaign.sms_gateway_id) else: logger.error("[SMS_TASK] Max retry exceeded, sub_id:%s" % obj_subscriber.id) return True
def check_retry_alarm(alarm_request_id): obj_alarmreq = AlarmRequest.objects.get(id=alarm_request_id) if obj_alarmreq.alarm.maxretry >= obj_alarmreq.alarm.num_attempt: logger.info('Alarm maxretry >= num_attempt') obj_alarmreq.update_status(ALARMREQUEST_STATUS.RETRY) obj_alarmreq.alarm.retry_alarm() else: logger.info('Alarm maxretry Not >= num_attempt') obj_alarmreq.update_status(ALARMREQUEST_STATUS.FAILURE) #Check phonenumber_sms_failure if obj_alarmreq.alarm.phonenumber_sms_failure: if obj_alarmreq.alarm.event.data and 'sms_failure' in obj_alarmreq.alarm.event.data: #Custom SMS Failure failure_sms = obj_alarmreq.alarm.event.data['sms_failure'] failure_sms = failure_sms.replace('#alarm_phonenumber#', str(obj_alarmreq.alarm.alarm_phonenumber)) failure_sms = failure_sms.replace('#num_attempt#', str(obj_alarmreq.alarm.num_attempt)) else: #Default SMS Failure failure_sms = "we haven't been able to reach '" \ + str(obj_alarmreq.alarm.alarm_phonenumber) \ + "' after trying " + str(obj_alarmreq.alarm.num_attempt) \ + " times" sms_obj = Message.objects.create( content=failure_sms, recipient_number=obj_alarmreq.alarm.phonenumber_sms_failure, sender=obj_alarmreq.alarm.survey.user, content_type=ContentType.objects.get(model='alarmrequest'), object_id=obj_alarmreq.id, ) try: calendar_user = obj_alarmreq.alarm.event.calendar.user calendar_setting = CalendarUserProfile.objects.get(user=calendar_user).calendar_setting SendMessage.delay(sms_obj.id, calendar_setting.sms_gateway_id) except: SendMessage.delay(sms_obj.id) print "Sent SMS Failure alarm : %s" % str(obj_alarmreq.alarm.alarm_phonenumber)