def ask_to_chose_action(update, context): chat_id = update.effective_chat.id user = get_user_from_chat_id(chat_id) ud = context.user_data all_meals, meals_as_cook = get_all_meals(user) bot = context.bot if len(all_meals) > 0: meal = all_meals[0] keyboard = ( cancel_meal_keyboard if meal in meals_as_cook else cancel_participation_keyboard ) message = create_meal_message(meal) if 'last_message' not in ud: last_message = bot.send_message( chat_id=chat_id, text=message, reply_markup=keyboard ) ud['last_message'] = last_message else: last_message = ud['last_message'] bot.edit_message_text( message_id=last_message.message_id, chat_id=chat_id, text=message, reply_markup=keyboard, ) return ConversationStage.CHOSING_MEAL else: first_message( update, context, prefix='*No meals where you cook ' 'or ' 'you participate* \n', ) return ConversationHandler.END
def quit(update, context): chat_id = update.effective_chat.id bot = context.bot user = get_user_from_chat_id(chat_id) members = user.community.members ud = context.user_data last_message = ud['last_message'] all_meals = get_all_meals(user) # admins = [member for member in user.community.members if member.admin] if len(members) < 2: message = ( f'Are you sure you want to quit the community? Since you\'re ' f'the last member this will delete it') keyboard = InlineKeyboardMarkup([ [IKB('Confirm', callback_data='confirm')], [IKB('Back', callback_data='back')], ]) bot.edit_message_text( message_id=last_message.message_id, chat_id=chat_id, text=message, reply_markup=keyboard, ) return ConversationStage.QUITTING # elif len(admins) < 2 and user.admin: # bot.edit_message_text(message_id=last_message.message_id, # chat_id=chat_id, text='To quit this community you need to name ' # 'another administrator' # ) # propose # # to name another admin # return ConversationHandler.END elif user.money_balance < 0: prefix = ('*To quit the community you need to ' 'have a money balance superior to zero.' '\nAsk another user to make a ' 'transaction* \n') first_message(update, context, prefix) return ConversationHandler.END elif len(all_meals) == 0: prefix = ('*To quit the community you need to have no meal ongoing ' 'please cancel your participation and the ones you organize ' 'before leaving.') first_message(update, context, prefix) return ConversationHandler.END else: message = f'Are you sure you want to quit the community?' keyboard = InlineKeyboardMarkup([ [IKB('Confirm', callback_data='confirm')], [IKB('Back', callback_data='back')], ]) bot.edit_message_text( message_id=last_message.message_id, chat_id=chat_id, text=message, reply_markup=keyboard, ) return ConversationStage.QUITTING
def quit_end(update, context): chat_id = update.effective_chat.id ud = context.user_data last_message = ud['last_message'] remove_user_from_community(chat_id) context.bot.delete_message( message_id=last_message.message_id, chat_id=chat_id, ) first_message(update, context) return ConversationHandler.END
def joining_end(update, context): ud = context.user_data token = ud['token'] last_message = ud['last_message'] chat_id = update.effective_chat.id add_user_to_community(chat_id, token) ud.clear() context.bot.delete_message( message_id=last_message.message_id, chat_id=chat_id, ) first_message(update, context) return ConversationHandler.END
def meal_action_end(update, context): chat_id = update.effective_chat.id ud = context.user_data meal = ud['meal_being_processed'] cancel_meal = ud['cancel_meal'] too_late = ud['too_late'] optional = 'your participation at ' nothing = '' prefix = ( f'*{optional if not cancel_meal else nothing} ' f'The meal {meal.what} on {meal.when} has been cancelled* \n' ) process_meal_action(chat_id, cancel_meal, meal, too_late) first_message(update, context, prefix=prefix) return ConversationHandler.END
def send_token(update, context): bot = context.bot chat_id = update.effective_chat.id ud = context.user_data last_message = ud['last_message'] user = get_user_from_chat_id(chat_id) community = user.community token = add_token(community) message = (f'Here is your token to invite one person, it will only work ' f'once : {token}') bot.edit_message_text(message_id=last_message.message_id, chat_id=chat_id, text=message) ud.clear() first_message(update, context) return ConversationHandler.END
def hard_restart(update, context): from foodshare.handlers.start_conversation.first_message import ( first_message, ) ud = context.user_data bot = context.bot chat_id = update.effective_chat.id if 'last_message' in ud: last_message = ud['last_message'] bot.delete_message( message_id=last_message.message_id, chat_id=chat_id, ) ud.clear() first_message(update, context) return ConversationHandler.END
def ask_for_user(update, context): chat_id = update.effective_chat.id user = get_user_from_chat_id(chat_id) callback_data = update.callback_query.data if callback_data == 'back': first_message(update, context) return ConversationHandler.END money = callback_data == 'money' context.user_data['money_or_meal'] = money keyboard = user_selection.construct_keyboard(user, money, False) update.callback_query.edit_message_text( text='Please select a user to make the transaction with', reply_markup=keyboard, parse_mode=ParseMode.MARKDOWN, ) return ConversationStage.SELECTING_USER
def action_chosing_handler(update, context): ( action_is_selected, want_back, meal, cancel_meal, ) = process_meal_selection(update, context) ud = context.user_data ud['meal_being_processed'] = meal ud['cancel_meal'] = cancel_meal if want_back: first_message(update, context) return ConversationHandler.END elif not action_is_selected: return ConversationStage.CHOSING_MEAL return cancelling(update, context)
def transaction_end(update, context): ud = context.user_data money = ud['money_or_meal'] to_whom = ud['user_transaction'] amount = ud['amount'] date_time = datetime.now() chat_id = update.effective_chat.id new_balance = add_transaction(chat_id, money, to_whom, amount, date_time) last_message = ud['last_message'] context.bot.delete_message( chat_id=chat_id, message_id=last_message.message_id ) ud.clear() prefix = f'*Your balance is now {new_balance}' prefix += '€*\n' if money else ' meals* \n' first_message(update, context, prefix) return ConversationHandler.END
def creating_end(update, context): from foodshare.handlers.start_conversation.first_message import ( first_message, ) # to # avoid circular # dependency ud = context.user_data chat_id = update.effective_chat.id name = ud['community_name'] description = ud['community_description'] last_message = ud['last_message'] bot = context.bot bot.delete_message(message_id=last_message.message_id, chat_id=chat_id) chat_id = update.effective_chat.id add_community(name, description, chat_id) ud.clear() first_message(update, context) return ConversationHandler.END
def end(update, context): # sticker_id = ( # 'CAACAgIAAxkBAAIJNF6N7Cj5oZ7qs9hrRce8HdLTn' # '7FdAAKcAgACa8TKChTuhP744omRGAQ' # ) # Lazybone ID bot = context.bot chat_id = context.user_data['chat_id'] ud = context.user_data bot.deleteMessage(chat_id, ud['last_message'].message_id) who_cooks = get_user_from_chat_id(chat_id) gif_url = get_gif_url(ud['meal_name']) if gif_url is not None: bot.send_document(chat_id=chat_id, document=gif_url) add_meal(who_cooks, ud, gif_url) # bot.send_sticker(chat_id, sticker_id) ud.clear() prefix = f'Messages sent : I will update you on the answers \n' while not handle_meals(): pass first_message(update, context, prefix=prefix) return ConversationHandler.END
def back_end(update, context): first_message(update, context) return ConversationHandler.END
def end_cook(update, context): first_message(update, context) return ConversationHandler.END