def cancel_activity(update: Update, context): query = update.callback_query query.answer() button_id, performed_activity_id = query.data.split('_') performed_activity_id = int(performed_activity_id) button_id = int(button_id) #Remove Inline Keyboard query.edit_message_reply_markup(None) query.message.reply_text( query.message.reply_markup.inline_keyboard[button_id][0].text, quote=False) if performed_activity_id == -1: return cancel(update, context) #Delete performed activity from the database performed_activity = get_performed_activity_by_id(id=performed_activity_id) activity = get_activity_by_id(activity_id=performed_activity.activity_id) performed_activity.delete_performed_activity() query.message.reply_text( f"{activity.activity_name} - {performed_activity.time_created:%m-%d %H:%M} was canceled β", quote=False) # Get new user input return ConversationHandler.END
def delete(update: Update, context): query = update.callback_query query.answer() button_id, activity_id = query.data.split('_') activity_id = int(activity_id) button_id = int(button_id) #Remove Inline Keyboard query.edit_message_reply_markup(None) query.message.reply_text( query.message.reply_markup.inline_keyboard[button_id][0].text, quote=False) if activity_id == -1: return wait_for_input(update, context) #Delete activity from the database activity = get_activity_by_id(activity_id=activity_id) activity.delete_activity() query.message.reply_text( f'Activity {activity.activity_name} was deleted β', quote=False) # Get new user input return ConversationHandler.END
def update_activity(update: Update, context): if update.callback_query is None: activity_id = context.user_data[ACTIVITY] button_id = context.user_data[UPDATE] query = update else: query = update.callback_query activity_id, button_id = query.data.split('_') query.answer() #Remove Inline Keyboard query.edit_message_reply_markup(None, quote=False) activity_id = int(activity_id) button_id = int(button_id) context.user_data[ACTIVITY] = activity_id if button_id == -1: return update_activity_command_handler(update, context) elif button_id == CHANGE_NAME: if update.callback_query is not None: query.message.reply_text( query.message.reply_markup.inline_keyboard[0][0].text, quote=False) force_reply = ForceReply(force_reply=True) query.message.reply_text(f'What is the new name of the activity', reply_markup=force_reply, quote=False) return CHANGE_ACTIVITY_NAME elif button_id == CHANGE_POINTS: activity = get_activity_by_id(activity_id=activity_id) if update.callback_query is not None: query.message.reply_text( query.message.reply_markup.inline_keyboard[0][0].text, quote=False) force_reply = ForceReply(force_reply=True) query.message.reply_text( f'How many points should I assign for {activity.activity_name} ?', reply_markup=force_reply, quote=False) return CHANGE_ACTIVITY_POINTS
def change_activity_name(update: Update, context): activity_name = update.message.text activity_id = context.user_data[ACTIVITY] if not activity_name: force_reply = ForceReply(force_reply=True) update.message.reply_text( f'Only text is allowed as a name of the Activity', reply_markup=force_reply, quote=False) context.user_data[UPDATE] = CHANGE_NAME return update_activity(update, context) activity = get_activity_by_id(activity_id=activity_id) activity.activity_name = activity_name activity.save_activity() update.message.reply_text( f'Ok. Activity name is changed to {activity_name} π', quote=False) return ConversationHandler.END
def change_activity_points(update: Update, context): points = update.message.text activity_id = context.user_data[ACTIVITY] try: points = int(points) except: # Telegram clients will display a reply interface to the user # (act as if the user has selected the botβs message and tapped βReplyβ) force_reply = ForceReply(force_reply=True, selective=True) update.message.reply_text( f'{points} is not a number. Please enter number π', reply_markup=force_reply) context.user_data[UPDATE] = CHANGE_POINTS return update_activity(update, context) else: activity = get_activity_by_id(activity_id=activity_id) activity.points = points activity.save_activity() update.message.reply_text( f'Ok. Activity {activity.activity_name} gives {points} points π', quote=False) return ConversationHandler.END
def what_to_update(update: Update, context): query = update.callback_query query.answer() button_id, activity_id = query.data.split('_') activity_id = int(activity_id) button_id = int(button_id) #Remove Inline Keyboard query.edit_message_reply_markup(None) if activity_id == -1: return ConversationHandler.END activity = get_activity_by_id(activity_id=activity_id) keyboard = [] keys = [ InlineKeyboardButton(f'π Change name', callback_data=f"{activity_id}_{CHANGE_NAME}"), InlineKeyboardButton(f'π Change points', callback_data=f"{activity_id}_{CHANGE_POINTS}") ] back_key = [ InlineKeyboardButton(f'π Back', callback_data=f"{activity_id}_{-1}") ] keyboard.append(keys) keyboard.append(back_key) keyboard_markup = InlineKeyboardMarkup(keyboard) query.message.reply_text(f"{activity.activity_name} β {activity.points}π", reply_markup=keyboard_markup, quote=False) # Get new user input return UPDATE_FINILAZE