def _set_once_reminder(r, orig_message): ''' Set Once Reminder Record a reminder in the database to be sent. -- @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'])) RemindOnce.create(orig_message=json.dumps(orig_message), time=r['parsed_time'], message=r['message']) return
def _set_once_reminder (r, orig_message): ''' Set Once Reminder Record a reminder in the database to be sent. -- @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'] )) RemindOnce.create( orig_message = json.dumps(orig_message), time = r['parsed_time'], message = r['message'] ) return
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 run_remind_once (): ''' Run Remind Once Find and send all of the once time reminders that are due -- @return void ''' logger.debug('Running Remind Once Job') try: for reminder in RemindOnce.select().where(RemindOnce.sent == 0, RemindOnce.time <= datetime.now()): logger.debug('Sending one time 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 ) # Mark it as complete reminder.sent = 1 reminder.save() except Exception: print traceback.format_exc() return
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...'