def _set_recurring_reminder(r, orig_message): ''' Set Recurring Reminder Record a recurring reminder in the database to be sent. #TODO: Implement this -- @param r:dict The parsed message from the user @param orig_message:str The original message received @return str ''' logger.debug( 'Storing reminder for {id}'.format(id=orig_message['chat']['id'])) RemindRecurring.create(orig_message=json.dumps(orig_message), rrules=r['parsed_time'], next_run=rrulestr( r['parsed_time'], dtstart=datetime.datetime.now()).after( datetime.datetime.now()), message=r['message']) return
def _set_recurring_reminder (r, orig_message): ''' Set Recurring Reminder Record a recurring reminder in the database to be sent. #TODO: Implement this -- @param r:dict The parsed message from the user @param orig_message:str The original message received @return str ''' logger.debug('Storing reminder for {id}'.format( id = orig_message['chat']['id'] )) RemindRecurring.create( orig_message = json.dumps(orig_message), rrules = r['parsed_time'], next_run = rrulestr(r['parsed_time'], dtstart = datetime.datetime.now()).after(datetime.datetime.now()), message = r['message'] ) return
def run_remind_recurring (): ''' Run Remind Recurring Find and send all of the recurring reminders that are due -- @return void ''' logger.debug('Running Remind Recurring Job') try: # Get reminders have have not been marked as completed, as well as # have their next_run date ready or not set for reminder in RemindRecurring.select().where(RemindRecurring.sent == 0, ((RemindRecurring.next_run <= datetime.now()) | ( RemindRecurring.next_run >> None))): # If we know the next_run date, send the message. If # we dont know the next_run, this will be skipped # and only the next_run determined if reminder.next_run is not None: logger.debug('Sending recurring reminder message with id {id}'.format( id = reminder.id )) # Send the actual reminder Telegram.send_message( _get_sender_information(reminder.orig_message), 'text', reminder.message) # Lets parse the rrules and update the next_run time for # a message. We will use python-dateutil to help with # determinig the next run based on the parsed RRULE # relative from now. next_run = rrulestr(reminder.rrules, dtstart = datetime.now()).after(datetime.now()) # If there is no next run, consider the # schedule complete and mark it as # sent if not next_run: reminder.sent = 1 reminder.save() continue # Save the next run reminder.next_run = next_run reminder.save() except Exception, e: print traceback.format_exc()
def _show_all_reminders(message): ''' Show All Reminders Returns all of the reminders for the appropriate chat ID that the message came from -- @param message:dict The message sent by the user @return str ''' response = '\n# One time reminders:\n\n' for reminder in RemindOnce.select().where( RemindOnce.sent == 0, RemindOnce.time >= datetime.datetime.now()): orig_message = json.loads(reminder.orig_message) if orig_message['chat']['id'] == message['chat']['id']: response += '(#{id}) {human} @{time} | {message}\n'.format( id = reminder.id, human = arrow.get(reminder.time, config.get('reminder', 'timezone', 'UTC')).humanize(), time = str(reminder.time), message = reminder.message[:20] + '...' \ if len(reminder.message) > 20 else reminder.message) response += '\n# Recurring reminders:\n\n' for reminder in RemindRecurring.select().where( RemindRecurring.sent == 0, RemindRecurring.next_run >= datetime.datetime.now()): orig_message = json.loads(reminder.orig_message) if orig_message['chat']['id'] == message['chat']['id']: response += '(#{id}) {human} @{next_run} | {message}\n'.format( id = reminder.id, human = arrow.get(reminder.next_run, config.get('reminder', 'timezone', 'UTC')).humanize(), next_run = str(reminder.next_run), message = reminder.message[:20] + '...' \ if len(reminder.message) > 20 else reminder.message) return response
def _show_all_reminders (message): ''' Show All Reminders Returns all of the reminders for the appropriate chat ID that the message came from -- @param message:dict The message sent by the user @return str ''' response = '\n# One time reminders:\n\n' for reminder in RemindOnce.select().where(RemindOnce.sent == 0, RemindOnce.time >= datetime.datetime.now()): orig_message = json.loads(reminder.orig_message) if orig_message['chat']['id'] == message['chat']['id']: response += '(#{id}) {human} @{time} | {message}\n'.format( id = reminder.id, human = arrow.get(reminder.time, config.get('reminder', 'timezone', 'UTC')).humanize(), time = str(reminder.time), message = reminder.message[:20] + '...' \ if len(reminder.message) > 20 else reminder.message) response += '\n# Recurring reminders:\n\n' for reminder in RemindRecurring.select().where(RemindRecurring.sent == 0, RemindRecurring.next_run >= datetime.datetime.now()): orig_message = json.loads(reminder.orig_message) if orig_message['chat']['id'] == message['chat']['id']: response += '(#{id}) {human} @{next_run} | {message}\n'.format( id = reminder.id, human = arrow.get(reminder.next_run, config.get('reminder', 'timezone', 'UTC')).humanize(), next_run = str(reminder.next_run), message = reminder.message[:20] + '...' \ if len(reminder.message) > 20 else reminder.message) return response
def _stop_reminder(message): ''' Stop A Reminder Sets a reminder as sent so that it will not run again. This function also ensures that the reminder's chatID matches the chatID of the user requesting the stop -- @param message:dict The message sent by the user @return str ''' # Remove the command form the text text = message['text'].replace('remind stop', '').strip() # Split the remainder of the text into 2 parts. # We are expecting either 'once' or 'recurring' # as the type of reminder and a number parts_list = text.split(' ') # Check the length of the list and assume a typo # if its not 2 if len(parts_list) != 2: return 'Could not understand what you wanted. Expecting:\n' + \ 'remind stop [once/recurring] [message number]' message_type = parts_list[0] message_number = parts_list[1] # Check that we got once or recurring if message_type not in ['once', 'recurring']: return 'Could not figure out which message type you are referring to. ' + \ 'Expected \'once\' / \'recurring\'' # For once time messages, perform the chatID check # and mark the message as sent if its ok if message_type == 'once': try: m = RemindOnce.select().where( RemindOnce.id == message_number).get() if json.loads( m.orig_message)['chat']['id'] == message['chat']['id']: m.sent = 1 m.save() else: logger.warning( 'User {id} tried to disable a reminder they dont own'. format(id=message['chat']['id'])) return 'That message number does not exist or you dont own it.' except RemindOnce.DoesNotExist: return 'That message number does not exist or you dont own it.' return 'Done stopping the one time reminder' # The same here for the recurring message. if message_type == 'recurring': try: m = RemindRecurring.select().where( RemindRecurring.id == message_number).get() if json.loads( m.orig_message)['chat']['id'] == message['chat']['id']: m.sent = 1 m.save() else: logger.warning( 'User {id} tried to disable a reminder they dont own'. format(id=message['chat']['id'])) return 'That message number does not exist or you dont own it.' except RemindRecurring.DoesNotExist: return 'That message number does not exist or you dont own it.' return 'Done stopping the recurring reminder' # We will most probably never get here, but # just in case... return 'Nothing happend...'
def _stop_reminder (message): ''' Stop A Reminder Sets a reminder as sent so that it will not run again. This function also ensures that the reminder's chatID matches the chatID of the user requesting the stop -- @param message:dict The message sent by the user @return str ''' # Remove the command form the text text = message['text'].replace('remind stop', '').strip() # Split the remainder of the text into 2 parts. # We are expecting either 'once' or 'recurring' # as the type of reminder and a number parts_list = text.split(' ') # Check the length of the list and assume a typo # if its not 2 if len(parts_list) != 2: return 'Could not understand what you wanted. Expecting:\n' + \ 'remind stop [once/recurring] [message number]' message_type = parts_list[0] message_number = parts_list[1] # Check that we got once or recurring if message_type not in ['once', 'recurring']: return 'Could not figure out which message type you are referring to. ' + \ 'Expected \'once\' / \'recurring\'' # For once time messages, perform the chatID check # and mark the message as sent if its ok if message_type == 'once': try: m = RemindOnce.select().where(RemindOnce.id == message_number).get() if json.loads(m.orig_message)['chat']['id'] == message['chat']['id']: m.sent = 1 m.save() else: logger.warning('User {id} tried to disable a reminder they dont own'.format( id = message['chat']['id'])) return 'That message number does not exist or you dont own it.' except RemindOnce.DoesNotExist: return 'That message number does not exist or you dont own it.' return 'Done stopping the one time reminder' # The same here for the recurring message. if message_type == 'recurring': try: m = RemindRecurring.select().where(RemindRecurring.id == message_number).get() if json.loads(m.orig_message)['chat']['id'] == message['chat']['id']: m.sent = 1 m.save() else: logger.warning('User {id} tried to disable a reminder they dont own'.format( id = message['chat']['id'])) return 'That message number does not exist or you dont own it.' except RemindRecurring.DoesNotExist: return 'That message number does not exist or you dont own it.' return 'Done stopping the recurring reminder' # We will most probably never get here, but # just in case... return 'Nothing happend...'