Beispiel #1
0
def add_participant(update: Update, user: User, leaderboard: Leaderboard):
    """
    Add Participant to db if not present
    """
    chat_type = update.effective_chat.type
    participant = get_participant_by_user_id_and_leaderboard_id(
        user_id=user.id, leaderboard_id=leaderboard.id)

    if not participant:
        participant = Participant(user_id=user.id,
                                  leaderboard_id=leaderboard.id)
        participant.save_participant()

        #If group chat - notify participants that new participant was added
        if chat_type != 'private':
            update.message.reply_text(
                f'{user.name} was added to the Leaderboard {leaderboard.name}',
                quote=False)
    return participant
Beispiel #2
0
def execute_activity(update: Update, context):
    query = update.callback_query
    query.answer()

    button_id, activity_id, activity_name, points = query.data.split('_')
    activity_id = int(activity_id)
    button_id = int(button_id)

    #Remove Inline Keyboard
    query.edit_message_reply_markup(None, quote=False)
    #Send user choise as a message
    query.message.reply_text(
        query.message.reply_markup.inline_keyboard[button_id][0].text,
        quote=False)

    #Return to user input in case Calcel is clicked
    if activity_id == -1:
        return wait_for_input(update, context)

    user_id = update.effective_user.id
    leaderboard_id = update.effective_chat.id
    participant = get_participant_by_user_id_and_leaderboard_id(
        user_id=user_id, leaderboard_id=leaderboard_id)
    if not participant:
        participant = Participant(user_id=user_id,
                                  leaderboard_id=leaderboard_id)
        participant.save_participant()

    # Create performed Activity
    performed_activity = Performed_Activity(activity_id=activity_id,
                                            participant_id=participant.id)
    performed_activity.save_performed_activity()

    query.message.reply_text(
        f'Activity {activity_name} was tracked\n'
        f'{points} points were added to @{update.effective_user.username}',
        quote=False)
    return ConversationHandler.END