Exemplo n.º 1
0
def send_participation_cancellation_notification(chat_id, meal):
    from foodshare.bdd.database_communication import get_user_from_chat_id

    canceller = get_user_from_chat_id(chat_id)
    message = (f'User {canceller.name} cancelled his participation to the '
               f'meal {meal.what} on {meal.when}')
    bot.send_message(chat_id=meal.who_cooks.telegram_id, text=message)
Exemplo n.º 2
0
def community_action(update, context):
    chat_id = update.effective_chat.id
    user = get_user_from_chat_id(chat_id)
    community = user.community
    message = emojize(
        f'You\'re in the community \n :family: {community.name} \n '
        f':desert_island: whose '
        f'description '
        f'is :  {community.description} \n What do you want to do?')
    buttons = [
        [IKB('Quit community', callback_data='quit')],
    ]
    if user.admin:
        buttons.append([IKB('Invite people', callback_data='invite')])
    buttons.append([IKB('Back', callback_data='back')])
    keyboard = InlineKeyboardMarkup(buttons)
    bot = context.bot
    ud = context.user_data
    if 'last_message' in ud:
        last_message = ud['last_message']
        bot.edit_message_text(
            message_id=last_message.message_id,
            chat_id=chat_id,
            text=message,
            reply_markup=keyboard,
        )
    else:
        ud['last_message'] = bot.send_message(chat_id=chat_id,
                                              text=message,
                                              reply_markup=keyboard)
    return ConversationStage.ACTION
Exemplo n.º 3
0
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
Exemplo n.º 4
0
def process_user_selection(update, context):
    ud = context.user_data
    money = ud['money_or_meal']
    callback_data = update.callback_query.data
    chat_id = update.effective_chat.id
    user = get_user_from_chat_id(chat_id)
    members = [
        member for member in user.community.members if member is not user
    ]
    # initialize some variables in `context.user_data` when the keyboard is
    # first called
    if '_number' not in ud:
        number = ''
        ud['_number'] = ''
    else:
        number = ud.get('_number')
    if '_page' not in ud:
        page = 0
        ud['_page'] = 0
    else:
        page = ud.get('_page')

    # process the keyboard callback data
    if callback_data == 'forward_page':
        page += 1
    elif callback_data == 'backward_page':
        page -= 1
    elif callback_data == 'back':
        ud.pop('_number')
        ud.pop('_page')
        return False, True, -1
    elif callback_data == 'confirm':
        user_chosed = members[int(number) - 1]
        ud.pop('_number')
        ud.pop('_page')
        return True, False, user_chosed
    else:
        number = callback_data

    # store number for next callback
    ud['_number'] = number
    ud['_page'] = page

    if number == '':
        message = 'Please select a user to make the transaction with'
    else:
        user_chosed = members[int(number) - 1]
        message = (
            f'You chosed *'
            f' {user_chosed.name}.* \n Press '
            f'confirm to continue. \n'
        )
    # choose keyboard with a confirm button if a valid number was selected
    keyboard = construct_keyboard(user, money, number != '', page)
    update.callback_query.edit_message_text(
        text=message, reply_markup=keyboard, parse_mode=ParseMode.MARKDOWN,
    )
    return False, False, -1
Exemplo n.º 5
0
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
Exemplo n.º 6
0
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
Exemplo n.º 7
0
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
Exemplo n.º 8
0
def process_meal_selection(update, context):
    ud = context.user_data
    callback_data = update.callback_query.data
    chat_id = update.effective_chat.id
    user = get_user_from_chat_id(chat_id)
    all_meals, meals_as_cook = get_all_meals(user)
    if '_page' not in ud:
        page = 0
        ud['_page'] = 0
    else:
        page = ud.get('_page')

    if callback_data == 'forward_page':
        page += 1
        ud['_page'] = page
    elif callback_data == 'backward_page':
        page -= 1
        ud['_page'] = page
    elif callback_data == 'back':
        ud.pop('_page')
        return False, True, -1, -1
    elif callback_data == 'cancel_meal':
        meal = all_meals[page % len(all_meals)]
        ud.pop('_page')
        return True, False, meal, True
    else:
        meal = all_meals[page % len(all_meals)]
        return True, False, meal, False
    meal = all_meals[page % len(all_meals)]
    print(all_meals)
    print(page)
    print(page)
    print(meal)
    # print(page)
    keyboard = (cancel_meal_keyboard
                if meal in meals_as_cook else cancel_participation_keyboard)
    message = create_meal_message(meal)
    update.callback_query.edit_message_text(
        text=message,
        reply_markup=keyboard,
    )
    return False, False, -1, -1
Exemplo n.º 9
0
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
Exemplo n.º 10
0
def first_message(update, context, prefix=''):
    chat_id = update.effective_chat.id
    user = get_user_from_chat_id(chat_id)
    ud = context.user_data
    if user is None:
        message = (
            "Hello there! First time we meet isn't it? "
            "I just need a few information about you!"
        )
        keyboard = InlineKeyboardMarkup(
            [[IKB('Register', callback_data='register_asked0523')]]
        )
        bot = context.bot
        chat_id = update.effective_chat.id
        context.user_data['last_message'] = bot.send_message(
            chat_id=chat_id, text=message, reply_markup=keyboard
        )
        return ConversationHandler.END
    elif user.community is None:
        message = "Before anything you need to join or create a community!"
        keyboard = InlineKeyboardMarkup(
            [
                [
                    IKB(
                        'Join or create a community',
                        callback_data='joining_asked0523',
                    )
                ]
            ]
        )
        bot = context.bot
        chat_id = update.effective_chat.id
        context.user_data['last_message'] = bot.send_message(
            chat_id=chat_id, text=message, reply_markup=keyboard
        )
        return ConversationHandler.END
    elif len(user.community.members) < 2:
        message = (
            "Before anything you need to invite people to your "
            "community or join one with already some people in it!"
        )
        keyboard = InlineKeyboardMarkup(
            [
                [
                    IKB(
                        'Invite people or quit community',
                        callback_data='invite_asked0523',
                    )
                ]
            ]
        )
        bot = context.bot
        chat_id = update.effective_chat.id
        context.user_data['last_message'] = bot.send_message(
            chat_id=chat_id, text=message, reply_markup=keyboard
        )
        return ConversationHandler.END
    else:
        message = prefix + "What do you want to do?"
        keyboard = InlineKeyboardMarkup(
            [
                [
                    IKB('Cook', callback_data='cook_asked0523'),
                    IKB('See meals', callback_data='meals_asked0523'),
                ],
                [
                    IKB(
                        'Make a transaction',
                        callback_data='balances_asked0523',
                    ),
                    IKB(
                        'Manage community', callback_data='community_asked0523'
                    ),
                ],
            ]
        )
        bot = context.bot
        chat_id = update.effective_chat.id
        if 'last_message' not in ud:
            context.user_data['last_message'] = bot.send_message(
                chat_id=chat_id,
                text=message,
                reply_markup=keyboard,
                parse_mode=ParseMode.MARKDOWN,
            )
        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,
                parse_mode=ParseMode.MARKDOWN,
            )
        return ConversationHandler.END