Esempio n. 1
0
def send_sms(request):

    try:
        input_request = jsonpickle.decode(request.raw_post_data)
        if not (input_request.get(NUMBERS) and input_request.get(MESSAGE)):
            raise ValueError
    except ValueError:
        return HttpResponse(status=400)

    organization = get_organization(request)
    current_month = datetime.date(datetime.datetime.now().year,
                                  datetime.datetime.now().month, 1)
    message_tracker = organization._get_message_tracker(current_month)
    client = SMSClient()
    result = {}
    org_tel_number = organization.tel_number()
    for number in input_request[NUMBERS]:
        if client.send_sms(org_tel_number, number,
                           unicode(input_request[MESSAGE]), MSG_TYPE_API,
                           message_tracker):
            result.update({number: "success"})
        else:
            result.update({number: "failure"})
    return HttpResponse(jsonpickle.encode(result, unpicklable=False),
                        content_type="application/javascript")
def send_sms(request):
    input_request = jsonpickle.decode(request.raw_post_data)
    organization = get_organization(request)
    client = SMSClient()
    result = {}
    org_tel_number = organization.tel_number()
    for number in input_request['numbers']:
        if client.send_sms(org_tel_number, number, unicode(input_request['message']), MSG_TYPE_API):
            result.update({number: "success"})
            organization.increment_sms_api_usage_count()
        else:
            result.update({number: "failure"})
    return HttpResponse(jsonpickle.encode(result, unpicklable=False), content_type="application/javascript")
Esempio n. 3
0
def send_reminders():
    """
    Entry point for the scheduler. Sends out reminders for the day.
    """
    now = datetime.now()
    send_reminders_scheduled_on(date(now.year, now.month, now.day),
                                SMSClient())
Esempio n. 4
0
def broadcast_message(data_sender_phone_numbers,
                      message,
                      organization_tel_number,
                      other_numbers,
                      message_tracker,
                      country_code=None):
    """

    :param data_sender_phone_numbers:
    :param message:
    :param organization_tel_number:
    :param other_numbers:
    :param message_tracker:
    :param country_code:
    :return:
    """
    sms_client = SMSClient()
    sms_sent = None
    failed_numbers = []
    for phone_number in data_sender_phone_numbers:
        if phone_number is not None and phone_number != TEST_REPORTER_MOBILE_NUMBER:
            logger.info(("Sending broadcast message to %s from %s") %
                        (phone_number, organization_tel_number))
            sms_sent = sms_client.send_sms(organization_tel_number,
                                           phone_number, message,
                                           MSG_TYPE_USER_MSG, message_tracker)
        if sms_sent:
            message_tracker.increment_message_count_for(send_message_count=1)
        else:
            failed_numbers.append(phone_number)

    for number in other_numbers:
        number = number.strip()
        number_with_country_prefix = number
        if country_code:
            number_with_country_prefix = "%s%s" % (
                country_code, re.sub(r"^[ 0]+", "", number))

        logger.info(("Sending broadcast message to %s from %s") %
                    (number_with_country_prefix, organization_tel_number))
        sms_sent = sms_client.send_sms(organization_tel_number,
                                       number_with_country_prefix, message,
                                       MSG_TYPE_USER_MSG, message_tracker)
        if not sms_sent:
            failed_numbers.append(number)

    return failed_numbers
def broadcast_message(data_senders, message, organization_tel_number, other_numbers, message_tracker, country_code=None):
    """

    :param data_senders:
    :param message:
    :param organization_tel_number:
    :param other_numbers:
    :param message_tracker:
    :param country_code:
    :return:
    """
    sms_client = SMSClient()
    sms_sent = None
    failed_numbers = []
    for data_sender in data_senders:
        phone_number = data_sender.get(
            'mobile_number') #This should not be a dictionary but the API in import_data should be fixed to return entity
        if phone_number is not None:
            logger.info(("Sending broadcast message to %s from %s") % (phone_number, organization_tel_number))
            sms_sent = sms_client.send_sms(organization_tel_number, phone_number, message, MSG_TYPE_USER_MSG)
        if sms_sent:
            message_tracker.increment_message_count_for(send_message_count=1)
        else:
            failed_numbers.append(phone_number)

    for number in other_numbers:
        number = number.strip()
        number_with_country_prefix = number
        if country_code:
            number_with_country_prefix = "%s%s" % (country_code, re.sub(r"^[ 0]+", "", number))

        logger.info(("Sending broadcast message to %s from %s") % (number_with_country_prefix, organization_tel_number))
        sms_sent = sms_client.send_sms(organization_tel_number, number_with_country_prefix, message, MSG_TYPE_USER_MSG)
        if sms_sent:
            message_tracker.increment_message_count_for(send_message_count=1)
        else:
            failed_numbers.append(number)

    return failed_numbers
Esempio n. 6
0
def broadcast_message(data_senders, message, organization_tel_number, other_numbers, message_tracker, country_code=None):
    sms_client = SMSClient()
    sms_sent = None
    for data_sender in data_senders:
        phone_number = data_sender.get(
            'mobile_number') #This should not be a dictionary but the API in import_data should be fixed to return entity
        if phone_number is not None:
            logger.info(("Sending broadcast message to %s from %s") % (phone_number, organization_tel_number))
            sms_sent = sms_client.send_sms(organization_tel_number, phone_number, message)
        if sms_sent:
            message_tracker.increment_outgoing_message_count_by(1)

    for number in other_numbers:
        number = number.strip()
        if country_code:
            number = "%s%s" % (country_code, number[1:])

        logger.info(("Sending broadcast message to %s from %s") % (number, organization_tel_number))
        sms_sent = sms_client.send_sms(organization_tel_number, number, message)
        if sms_sent:
            message_tracker.increment_outgoing_message_count_by(1)

    return sms_sent
Esempio n. 7
0
    def setUp(self):
        self.FROM_NUMBER = "from_num"
        self.mock_date = Mock(spec=date)
        self.data_senders = [{
            'name': 'reporter1',
            'mobile_number': 'tel1'
        }, {
            'name': 'reporter2',
            'mobile_number': 'tel2'
        }, {
            'name': 'reporter3',
            'mobile_number': 'tel3'
        }, {
            'name': 'reporter4',
            'mobile_number': 'tel4'
        }]
        self.project = Mock(spec=Project)
        self.project.get_data_senders.return_value = self.data_senders

        self.reminder1 = Mock(spec=Reminder)
        self.reminder1.should_be_send_on.return_value = True
        self.reminder1.message = 'reminder1 message'
        self.reminder1.remind_to = RemindTo.ALL_DATASENDERS
        self.reminder1.get_sender_list.return_value = self.data_senders

        self.reminder2 = Mock(spec=Reminder)
        self.reminder2.should_be_send_on.return_value = False
        self.reminder2.message = 'reminder2 message'
        self.reminder2.remind_to = RemindTo.ALL_DATASENDERS
        self.reminder2.get_sender_list.return_value = self.data_senders

        self.reminder3 = Mock(spec=Reminder)
        self.reminder3.should_be_send_on.return_value = True
        self.reminder3.message = 'reminder3 message'
        self.reminder3.remind_to = RemindTo.ALL_DATASENDERS
        self.reminder3.get_sender_list.return_value = self.data_senders

        self.reminder4 = Mock(spec=Reminder)
        self.reminder4.should_be_send_on.return_value = True
        self.reminder4.message = 'reminder4 message'
        self.reminder4.remind_to = RemindTo.ALL_DATASENDERS
        self.reminder4.get_sender_list.return_value = Exception()

        self.reminders = [self.reminder1, self.reminder2, self.reminder3]
        self.sms_client = SMSClient()
        self.sms_client.send_sms = Mock()
        self.sms_client.send_sms.return_value = True
Esempio n. 8
0
        reminder for reminder in reminders
        if reminder.should_be_send_on(project.deadline(), on_date)
    ]
    for reminder in reminders_to_be_sent:
        #send next reminder if their is any error in sending reminder
        smses_sent = 0
        try:
            smses_sent = sms_client.send_reminder(from_number, on_date,
                                                  project, reminder, dbm)
        except Exception:
            logger.exception("Exception while sending Reminder")

        if smses_sent > 0:
            total_sent += smses_sent
            reminders_sent.append(reminder)
    logger.info("Reminders scheduled: %d " % len(reminders_to_be_sent))
    logger.info("Reminders sent: %d " % len(reminders_sent))
    return reminders_sent, total_sent


def _get_reminders_grouped_by_project_for_organization(organization_id):
    reminders_grouped_project_id = defaultdict(list)
    for reminder in get_reminder_repository().get_all_reminders_for(
            organization_id):
        reminders_grouped_project_id[reminder.project_id].append(reminder)
    return reminders_grouped_project_id


if __name__ == "__main__":
    send_reminders_scheduled_on(date(2011, 10, 20), SMSClient())