Example #1
0
def run():
    try:
        while True:
            # checks if there are any new messages
            if mess_util.list_after(
                    Main.NEWEST_MESSAGE_ANALYZED_ID) is not None:
                current_message = mess_util.list()[0]  # newest message
                text = current_message.text.lower()
                name = current_message.name
                at_bot = '@' + Main.BOT.name
                # checks if the bot is mentioned
                if at_bot.lower() in text:
                    # print(text)
                    Analyze.analyze_message(text, name)  # analyzes the message
                Main.NEWEST_MESSAGE_ANALYZED_ID = current_message.id  # sets the new analyzed id
            # if it is time to check for the day's events (8:30 by default) or
            # if the bot just started
            if not Main.checked_events and datetime.datetime.now(
            ) > CHECK_TIME and datetime.datetime.now() < CHECK_TIME_END:
                Functions.check_date()  # checks if any events have passed
    # enters terminal command mode
    except KeyboardInterrupt:
        Log.log_debug(str(datetime.datetime.now()) + " >> KeyboardInterrupt")
        command = str(input("Would you like to do?\n")).lower()
        # reads the chats that have not been read yet
        if command == 'read':
            Log.log_debug(str(datetime.datetime.now()) + " >> Messages Read")
            if mess_util.list_after(Main.NEWEST_MESSAGE_READ_ID) is not None:
                for m in mess_util.list_after(Main.NEWEST_MESSAGE_READ_ID):
                    print(m.name + ': ' + m.text)
                Main.NEWEST_MESSAGE_READ_ID = mess_util.list()[0].id
        # post into the group as the bot
        elif command == 'post':
            message = str(
                input(
                    'What would you like to say? (type "cancel" to cancel message)\n'
                ))
            if message.lower() == 'cancel':
                run()
            Log.log_debug(
                str(datetime.datetime.now()) + " >> Manual Posting: " +
                message)
            Functions.post_message(message)
        # get help information
        elif command == 'create event':
            date = str(input('Enter the date of the event\n')).split('/')
            name = str(input('Enter the name of the event\n'))
            desc = str(input('Enter the description of the event\n'))
            if len(date[2]) != 4:
                date[2] = '20' + date[2]
            response = Functions.create_event(name, int(date[2]), int(date[1]),
                                              int(date[0]), desc)
            print(response)
        elif command == 'list events':
            response = Functions.list_events()
            print(response)
        elif command == 'delete event':
            name = str(input('Enter the name of the event\n'))
            Functions.delete_event(name)
            print("Event Deleted")
        elif command == 'create reminder':
            day = str(input('Enter the day for the reminder\n'))
            id = str(input('Enter the name of the reminder\n'))
            desc = str(input('Enter the description of the event\n'))
            response = Functions.create_reminder(day, id, desc)
            print(response)
        elif command == 'list reminders':
            response = Functions.list_reminders()
            print(response)
        elif command == 'delete reminder':
            name = str(input('Enter the name of the reminder\n'))
            Functions.delete_reminder(name)
        elif command == 'help':
            Log.log_debug(str(datetime.datetime.now()) + " >> Help")
            print("Possible Commands:")
            for command in console_commands:
                print("\t" + command)
        elif command == 'info':
            Log.log_debug(str(datetime.datetime.now()) + " >> Info")
            command = str(input("What command would you like info about?\n"))
            print(Info.get_info(command))
        # shutdown the system
        elif command == 'shutdown':
            Log.log_debug(str(datetime.datetime.now()) + " >> System Shutdown")
            exit()
        # cancel command mode
        elif command == 'cancel':
            pass
        Log.log_debug(
            str(datetime.datetime.now()) + " >> Continuing operation")
        run()