Example #1
0
 def cancel_allocation(self, update, context):
     query = DatabaseQuery(self.default_replies)
     chat_id = update.effective_chat.id
     user = update.message.from_user
     if len(context.args) > 0:
         entered_date = context.args[0]
         old_alloc_date = query.getAllocation(user.id)
         if utils.convertdate_for_display(
                 old_alloc_date
         ) != "None" and entered_date == old_alloc_date:
             date = utils.convertdate_for_display(old_alloc_date)
             query.cancelAllocation(user.id)
             context.bot.send_message(
                 chat_id=chat_id,
                 text=query.getReply(
                     cname.i_alloc_cancel_pass).format(date=date))
         else:
             date = utils.convertdate_for_display(entered_date)
             if date != "None":
                 context.bot.send_message(
                     chat_id=chat_id,
                     text=query.getReply(
                         cname.i_alloc_cancel_fail2).format(date=date))
             else:
                 context.bot.send_message(chat_id=chat_id,
                                          text=query.getReply(
                                              cname.i_alloc_cancel_fail1))
     else:
         context.bot.send_message(chat_id=chat_id,
                                  text=query.getReply(
                                      cname.i_alloc_cancel_fail1))
Example #2
0
 def change_allocation(self, update, context):
     query = DatabaseQuery(self.default_replies)
     chat_id = update.effective_chat.id
     user = update.message.from_user
     old_alloc_date = query.getAllocation(user.id)
     if len(context.args) > 0:
         if old_alloc_date == alloc_status.pending.name:
             context.bot.send_message(chat_id=chat_id,
                                      text=query.getReply(
                                          cname.i_alloc_chg_fail))
         elif old_alloc_date == 'error':
             context.bot.send_message(
                 chat_id=chat_id,
                 text=
                 "DB error. Allocated date in DB not in dd/mm/yy format.")
         else:
             new_alloc_date = context.args[0]
             success = query.setAllocation(user.id, new_alloc_date)
             if success:
                 new_date = utils.convertdate_for_display(new_alloc_date)
                 old_date = utils.convertdate_for_display(old_alloc_date)
                 context.bot.send_message(
                     chat_id=chat_id,
                     text=query.getReply(cname.i_alloc_chg_pass).format(
                         new_date=new_date, old_date=old_date))
             else:
                 context.bot.send_message(
                     chat_id=chat_id, text="Failed to change allocation")
     else:
         context.bot.send_message(
             chat_id=chat_id,
             text=
             "Please enter a date (in dd/mm/yy format) after /change_alloc."
         )
 def cancelAllocation(self, tgUserid: str):
     temp_list = self.parents_profile.index[self.parents_profile[
         cname.tg_id] == int(tgUserid)].tolist()
     if len(temp_list) > 0:
         parent_profile_rl = temp_list[0]
         curr_alloc_date = self.getAllocation(tgUserid=tgUserid)
         if utils.convertdate_for_display(curr_alloc_date) != 'None':
             curr_alloc_date_rl = self.op_dates.index[self.op_dates[
                 cname.op_date] == curr_alloc_date].tolist()[0]
             self.parents_profile.at[
                 parent_profile_rl, cname.
                 op_date] = utils.blank_date_string  # remove allocated date
             self.op_dates.at[
                 curr_alloc_date_rl,
                 cname.vacancy_alloc] = self.op_dates.at[
                     curr_alloc_date_rl,
                     cname.vacancy_alloc] - 1  # reduce vacancy count
             self.parents_profile.at[parent_profile_rl,
                                     cname.alloc_curr] = datetime.strftime(
                                         datetime.now(),
                                         utils.dateformat_currency
                                     )  # update allocation currency
         success = True
     else:
         success = False
     if success: self.saveExcelFile(cname.excel_filepath)
     return success
Example #4
0
 def get_event_time(self, update, context):
     query = DatabaseQuery(self.default_replies)
     chat_id = update.effective_chat.id
     if len(context.args) > 0:
         date = context.args[0]
         date_converted = utils.convertdate_for_display(date)
         if date_converted != "None":
             startend = query.getEventStartEnd(date)
             if len(startend) > 0:
                 start = startend[0]
                 end = startend[1]
                 context.bot.send_message(
                     chat_id=chat_id,
                     text=query.getReply(
                         cname.i_duration_user_entered).format(
                             date=date_converted, start=start, end=end))
                 context.bot.send_message(chat_id=chat_id,
                                          text=query.getReply(
                                              cname.i_contConverse))
             else:
                 context.bot.send_message(
                     chat_id=chat_id,
                     text=query.getReply(
                         cname.i_duration_invalid_date).format(
                             date=date_converted))
         else:
             context.bot.send_message(
                 chat_id=chat_id,
                 text="Please enter a date in dd/mm/yy format")
     else:
         context.bot.send_message(
             chat_id=chat_id, text="Please enter a date in dd/mm/yy format")
Example #5
0
 def change_avail(self, update, context):
     query = DatabaseQuery(self.default_replies)
     chat_id = update.effective_chat.id
     user = update.message.from_user
     if len(context.args) > 1:
         action = "set" if context.args[0] == "set" else "cancel"
         date = context.args[1]
         date_converted = utils.convertdate_for_display(
             date)  # ensure date is in dd/mm/yy format
         if date_converted != "None":
             success = query.setAvailability(user.id,
                                             date,
                                             set=(action == "set"))
             if success:
                 context.bot.send_message(
                     chat_id=chat_id,
                     text=query.getReply(cname.i_avail_set_pass).format(
                         action=action, date=date_converted))
             else:
                 context.bot.send_message(
                     chat_id=chat_id,
                     text=query.getReply(
                         cname.i_avail_set_fail).format(action=action))
         else:
             context.bot.send_message(
                 chat_id=chat_id,
                 text=
                 "Please enter a date (in dd/mm/yy format) after /change_avail <action>."
             )
     else:
         context.bot.send_message(
             chat_id=chat_id,
             text=
             "Please enter an action (set/cancel) and a date (in dd/mm/yy format) after /set_avail."
         )
 def getAllocation(self, tgUserid: str) -> str:
     dr = self.parents_profile.loc[self.parents_profile[cname.tg_id] == int(
         tgUserid)]
     allocatedDate = dr[cname.op_date].values[0] if not dr.empty else "None"
     if str(allocatedDate) == 'nan':
         allocatedDate = alloc_status.pending.name
     elif allocatedDate.lower() == alloc_status.unabletoretrieve.name:
         allocatedDate = alloc_status.availabilitynotgiven.name
     elif allocatedDate.lower() == alloc_status.nodatesavailable.name:
         allocatedDate = alloc_status.nodatesavailable.name
     elif utils.convertdate_for_display(allocatedDate) == "None":
         allocatedDate = "error"
     return allocatedDate
    def setAllocation(self, tgUserid: str, new_alloc_date: str) -> bool:
        temp_list1 = self.parents_profile.index[self.parents_profile[
            cname.tg_id] == int(tgUserid)].tolist()
        temp_list2 = self.op_dates.index[self.op_dates[cname.op_date] ==
                                         new_alloc_date].tolist()
        if len(temp_list1) > 0 and len(temp_list2) > 0:
            # get row labels of parent and new_alloc_date
            parent_profile_rl = temp_list1[0]
            new_alloc_date_rl = temp_list2[0]

            # check if parent has an allocated date currently
            curr_alloc_date = self.getAllocation(tgUserid=tgUserid)
            if utils.convertdate_for_display(curr_alloc_date) != 'None':
                curr_alloc_date_rl = self.op_dates.index[self.op_dates[
                    cname.op_date] == curr_alloc_date].tolist()[0]
            else:
                curr_alloc_date_rl = -1

            # update allocated date
            if curr_alloc_date != new_alloc_date:
                self.parents_profile.at[
                    parent_profile_rl,
                    cname.op_date] = new_alloc_date  # update allocated date
                self.op_dates.at[
                    new_alloc_date_rl, cname.vacancy_alloc] = self.op_dates.at[
                        new_alloc_date_rl,
                        cname.vacancy_alloc] + 1  # increase vacancy count
                if curr_alloc_date_rl != -1:
                    self.op_dates.at[
                        curr_alloc_date_rl,
                        cname.vacancy_alloc] = self.op_dates.at[
                            curr_alloc_date_rl,
                            cname.vacancy_alloc] - 1  # reduce vacancy count
                self.parents_profile.at[
                    parent_profile_rl, cname.alloc_curr] = datetime.strftime(
                        datetime.now(),
                        '%d/%m/%y %H:%M')  # update allocation currency
            success = True
        else:
            success = False
        if success: self.saveExcelFile(cname.excel_filepath)
        return success
Example #8
0
    def openended_reply(self, update, context):
        print("message: ", update.message.text)
        user = update.message.from_user
        chat_id = update.effective_chat.id
        query = DatabaseQuery(self.default_replies)
        response = get_intent(session_id=update.effective_chat.id,
                              text=update.message.text)
        intent_name = response.query_result.intent.display_name
        print(f"intent: {intent_name}")

        if not query.checkIfRegistered(user.id):
            context.bot.send_message(
                chat_id=chat_id,
                text=query.getReply(
                    cname.i_reg_user).format(firstname=user.first_name))
            return

        if intent_name == "getAllocation" or intent_name == 'changeAllocation':
            date = query.getAllocation(user.id)
            if date == alloc_status.pending.name:
                context.bot.send_message(chat_id=chat_id,
                                         text=query.getReply(
                                             cname.i_alloc_get_fail_pend),
                                         reply_markup=ReplyKeyboardRemove())
                context.bot.send_message(chat_id=chat_id,
                                         text=query.getReply(
                                             cname.i_contConverse))
            elif date == alloc_status.nodatesavailable.name or date == alloc_status.availabilitynotgiven.name:
                kb = self.create_dates_kb('/change_alloc', query=query)
                if len(kb) > 0:
                    reply = cname.i_alloc_get_fail1 if date == alloc_status.nodatesavailable.name else cname.i_alloc_get_fail3
                    context.bot.send_message(
                        chat_id=chat_id,
                        text=query.getReply(reply),
                        reply_markup=ReplyKeyboardMarkup(kb))
                else:
                    context.bot.send_message(
                        chat_id=chat_id,
                        text=query.getReply(cname.i_alloc_get_fail2),
                        reply_markup=ReplyKeyboardRemove())
            elif utils.convertdate_for_display(date) != "None":
                date = utils.convertdate_for_display(date)
                if intent_name == 'getAllocation':
                    context.bot.send_message(
                        chat_id=chat_id,
                        text=query.getReply(
                            cname.i_alloc_get).format(date=date),
                        reply_markup=ReplyKeyboardRemove())
                    context.bot.send_message(chat_id=chat_id,
                                             text=query.getReply(
                                                 cname.i_contConverse))
                else:
                    reply_markup = InlineKeyboardMarkup(
                        self.create_changecancel_inline(purpose='allocation'))
                    update.message.reply_text(
                        f'You have a slot on {date}. Do you like to CHANGE or CANCEL it?',
                        reply_markup=reply_markup)
            else:
                context.bot.send_message(chat_id=chat_id,
                                         text='DB error',
                                         reply_markup=ReplyKeyboardRemove())
                context.bot.send_message(chat_id=chat_id,
                                         text=query.getReply(
                                             cname.i_contConverse))
        elif intent_name == "changeAvailability" or intent_name == "setAvailability":
            availabilities = query.getAvailability(user.id)
            availabilities = [
                utils.convertdate_for_display(date) for date in availabilities
            ]
            avail_string = '\n'.join(
                availabilities) if len(availabilities) > 0 else "None"
            reply_markup = InlineKeyboardMarkup(
                self.create_setcancel_inline(purpose='availability'))
            update.message.reply_text(query.getReply(cname.i_avail_set) +
                                      '\n' + avail_string,
                                      reply_markup=reply_markup)
        elif intent_name == "getEventDuration" or intent_name == "getEventTime":
            date = query.getAllocation(user.id)
            if utils.convertdate_for_display(date) != "None":
                startend = query.getEventStartEnd(date)
                start = startend[0]
                end = startend[1]
                date = utils.convertdate_for_display(date)
                context.bot.send_message(
                    chat_id=chat_id,
                    text=query.getReply(cname.i_duration_user_alloc).format(
                        date=date, start=start, end=end),
                    reply_markup=ReplyKeyboardRemove())
                context.bot.send_message(chat_id=chat_id,
                                         text=query.getReply(
                                             cname.i_contConverse))
            else:
                kb = self.create_dates_kb("/eventtime", query)
                context.bot.send_message(chat_id=chat_id,
                                         text="Please choose a date.",
                                         reply_markup=ReplyKeyboardMarkup(kb))
        else:
            resp_text = query.getReply(
                intent_name) if intent_name in query.replies.keys(
                ) else response.query_result.fulfillment_text
            context.bot.send_message(chat_id=update.effective_chat.id,
                                     text=resp_text,
                                     reply_markup=ReplyKeyboardRemove())
            if intent_name != "endConverse" and intent_name != 'contConverse' and intent_name != 'defaultWelcomeIntent':
                context.bot.send_message(chat_id=chat_id,
                                         text=query.getReply(
                                             cname.i_contConverse))