Ejemplo n.º 1
0
def alarmTakeAway(context):
    job = context.job
    userID = job.context
    # encode leaving to specific user ID
    exitID = "EXITCONFIRM_" + str(userID)

    TAKEAWAY_MESSAGE = "<b>Hi, you have been in the Dining Hall for 7 minutes to take away food. Kindly leave now, thank you for your cooperation!</b> " + RUN + "\n"

    button_list = [
        InlineKeyboardButton(text='Leave Dining Hall', callback_data=exitID)
    ]
    menu = build_menu(button_list,
                      n_cols=1,
                      header_buttons=None,
                      footer_buttons=None)

    userIn = db.checkUser(str(userID))
    if userIn:
        logger.info(
            "Reminder text for takeaway has been sent to the user {}".format(
                str(userID)))
        context.bot.send_message(userID,
                                 text=TAKEAWAY_MESSAGE,
                                 reply_markup=InlineKeyboardMarkup(menu),
                                 parse_mode=ParseMode.HTML)
    else:  # if user has left early
        logger.info(
            "User {} has already long left the DH! Nevertheless, this job has still be executed and no reminder message is sent to the user."
            .format(userID))
    return
def send_final(update, context):
    query = update.callback_query
    user = query.from_user
    chatid = query.message.chat_id

    log_text = "User " + str(user.id) + " has now confirmed entry to the DH."
    logger.info(log_text)

    reply_text = "<b>Okay, thank you for indicating on this bot! Do remind your friends to do the same as well!</b>\n\n" \
                + "I have also set up timers to remind you when the time limit is up!\n\n" + EAT + " Enjoy your meal! " + EAT \
                + "\n\nPlease press the button below <b>only if you are currently leaving</b> the dining hall:"

    # encode leaving to specific user ID
    exitID = "LEAVE_" + str(user.id)
    button_list = [
        InlineKeyboardButton(text='Leave Dining Hall', callback_data=exitID)
    ]
    menu = build_menu(button_list,
                      n_cols=1,
                      header_buttons=None,
                      footer_buttons=None)

    context.bot.editMessageText(text=reply_text,
                                chat_id=chatid,
                                message_id=query.message.message_id,
                                reply_markup=InlineKeyboardMarkup(menu),
                                parse_mode=ParseMode.HTML
                                )  # no buttons for final text sent to the user

    indicatedIntention = context.chat_data['Intention']
    logger.info("Pulled intention is " + indicatedIntention)
    if (indicatedIntention == "TAKEAWAY"):
        # Add user to DB for takeaway
        res = db.addTakeAwayUser(str(user.id))
        if res:
            notify_admin(TAKEAWAY_OVERFLOW_MESSAGE, context)
        new_job = context.job_queue.run_once(
            alarmTakeAway, 420, context=user.id
        )  # changed context to userID so as to be not usable in groups; 420 for 7 mins
        #INFOSTORE[str(user.id)] = new_job
        logger.info("Takeaway timer has started for {}".format(str(user.id)))
    elif (indicatedIntention == "DINE-IN"):
        # Add user to DB for dine-in
        res = db.addDineInUser(str(user.id))
        if res:
            notify_admin(DINE_IN_OVERFLOW_MESSAGE, context)
        new_job1 = context.job_queue.run_once(
            alarmEatIn25, 1500, context=user.id)  # 1500s = 25 mins
        new_job2 = context.job_queue.run_once(
            alarmEatIn20, 1200, context=user.id)  # 1200s = 20 mins
        #INFOSTORE[str(user.id)] = new_job
        logger.info("Two dining in timers have started for {}".format(
            str(user.id)))
    else:
        logger.warning("Something went wrong with the intention...")
    return
def indicate_intention(update, context):
    query = update.callback_query
    user = query.from_user
    chatid = query.message.chat_id

    # get status of user from POSTGRESQL + if user is already indicated, cannot press start again
    userIn = db.checkUser(str(user.id))
    if userIn:
        warnText = "<b>You have already indicated earlier.</b> You can't enter the DH twice!\n\nTo check the status of the DH currently, press /status."
        context.bot.editMessageText(text=warnText,
                                    chat_id=user.id,
                                    parse_mode=ParseMode.HTML)
        return ConversationHandler.END # end convo if user pressed start but is in DH

    else:
        # get user intention from button pressed
        pressed = str(query.data)
        if pressed == 'INTENT_0':
            intention = "TAKEAWAY"
        if pressed == 'INTENT_1':
            intention = "DINE-IN"

        # Using chat_data to store information from the same chat ID
        context.chat_data['Intention'] = intention

        log_text = "User " + str(user.id) + " has indicated to {}.".format(intention)
        logger.info(log_text)

        reply_text = "Yumz, time for some good food!\n\n<b>You wish to {} in the Dining Hall now, can I confirm?</b>\n".format(intention)

        reply_text += DINE_IN_SCHEDULE_MESSAGE 

        reply_text += "\n"

        schedule_index = (datetime.datetime.now().date().isocalendar()[1] - 32) % 3

        if (datetime.datetime.now().hour <= 12):
            reply_text += DINE_IN_SCHEDULE_BREAKFAST[schedule_index]
        else:
            reply_text += DINE_IN_SCHEDULE_DINNER[schedule_index]

        reply_text += "\nOr if now is not the time allocated to your zone or you have accidentally pressed, you can press <i>Back</i> to go back to the previous page!"

        button_list = [InlineKeyboardButton(text='Yes, I confirm.', callback_data='CONFIRM_ENTRY'),
                    InlineKeyboardButton(text='Back', callback_data='CANCEL')]
        menu = build_menu(button_list, n_cols=1, header_buttons=None, footer_buttons=None)

        context.bot.editMessageText(text=reply_text,
                                    chat_id=chatid,
                                    message_id=query.message.message_id,
                                    reply_markup=InlineKeyboardMarkup(menu),
                                    parse_mode=ParseMode.HTML)
        return CONFIRM_ENTRY
def leaveEarly(update, context):
    query = update.callback_query
    user = query.from_user
    chatid = query.message.chat_id
    reply_text = "<b>Are you sure you are leaving the Dining Hall right now?</b>\n"

    # encode leaving to specific user ID
    exitID = "EXITCONFIRM_" + str(user.id)

    button_list = [InlineKeyboardButton(text='Yes, Leave Dining Hall', callback_data = exitID)]
    menu = build_menu(button_list, n_cols=1, header_buttons=None, footer_buttons=None)

    context.bot.editMessageText(text=reply_text,
                            chat_id=chatid,
                            message_id=query.message.message_id,
                            reply_markup=InlineKeyboardMarkup(menu),
                            parse_mode=ParseMode.HTML)
    return
def send_help(update, context):
    query = update.callback_query
    user = query.from_user
    chatid = query.message.chat_id

    log_text = "User " + str(user.id) + " is now seeking help."
    logger.info(log_text)

    reply_text = HELP_TEXT

    button_list = [InlineKeyboardButton(text='Back', callback_data='BACKTOSTART')]
    menu = build_menu(button_list, n_cols=1, header_buttons=None, footer_buttons=None)

    context.bot.editMessageText(text=reply_text,
                                chat_id=chatid,
                                message_id=query.message.message_id,
                                reply_markup=InlineKeyboardMarkup(menu),
                                parse_mode=ParseMode.HTML)
    return AFTER_HELP
Ejemplo n.º 6
0
def start(update, context):
    reply_text = "Hello! You are currently being served by the CAPT Bot. " + ROBOT + "\nCredits to: RC4SPACE" + "\n\n"

    # Get current status from DB
    DINE_IN_COUNT, TAKEAWAY_COUNT = db.getCount()
    TOTAL_COUNT = int(DINE_IN_COUNT) + int(TAKEAWAY_COUNT)

    timeNow = datetime.datetime.now()
    STATUS_TEXT = "<b>Current Status of DH:</b>\n"
    # check if overload > 50 people in DH
    if TOTAL_COUNT >= 50:
        STATUS_TEXT += FIRE + " <b>Crowd level is currently HIGH, please wait before coming to the dining hall.</b>\n\n"
    STATUS_TEXT += "Total number of people in Dining Hall: <b>{}</b>".format(
        str(TOTAL_COUNT))
    STATUS_TEXT += "\n" + EAT + " Dining In: <b>{}</b>".format(
        str(DINE_IN_COUNT))
    STATUS_TEXT += "\n" + BURGER + " Taking Away: <b>{}</b>".format(
        str(TAKEAWAY_COUNT))
    STATUS_TEXT += "\n<i>Accurate as of: {}</i>".format(
        timeNow.strftime("%d/%m/%Y %H:%M:%S"))

    reply_text += STATUS_TEXT
    reply_text += "\n\n**************************************\n"
    reply_text += "\nHey there! Thanks for using the bot! Do you wish to dine-in or takeaway?\n\n" \
                    + BUTTON + "Press <i>Dine-In</i> to eat inside the dining hall (limit of 35 mins)\n\n" \
                    + BUTTON + "Press <i>Takeaway</i> to takeaway food with your own container (limit of 7 mins)\n\n" \
                    + BUTTON + "Press <i>Refresh</i> to get the latest crowd level!\n\n" \
                    + BUTTON + "Press <i>Help</i> if you need further assistance or to find more information :)" \

    takeawayText = BURGER + " Takeaway"
    dineInText = EAT + " Dine-In"
    helpText = INFO + " Help"
    refreshText = LIGHTNING + " Refresh"
    button_list = [
        InlineKeyboardButton(text=takeawayText, callback_data='INTENT_0'),
        InlineKeyboardButton(text=dineInText, callback_data='INTENT_1'),
        InlineKeyboardButton(text=helpText, callback_data='HELP'),
        InlineKeyboardButton(text=refreshText, callback_data='REFRESH')
    ]
    menu = build_menu(button_list,
                      n_cols=2,
                      header_buttons=None,
                      footer_buttons=None)

    # create a jobqueue
    jobq = context.job_queue

    # split into 2 modes of entry for this state - can be command or callbackquery data
    try:  # for command entry
        user = update.message.from_user
        chatid = update.message.chat_id

        # get status of user from POSTGRESQL + if user is already indicated, cannot press /start again
        userIn = db.checkUser(str(user.id))
        if userIn:
            warnText = "<b>You have already indicated earlier.</b> You can't enter the DH twice!\n\nTo leave the dining hall, press the leave button in any previous message (or reminder message) I have sent you!\n\n"
            warnText += STATUS_TEXT
            context.bot.send_message(text=warnText,
                                     chat_id=user.id,
                                     parse_mode=ParseMode.HTML)
            return ConversationHandler.END  # end convo if user pressed start but is in DH

        else:
            # if new start, send a new message
            context.bot.send_message(text=reply_text,
                                     chat_id=chatid,
                                     parse_mode=ParseMode.HTML,
                                     reply_markup=InlineKeyboardMarkup(menu))

            # job queue for reminders for temp takings; if job has been created, delete it first, then create new one again (following telegram API)
            if 'morningReminder' in context.chat_data:
                old_job = context.chat_data['morningReminder']
                old_job.schedule_removal()
            if 'eveningReminder' in context.chat_data:
                old_job = context.chat_data['eveningReminder']
                old_job.schedule_removal()

            morningReminder = jobq.run_daily(callback_reminder,
                                             datetime.time(8, 00, 00),
                                             context=chatid)  # reminder at 8am
            context.chat_data['morningReminder'] = morningReminder
            eveningReminder = jobq.run_daily(
                callback_reminder, datetime.time(17, 30, 00),
                context=chatid)  # reminder at 530pm
            context.chat_data['eveningReminder'] = eveningReminder

    except AttributeError:  # for backs and refreshes
        query = update.callback_query
        user = query.from_user
        chatid = query.message.chat_id
        # if existing user, edit message
        context.bot.editMessageText(
            text=reply_text,  # same reply text
            chat_id=chatid,
            message_id=query.message.
            message_id,  # to edit the prev message sent by bot
            reply_markup=InlineKeyboardMarkup(menu),
            parse_mode=ParseMode.HTML)

    log_text = "User " + str(user.id) + " has started using bot."
    logger.info(log_text)

    return AFTER_START