def handle(self, text): if not self.msg.contact: self.respond(self.UNGREGISTERED) return text = text.strip() if not text: self.respond(self.HELP_TEXT) return clinic_code = text[:6] try: location = Location.objects.get(slug=clinic_code) except Location.DoesNotExist: self.respond(self.UNKNOWN_LOCATION, code=clinic_code) return contact = self.msg.contact TrainingSession.objects.create(trainer=contact, location=location) for help_admin in Contact.active.filter(is_help_admin=True): ha_msg = OutgoingMessage( help_admin.default_connection, "Training is starting at %s, %s" ". Notification was sent by %s, %s" % (location.name, location.slug, contact.name, contact.default_connection.identity)) ha_msg.send() self.respond( "Thanks %(name)s for your message that training is " "starting for %(clinic)s. At end of training please send TRAINING STOP", name=contact.name, clinic=location.name)
def handle(self, text): if not self.msg.contact: self.respond(self.UNGREGISTERED) return text = text.strip() if not text: self.respond(self.HELP_TEXT) return clinic_code = text[:6] try: location = Location.objects.get(slug=clinic_code) except Location.DoesNotExist: self.respond(self.UNKNOWN_LOCATION, code=clinic_code) return contact = self.msg.contact TrainingSession.objects.create(trainer=contact, location=location) for help_admin in Contact.active.filter(is_help_admin=True): ha_msg = OutgoingMessage(help_admin.default_connection, "Training is starting at %s, %s" ". Notification was sent by %s, %s" % (location.name, location.slug, contact.name, contact.default_connection.identity)) ha_msg.send() self.respond("Thanks %(name)s for your message that training is " "starting for %(clinic)s. At end of training please send TRAINING STOP", name=contact.name, clinic=location.name)
def status_update(self): """ cron job handler """ from goals.models import Goal self.debug('{0} running'.format(SCHEDULE_DESC)) now = datetime.datetime.now() goals = Goal.active.filter(date_next_notified__lt=now) goals = goals.exclude(schedule_start_date__isnull=True, schedule_frequency='') self.info('found {0} goals'.format(goals.count())) for goal in goals: msg = OutgoingMessage(connection=goal.contact.default_connection, template=self.template, goal=goal.body, month=goal.date_created.strftime('%b')) try: msg.send() except Exception, e: self.exception(e) # Disable this goal if it was a one-time notification (we just # sent it). This will make get_next_date() return None below. if goal.schedule_frequency == 'one-time': goal.schedule_frequency = '' goal.date_last_notified = now goal.date_next_notified = goal.get_next_date() goal.in_session = True goal.save()
def sms_password_complete(request, template_name='accounts/sms_password_done.html'): contact_detail = ContactDetail.objects.get(user=request.user.ilsgatewayuser) default_connection = contact_detail.default_connection if default_connection: m = OutgoingMessage(default_connection, _("Your password is: password")) m.send() return render_to_response(template_name, context_instance=RequestContext(request, {'login_url': settings.LOGIN_URL}))
def _send_question(self, session, msg=None): '''Sends the next question in the session, if there is one''' state = session.state if state and state.question: response = _(state.question.text, get_language_code(session.connection)) self.info("Sending: %s" % response) if msg: msg.respond(response) else: # we need to get the real backend from the router # to properly send it real_backend = self.router.get_backend(session.connection.backend.slug) if real_backend: connection = Connection(real_backend, session.connection.identity) outgoing_msg = OutgoingMessage(connection, response) outgoing_msg.send() else: # todo: do we want to fail more loudly than this? error = "Can't find backend %s. Messages will not be sent" % connection.backend.slug self.error(error)
def send_messages(self): """ send queued for delivery messages """ messages = BroadcastMessage.objects.filter(status="queued")[:50] if messages.count() > 0: self.info("found {0} message(s) to send".format(messages.count())) for message in messages: connection = message.recipient.default_connection msg = OutgoingMessage(connection=connection, template=message.broadcast.body) success = True try: msg.send() except Exception, e: self.exception(e) success = False if success and msg.sent: self.debug("message sent successfully") message.status = "sent" message.date_sent = datetime.datetime.now() else: self.debug("message failed to send") message.status = "error" message.save()
def send_messages(self): """ send queued for delivery messages """ messages = BroadcastMessage.objects.filter(status='queued')[:50] self.info('found {0} message(s) to send'.format(messages.count())) for message in messages: connection = message.recipient.default_connection msg = OutgoingMessage(connection=connection, template=message.broadcast.body) success = True try: msg.send() except Exception, e: self.exception(e) success = False if success and msg.sent: self.debug('message sent successfully') message.status = 'sent' message.date_sent = datetime.datetime.now() else: self.debug('message failed to send') message.status = 'error' message.save()
def handle(self, text): result = text.lower().split() command = result.pop(0) msd_code = result.pop(0) extra = '' while len(result) > 0: extra = extra + ' ' + result.pop(0) if command != 'send_inquiry_message': try: sdp = ServiceDeliveryPoint.objects.get(msd_code=msd_code.upper()) except: self.respond("Invalid msd code %s" % msd_code) return contact_details_to_remind = ContactDetail.objects.filter(service_delivery_point=sdp) if command in ['la']: for contact_detail in contact_details_to_remind: default_connection = contact_detail.default_connection if default_connection: m = OutgoingMessage(default_connection, _("Please send in your adjustments in the format 'la <product> +-<amount> +-<product> +-<amount>...'")) m.send() st = ServiceDeliveryPointStatusType.objects.filter(short_name="lost_adjusted_reminder_sent_facility")[0:1].get() ns = ServiceDeliveryPointStatus(service_delivery_point=contact_detail.service_delivery_point, status_type=st, status_date=datetime.now()) ns.save() self.respond("Sent") if command in ['fw']: for contact_detail in contact_details_to_remind: default_connection = contact_detail.default_connection if default_connection: m = OutgoingMessage(default_connection, _(extra)) m.send() self.respond("Sent '%s'" % _(extra)) if command in ['supervision']: for contact_detail in contact_details_to_remind: default_connection = contact_detail.default_connection if default_connection: m = OutgoingMessage(default_connection, _("Have you received supervision this month? Please reply 'supervision yes' or 'supervision no'")) m.send() st = ServiceDeliveryPointStatusType.objects.filter(short_name="supervision_reminder_sent_facility")[0:1].get() ns = ServiceDeliveryPointStatus(service_delivery_point=contact_detail.service_delivery_point, status_type=st, status_date=datetime.now()) ns.save() self.respond("Sent") if command in ['soh','hmk']: for contact_detail in contact_details_to_remind: default_connection = contact_detail.default_connection if default_connection: m = OutgoingMessage(default_connection, _("Please send in your stock on hand information in the format 'soh <product> <amount> <product> <amount>...'")) m.send() st = ServiceDeliveryPointStatusType.objects.filter(short_name="soh_reminder_sent_facility")[0:1].get() ns = ServiceDeliveryPointStatus(service_delivery_point=contact_detail.service_delivery_point, status_type=st, status_date=datetime.now()) ns.save() self.respond("Sent") elif command in ['si']: product = Product.objects.get(product_code=addl_parameter) if not product: self.respond("Invalid product code %s" % addl_parameter) return if sdp.service_delivery_point_type.name == "FACILITY": contact_details_to_remind = ContactDetail.objects.filter(service_delivery_point=sdp) for contact_detail in contact_details_to_remind: default_connection = contact_detail.default_connection if default_connection: m = OutgoingMessage(default_connection, _("How much %s (msd code %s) do you have in stock? Please respond 'si %s <amount>'" % (product.name, addl_parameter, addl_parameter) )) m.send() self.respond("Sent") else: self.respond("Can only initiate product inquiry for a single facility via SMS - %s is a %s" % (sdp.name, sdp.service_delivery_point_type.name) ) return elif command in ['send_inquiry_message']: product = Product.objects.get(product_code=addl_parameter) if not product: self.respond("Invalid product code %s" % addl_parameter) return try: sdp = ServiceDeliveryPoint.objects.get(id=msd_code) except: self.respond("Invalid ID %s" % msd_code) return if sdp.service_delivery_point_type.name == "FACILITY": contact_details_to_remind = ContactDetail.objects.filter(service_delivery_point=sdp) for contact_detail in contact_details_to_remind: default_connection = contact_detail.default_connection if default_connection: m = OutgoingMessage(default_connection, _("How much %s (msd code %s) do you have in stock? Please respond 'si %s <amount>'" % (product.name, addl_parameter, addl_parameter) )) m.send() elif sdp.service_delivery_point_type.name == "DISTRICT": for facility_sdp in sdp.child_sdps(): contact_details_to_remind = ContactDetail.objects.filter(service_delivery_point=facility_sdp) for contact_detail in contact_details_to_remind: default_connection = contact_detail.default_connection if default_connection: m = OutgoingMessage(default_connection, _("How much %s (msd code %s) do you have in stock? Please respond 'si %s <amount>'" % (product.name, addl_parameter, addl_parameter) )) m.send() elif sdp.service_delivery_point_type.name == "REGION": for district_sdp in sdp.child_sdps(): for facility_sdp in district_sdp.child_sdps: contact_details_to_remind = ContactDetail.objects.filter(service_delivery_point=facility_sdp) for contact_detail in contact_details_to_remind: default_connection = contact_detail.default_connection if default_connection: m = OutgoingMessage(default_connection, _("How much %s (msd code %s) do you have in stock? Please respond 'si %s <amount>'" % (product.name, addl_parameter, addl_parameter) )) m.send() elif command in ['randr']: for contact_detail in contact_details_to_remind: default_connection = contact_detail.default_connection if default_connection: if sdp.service_delivery_point_type.name == "DISTRICT": m = OutgoingMessage(default_connection, _("How many R&R forms have you submitted to MSD? Reply with 'submitted A <number of R&Rs submitted for group A> B <number of R&Rs submitted for group B>'")) m.send() elif sdp.service_delivery_point_type.name == "FACILITY": m = OutgoingMessage(default_connection, _("Have you sent in your R&R form yet for this quarter? Please reply \"submitted\" or \"not submitted\"")) m.send() st = ServiceDeliveryPointStatusType.objects.filter(short_name="r_and_r_reminder_sent_facility")[0:1].get() ns = ServiceDeliveryPointStatus(service_delivery_point=contact_detail.service_delivery_point, status_type=st, status_date=datetime.now()) ns.save() self.respond("Sent") elif command in ['delivery']: for contact_detail in contact_details_to_remind: default_connection = contact_detail.default_connection if default_connection: if contact_detail.service_delivery_point.service_delivery_point_type.name == "FACILITY": m = OutgoingMessage(default_connection, _("Did you receive your delivery yet? Please reply 'delivered <product> <amount> <product> <amount>...'")) m.send() st = ServiceDeliveryPointStatusType.objects.filter(short_name="delivery_received_reminder_sent_facility")[0:1].get() ns = ServiceDeliveryPointStatus(service_delivery_point=contact_detail.service_delivery_point, status_type=st, status_date=datetime.now()) ns.save() elif contact_detail.service_delivery_point.service_delivery_point_type.name == "DISTRICT": m = OutgoingMessage(default_connection, _("Did you receive your delivery yet? Please reply 'delivered' or 'not delivered'")) m.send() st = ServiceDeliveryPointStatusType.objects.filter(short_name="r_and_r_reminder_sent_district")[0:1].get() ns = ServiceDeliveryPointStatus(service_delivery_point=contact_detail.service_delivery_point, status_type=st, status_date=datetime.now()) ns.save() else: self.respond("Sorry there was a problem with your service delivery point setup. Please check via the admin.") self.respond("Sent") elif command in ['latedelivery']: for contact_detail in contact_details_to_remind: default_connection = contact_detail.default_connection if default_connection: service_delivery_point = contact_detail.service_delivery_point kwargs = {"group_name": current_delivering_group(), "group_total": service_delivery_point.child_sdps().filter(delivery_group__name=current_delivering_group()).count(), "not_responded_count": service_delivery_point.child_sdps_not_responded_delivery_this_month(), "not_received_count": service_delivery_point.child_sdps_not_received_delivery_this_month()} m = OutgoingMessage(default_connection, _("Facility deliveries for group %(group_name)s (out of %(group_total)d): %(not_responded_count)d haven't responded and %(not_received_count)d have reported not receiving. See ilsgateway.com"), **kwargs) m.send() self.respond("Sent")
def handle(self, text): result = text.lower().split() command = result.pop(0) msd_code = result.pop(0) extra = '' while len(result) > 0: extra = extra + ' ' + result.pop(0) if command != 'send_inquiry_message': try: sdp = ServiceDeliveryPoint.objects.get( msd_code=msd_code.upper()) except: self.respond("Invalid msd code %s" % msd_code) return contact_details_to_remind = ContactDetail.objects.filter( service_delivery_point=sdp) if command in ['la']: for contact_detail in contact_details_to_remind: default_connection = contact_detail.default_connection if default_connection: m = OutgoingMessage( default_connection, _("Please send in your adjustments in the format 'la <product> +-<amount> +-<product> +-<amount>...'" )) m.send() st = ServiceDeliveryPointStatusType.objects.filter( short_name="lost_adjusted_reminder_sent_facility" )[0:1].get() ns = ServiceDeliveryPointStatus( service_delivery_point=contact_detail. service_delivery_point, status_type=st, status_date=datetime.now()) ns.save() self.respond("Sent") if command in ['fw']: for contact_detail in contact_details_to_remind: default_connection = contact_detail.default_connection if default_connection: m = OutgoingMessage(default_connection, _(extra)) m.send() self.respond("Sent '%s'" % _(extra)) if command in ['supervision']: for contact_detail in contact_details_to_remind: default_connection = contact_detail.default_connection if default_connection: m = OutgoingMessage( default_connection, _("Have you received supervision this month? Please reply 'supervision yes' or 'supervision no'" )) m.send() st = ServiceDeliveryPointStatusType.objects.filter( short_name="supervision_reminder_sent_facility" )[0:1].get() ns = ServiceDeliveryPointStatus( service_delivery_point=contact_detail. service_delivery_point, status_type=st, status_date=datetime.now()) ns.save() self.respond("Sent") if command in ['soh', 'hmk']: for contact_detail in contact_details_to_remind: default_connection = contact_detail.default_connection if default_connection: m = OutgoingMessage( default_connection, _("Please send in your stock on hand information in the format 'soh <product> <amount> <product> <amount>...'" )) m.send() st = ServiceDeliveryPointStatusType.objects.filter( short_name="soh_reminder_sent_facility")[0:1].get() ns = ServiceDeliveryPointStatus( service_delivery_point=contact_detail. service_delivery_point, status_type=st, status_date=datetime.now()) ns.save() self.respond("Sent") elif command in ['si']: product = Product.objects.get(product_code=addl_parameter) if not product: self.respond("Invalid product code %s" % addl_parameter) return if sdp.service_delivery_point_type.name == "FACILITY": contact_details_to_remind = ContactDetail.objects.filter( service_delivery_point=sdp) for contact_detail in contact_details_to_remind: default_connection = contact_detail.default_connection if default_connection: m = OutgoingMessage( default_connection, _("How much %s (msd code %s) do you have in stock? Please respond 'si %s <amount>'" % (product.name, addl_parameter, addl_parameter))) m.send() self.respond("Sent") else: self.respond( "Can only initiate product inquiry for a single facility via SMS - %s is a %s" % (sdp.name, sdp.service_delivery_point_type.name)) return elif command in ['send_inquiry_message']: product = Product.objects.get(product_code=addl_parameter) if not product: self.respond("Invalid product code %s" % addl_parameter) return try: sdp = ServiceDeliveryPoint.objects.get(id=msd_code) except: self.respond("Invalid ID %s" % msd_code) return if sdp.service_delivery_point_type.name == "FACILITY": contact_details_to_remind = ContactDetail.objects.filter( service_delivery_point=sdp) for contact_detail in contact_details_to_remind: default_connection = contact_detail.default_connection if default_connection: m = OutgoingMessage( default_connection, _("How much %s (msd code %s) do you have in stock? Please respond 'si %s <amount>'" % (product.name, addl_parameter, addl_parameter))) m.send() elif sdp.service_delivery_point_type.name == "DISTRICT": for facility_sdp in sdp.child_sdps(): contact_details_to_remind = ContactDetail.objects.filter( service_delivery_point=facility_sdp) for contact_detail in contact_details_to_remind: default_connection = contact_detail.default_connection if default_connection: m = OutgoingMessage( default_connection, _("How much %s (msd code %s) do you have in stock? Please respond 'si %s <amount>'" % (product.name, addl_parameter, addl_parameter))) m.send() elif sdp.service_delivery_point_type.name == "REGION": for district_sdp in sdp.child_sdps(): for facility_sdp in district_sdp.child_sdps: contact_details_to_remind = ContactDetail.objects.filter( service_delivery_point=facility_sdp) for contact_detail in contact_details_to_remind: default_connection = contact_detail.default_connection if default_connection: m = OutgoingMessage( default_connection, _("How much %s (msd code %s) do you have in stock? Please respond 'si %s <amount>'" % (product.name, addl_parameter, addl_parameter))) m.send() elif command in ['randr']: for contact_detail in contact_details_to_remind: default_connection = contact_detail.default_connection if default_connection: if sdp.service_delivery_point_type.name == "DISTRICT": m = OutgoingMessage( default_connection, _("How many R&R forms have you submitted to MSD? Reply with 'submitted A <number of R&Rs submitted for group A> B <number of R&Rs submitted for group B>'" )) m.send() elif sdp.service_delivery_point_type.name == "FACILITY": m = OutgoingMessage( default_connection, _("Have you sent in your R&R form yet for this quarter? Please reply \"submitted\" or \"not submitted\"" )) m.send() st = ServiceDeliveryPointStatusType.objects.filter( short_name="r_and_r_reminder_sent_facility" )[0:1].get() ns = ServiceDeliveryPointStatus( service_delivery_point=contact_detail. service_delivery_point, status_type=st, status_date=datetime.now()) ns.save() self.respond("Sent") elif command in ['delivery']: for contact_detail in contact_details_to_remind: default_connection = contact_detail.default_connection if default_connection: if contact_detail.service_delivery_point.service_delivery_point_type.name == "FACILITY": m = OutgoingMessage( default_connection, _("Did you receive your delivery yet? Please reply 'delivered <product> <amount> <product> <amount>...'" )) m.send() st = ServiceDeliveryPointStatusType.objects.filter( short_name="delivery_received_reminder_sent_facility" )[0:1].get() ns = ServiceDeliveryPointStatus( service_delivery_point=contact_detail. service_delivery_point, status_type=st, status_date=datetime.now()) ns.save() elif contact_detail.service_delivery_point.service_delivery_point_type.name == "DISTRICT": m = OutgoingMessage( default_connection, _("Did you receive your delivery yet? Please reply 'delivered' or 'not delivered'" )) m.send() st = ServiceDeliveryPointStatusType.objects.filter( short_name="r_and_r_reminder_sent_district" )[0:1].get() ns = ServiceDeliveryPointStatus( service_delivery_point=contact_detail. service_delivery_point, status_type=st, status_date=datetime.now()) ns.save() else: self.respond( "Sorry there was a problem with your service delivery point setup. Please check via the admin." ) self.respond("Sent") elif command in ['latedelivery']: for contact_detail in contact_details_to_remind: default_connection = contact_detail.default_connection if default_connection: service_delivery_point = contact_detail.service_delivery_point kwargs = { "group_name": current_delivering_group(), "group_total": service_delivery_point.child_sdps().filter( delivery_group__name=current_delivering_group( )).count(), "not_responded_count": service_delivery_point. child_sdps_not_responded_delivery_this_month(), "not_received_count": service_delivery_point. child_sdps_not_received_delivery_this_month() } m = OutgoingMessage( default_connection, _("Facility deliveries for group %(group_name)s (out of %(group_total)d): %(not_responded_count)d haven't responded and %(not_received_count)d have reported not receiving. See ilsgateway.com" ), **kwargs) m.send() self.respond("Sent")