Example #1
0
    def check_schedules():
        print (schedulecheck.updater)
        db = MongoDB('heroku_mqncqpgt', 'reminders')

        # Get all reminders today.
        current_date = datetime.now().date().strftime('%d%m%Y')
        query = { "date" : current_date }

        # Get current time
        current_time = datetime.now().strftime("%H%M")

        # Loop through all reminders today to check for the time
        for element in db.finddb(query):

                # separate the date str
                # day = element["date"][0:2]
                # month = element["date"][2:4]
                # year = element["date"][4:8]

                # separate the time str
                hour = int(element["time"][0:2])
                minute = int(element["time"][2:4])

                # Convert hour and minute into datetime
                remindertime = datetime.now().replace(hour=hour, minute=minute).strftime("%H%M")

                # If remindertime is less than current timing, send out timing and delete the reminder from DB
                if remindertime <= current_time:
                    chatid = element["chatid"]

                    messagestr = "Reminder: {0}".format(element["description"])

                    schedulecheck.updater.bot.send_message(chatid, messagestr)

                    # Convert object id str into ObjectID
                    objectid = ObjectId(str(element["_id"]))

                    # Delete from db
                    query = { "_id" : objectid }
                    db.deleteonedb(query)
Example #2
0
class ReviewReminders:
    def __init__(self):
        # Init database
        self.db = MongoDB('heroku_mqncqpgt', 'reminders')

    def showallreminders(self, update, context):
        # get the chat id to retrieve all reminders for that chat
        chatid = update.message.chat.id

        query = {"chatid": chatid}

        for element in self.db.finddb(query):

            # separate the date str
            day = element["date"][0:2]
            month = element["date"][2:4]
            year = element["date"][4:8]

            # separate the time str
            hour = element["time"][0:2]
            minute = element["time"][2:4]

            #id = str(element["_id"])

            messagestr = "Description: {0}\nDate(Day/Month/Year): {1}/{2}/{3}\nTime(hh:mm): {4}:{5}\noid: {6}".format(
                element["description"], day, month, year, hour, minute,
                str(element["_id"]))

            # Add inlinebutton
            keyboard = [[
                InlineKeyboardButton("Delete from database",
                                     callback_data='delete')
            ]]

            context.bot.send_message(
                update.effective_chat.id,
                messagestr,
                reply_markup=InlineKeyboardMarkup(keyboard))

    def get_review_handler(self):
        return CommandHandler("allreminders", self.showallreminders)

    def reminder_button(self, update, context):
        # if is delete button
        if update.callback_query.data == "delete":
            callback_message = update.callback_query.message

            # Retrieve the objectid str from the message
            objectidstr = callback_message.text.split("\n")[3].split(
                ":")[1].strip()

            # Convert object id str into ObjectID
            objectid = ObjectId(objectidstr)

            # Delete from db
            query = {"_id": objectid}
            self.db.deleteonedb(query)

            # Delete the message
            context.bot.delete_message(
                chat_id=update.effective_chat.id,
                message_id=update.callback_query.message.message_id)

    def get_callbackquery_handler(self):
        return CallbackQueryHandler(self.reminder_button)