Exemple #1
0
def queue(simulate, simulated_date):
    try:
        if simulated_date:
            dt = arrow.get(simulated_date)
        else:
            dt = arrow.utcnow()
        scheduler = Scheduler()
        print 'getting messages'
        msgs = scheduler.get_messages()
        logging.info("Starting MessageManager")
        logging.info("messages = %s" % len(msgs))
        if len(msgs) == 0:
            click.echo("No messages are ready to be queued")
        else:
            click.echo("Number of messages to be scheduled: %s" % len(msgs))
        for m in msgs:
            if not simulate:
                logging.info("Getting person %s " % m.person_name)
                pm = PersonManager()
                p = pm.get_person(m.person_name)
                if not p:
                    logging.warn(m.person_name +
                                 "does not have an entry in the " +
                                 "Person table")
                    continue
                if p.all_available_count(dt) == 0:
                    logging.warn('No locations available for %s' %
                                 m.person_name)
                    click.echo('No locations available for %s' % m.person_name)
                    continue
                avail_windows = p.all_available(dt)
                click.echo('# of locations avail: {}, last_loc={}'.format(
                    p.all_available_count(dt), m.last_loc))
                if m.last_loc == p.all_available_count(dt) - 1:
                    click.echo('Resetting to first location')
                    idx = 0
                else:
                    click.echo('Moving to next location')
                    idx = m.last_loc + 1

                active_window = avail_windows[int(idx)]
                next_exp = m.next_expiration_utc.isoformat()
                mm = MessageManager(LocationName=active_window.location_name)
                click.echo("Publishing message for person %s to location %s" %
                           (m.person_name, active_window.location_name))
                mm.publish_message(Body=m.body,
                                   UUID=m.uuid_key,
                                   PersonName=m.person_name,
                                   NoMoreOccurrences=m.no_more_occurrences,
                                   ExpirationDateTimeInUtc=next_exp)
                scheduler.update_queue_status(m.uuid_key, m.person_name, True)
                scheduler.update_last_location(m.uuid_key, m.person_name, idx)
            else:
                click.echo("Publishing message(simulated):")
                click.echo(str(m))

    except Exception:
        print 'here'
        click.echo(traceback.print_exc())
        raise
 def test_can_read_one_message_from_queue(self):
     message_manager_under_test = MessageManager(DeviceName="TEST")
     message_manager_under_test.publish_message(
             UUID="test",
             ExpirationDateTimeInUtc="2013-01-01 11:00",
             Body="Hey there")
     message_manager_under_test.get_messages()
     self.assertEqual(1, len(message_manager_under_test.messages))
def queue_message(intent, session):

    card_title = intent['name']
    session_attributes = {}
    should_end_session = False
    reprompt_text = 'Excuse me?'
    if 'person' in intent['slots'] and 'message' in intent['slots']:
        if 'voice' in intent['slots']:
            voice = str(intent['slots']['voice']['value'])
        else:
            voice = 'Joanna'
        person = str(intent['slots']['person']['value']).lower()
        name = intent['slots']['message']['value']
        dt = arrow.utcnow()
        pm = PersonManager()
        p = pm.get_person(person)
        lm = LibraryManager()
        m = lm.get_message(Name=name)
        if not p:
            logging.error('{} does not have an entry in the '
                          'Person table . . .'.format(person))
            speech_output = "Sorry, I don't know the name {}" \
                            .format(person)
            should_end_session = True

        elif not m:
            logging.error('There is no message named {}'.format(name))
            speech_output = "Sorry, I don't have a message named {}" \
                            .format(name)
            should_end_session = True

        elif p.all_available_count(dt) == 0:
            logging.error('No locations are available for {}'.format(person))
            speech_output = "Sorry, there are no locations for {}" \
                            .format(name)
            should_end_session = True

        else:
            active_window = p.all_available(dt)[0]
            mm = MessageManager(LocationName=active_window.location_name)
            speech_output = "Got it. Publishing message {} to {} at " \
                            "location {}".format(name,
                                                 person,
                                                 active_window.location_name)
            mm.publish_message(Body=m['message'],
                               PersonName=person,
                               Voice=voice)
            should_end_session = True
    else:
        logging.error('Missing person or message')
        speech_output = 'Person or message name is missing. Please try again'
        should_end_session = True

    return build_response(
        session_attributes,
        build_speechlet_response(card_title, speech_output, reprompt_text,
                                 should_end_session))
 def test_expired_message_is_expired(self):
     message_manager_under_test = MessageManager(DeviceName="TEST")
     expiration_date = arrow.utcnow().replace(days=-7)
     message_manager_under_test.publish_message(
             UUID='test',
             ExpirationDateTimeInUtc=expiration_date.isoformat(),
             Body="Hey there")
     message_manager_under_test.get_messages()
     polexa_message_under_test = message_manager_under_test.messages[0]
     self.assertTrue(polexa_message_under_test.is_expired)
 def test_can_publish_message_with_uuid(self):
     body = 'hey there'
     message_manager_under_test = MessageManager(DeviceName="TEST")
     message_manager_under_test.publish_message(
             UUID="test_uuid",
             ExpirationDateTimeInUtc="2013-01-01 11:00",
             Body=body)
     message_manager_under_test.get_messages()
     polexa_message_under_test = message_manager_under_test.messages[0]
     assert polexa_message_under_test.uuid_key == "test_uuid"
def handler(event, context):
    scheduler = Scheduler()
    dt = arrow.utcnow()
    logging.info("Getting messages")
    msgs = scheduler.get_messages()
    logging.info("messages = %s" % len(msgs))
    if len(msgs) == 0:
        logging.info("No messages are ready to be queued")
    else:
        logging.info("Number of messages to be scheduled: %s" % len(msgs))

    for m in msgs:
        logging.info("Getting person %s " % m.person_name)
        pm = PersonManager()
        p = pm.get_person(m.person_name)
        if not p:
            logging.warn(m.person_name + "does not have an entry in the " +
                         "Person table . . . skipping")
            continue
        if p.all_available_count(dt) == 0:
            logging.warn('No locations available for %s . . . skipping' %
                         m.person_name)
            continue
        avail_windows = p.all_available(dt)
        logging.info('# of locations avail: {}, last_loc={}'.format(
            p.all_available_count(dt), m.last_loc))
        if p.all_available_count(dt) > 1 and \
                m.last_loc == p.all_available_count(dt)-1:
            logging.info('Resetting to first location')
            idx = 0
        else:
            if p.all_available_count(dt) > 1:
                logging.info('Moving to next location')
                idx = m.last_loc + 1
            else:
                idx = 0

            active_window = avail_windows[int(idx)]
            next_exp = m.next_expiration_utc.isoformat()
            mm = MessageManager(LocationName=active_window.location_name)
            logging.info("Publishing message for person %s to location %s" %
                         (m.person_name, active_window.location_name))
            mm.publish_message(Body=m.body,
                               UUID=m.uuid_key,
                               PersonName=m.person_name,
                               NoMoreOccurrences=m.no_more_occurrences,
                               BotNames=m.bot_names,
                               IceBreaker=m.ice_breaker,
                               RequiredBots=m.required_bots,
                               ExpirationDateTimeInUtc=next_exp)
            scheduler.update_queue_status(m.uuid_key, m.person_name, True)
            scheduler.update_last_location(m.uuid_key, m.person_name, idx)
 def test_can_publish_message_with_expiration(self):
     body = 'hey there'
     expire_datetime_in_utc = '2016-12-31 01:00 UTC'
     message_manager_under_test = MessageManager(DeviceName="TEST")
     message_manager_under_test.publish_message(
             UUID='test',
             ExpirationDateTimeInUtc=expire_datetime_in_utc,
             Body=body)
     message_manager_under_test.get_messages()
     polexa_message_under_test = message_manager_under_test.messages[0]
     self.assertEqual(
             arrow.get(expire_datetime_in_utc),
             polexa_message_under_test.expiration_datetime_in_utc)
 def test_can_parse_valid_expiration_timestamp(self):
     message_manager_under_test = MessageManager(DeviceName="TEST")
     message_manager_under_test.publish_message(
             UUID="test",
             ExpirationDateTimeInUtc="2013-01-01 11:00",
             Body="Hey there")
     message_manager_under_test.get_messages()
     polexa_message_under_test = message_manager_under_test.messages[0]
     self.assertEqual(
             polexa_message_under_test.expiration_datetime_in_utc.year,
             2013)
     self.assertEqual(
             polexa_message_under_test.expiration_datetime_in_utc.hour,
             11)
 def test_can_properly_write_speech(self):
     message1 = "Hey there 1"
     message2 = "Hey there 2"
     final_speech = "<speak><p>Hey there 1</p><p>Hey there 2</p></speak>"
     message_manager_under_test = MessageManager(DeviceName="TEST")
     message_manager_under_test.publish_message(
             UUID="test",
             ExpirationDateTimeInUtc="2018-01-01 11:00",
             Body=message1)
     message_manager_under_test.publish_message(
             UUID="test",
             ExpirationDateTimeInUtc="2018-01-01 11:00",
             Body=message2)
     self.assertEqual(message_manager_under_test.write_speech(),
                      final_speech)
Exemple #10
0
def queue(simulate, simulated_date, verbose):
    log = logging.getLogger('PollexyCli')
    if verbose:
        os.environ['LOG_LEVEL'] = 'DEBUG'
        log.setLevel(logging.DEBUG)
    try:
        if simulated_date:
            dt = arrow.get(simulated_date)
        else:
            dt = arrow.utcnow()
        scheduler = Scheduler()
        msgs = scheduler.get_messages()
        if len(msgs) == 0:
            click.echo("No messages are ready to be queued")
            return

        log.debug("Number of messages to be scheduled: %s" % len(msgs))
        for m in msgs:
            if not simulate:
                pm = PersonManager()
                p = pm.get_person(m.person_name)
                if not p:
                    log.warn(m.person_name +
                             "does not have an entry in the " +
                             "Person table")
                    continue
                if p.all_available_count(dt) == 0:
                    log.debug('No locations available for %s' %
                              m.person_name)
                    continue
                avail_windows = p.all_available(dt)
                log.debug('# of locations avail: {}, last_loc={}'
                          .format(p.all_available_count(dt),
                                  m.last_loc))
                if m.last_loc == p.all_available_count(dt)-1:
                    log.debug('Resetting to first location')
                    idx = 0
                else:
                    log.debug('Moving to next location')
                    idx = m.last_loc + 1

                active_window = avail_windows[int(idx)]
                next_exp = m.next_expiration_utc.isoformat()
                mm = MessageManager(LocationName=active_window.location_name)
                log.debug("Publishing message for person %s to location %s"
                          % (m.person_name, active_window.location_name))
                mm.publish_message(Body=m.body, UUID=m.uuid_key,
                                   PersonName=m.person_name,
                                   NoMoreOccurrences=m.no_more_occurrences,
                                   BotNames=m.bot_names,
                                   RequiredBots=m.required_bots,
                                   IceBreaker=m.ice_breaker,
                                   Introduction=m.introduction,
                                   ExpirationDateTimeInUtc=next_exp)
                scheduler.update_queue_status(m.uuid_key, m.person_name, True)
                scheduler.update_last_location(m.uuid_key, m.person_name, idx)
            else:
                click.echo("Publishing message(simulated):")
                click.echo(str(m))

    except Exception:
        print 'here'
        click.echo(traceback.print_exc())
        raise
 def test_empty_uuid_throws_value_error(self):
     with self.assertRaises(Exception) as context:
         message_manager_under_test = MessageManager(DeviceName="TEST")
         message_manager_under_test.publish_message(
             Body="Test")
     self.assertTrue('No uuid provided' in context.exception)
 def test_empty_body_throws_value_error(self):
     with self.assertRaises(Exception) as context:
         message_manager_under_test = MessageManager(DeviceName="TEST")
         message_manager_under_test.publish_message(UUID='test')
     self.assertTrue('No message body provided' in context.exception)