Exemplo n.º 1
0
def handle_pv_edit_profile_skills(telegram_profile, msg):
    if msg['text'] != bot_commands['return']:
        p = telegram_profile.profile

        skills = list(set(msg['text'].split('\n')))
        p.skills.all().delete()
        for skill in skills:
            if skill == '':
                continue
            skill = skill.replace(" ", "_")
            lf = Term.objects.filter(title=skill)
            if lf.exists():
                lf = lf.first()
            else:
                lf = Term.objects.create(
                    title=skill, taxonomy_type=TaxonomyType.LEARNING_FIELD)
            LearningInfo.objects.create(student=p, learning_field=lf)

    message = bot_profile_to_string(
        telegram_profile.profile) + '\n\n' + bot_messages['edit_profile']
    keyboard = bot_keyboards['edit_profile']

    telegram_profile.menu_state = MenuState.EDIT_PROFILE
    telegram_profile.save()

    return message, keyboard
Exemplo n.º 2
0
def handle_pv_edit_profile_bio(telegram_profile, msg):
    if msg['text'] != bot_commands['return']:
        p = telegram_profile.profile
        p.bio = msg['text']
        p.save()

    message = bot_profile_to_string(
        telegram_profile.profile) + '\n\n' + bot_messages['edit_profile']
    keyboard = bot_keyboards['edit_profile']

    telegram_profile.menu_state = MenuState.EDIT_PROFILE
    telegram_profile.save()

    return message, keyboard
Exemplo n.º 3
0
def handle_pv_start(telegram_profile, msg):
    if telegram_profile.profile:

        if msg['text'] == bot_commands['add_project']:
            message = bot_messages['add_project_get_content']
            keyboard = [[bot_commands['return']]]
            telegram_profile.menu_state = MenuState.ADD_PROJECT_JOB
            telegram_profile.save()
        elif msg['text'] == bot_commands['edit_profile']:
            message = bot_profile_to_string(
                telegram_profile.profile
            ) + '\n\n' + bot_messages['edit_profile']
            keyboard = bot_keyboards['edit_profile']
            telegram_profile.menu_state = MenuState.EDIT_PROFILE
            telegram_profile.save()
        else:
            message = bot_messages['unknown_command']
            keyboard = bot_keyboards['main_menu']

    else:
        if msg['text'] == bot_commands['login']:
            message = bot_messages['login_get_username_or_email']
            keyboard = bot_keyboards['return']
            telegram_profile.menu_state = MenuState.LOGIN_GET_USERNAME
            telegram_profile.save()

        elif msg['text'] == bot_commands['register']:
            message = bot_messages['register_get_email']
            keyboard = bot_keyboards['return']
            telegram_profile.menu_state = MenuState.REGISTER_GET_EMAIL
            telegram_profile.save()

        else:
            message = bot_messages['start_msg'] % (
                settings.HOST_URL, telegram_profile.verify_token)
            keyboard = [[bot_commands['login'], bot_commands['register']]]

    return message, keyboard
Exemplo n.º 4
0
    def post(self, request, format=None):
        msg = request.data['msg']

        telegram_user_id = msg['from']['id']
        telegram_profile = TelegramProfile.objects.filter(
            telegram_user_id=telegram_user_id)
        if telegram_profile.exists():
            telegram_profile = telegram_profile.first()
            if telegram_profile.pv_enabled:
                telegram_profile.pv_enabled = False
                telegram_profile.save()
        else:
            telegram_profile = TelegramProfile.objects.create(
                telegram_user_id=telegram_user_id, pv_enabled=True)

        if msg['text'] == 'خروج':
            telegram_profile.profile = None
            telegram_profile.user_input.all().delete()
            telegram_profile.menu_state = MenuState.START
            telegram_profile.save()

        if telegram_profile.profile and 'forward_from' in msg:
            try:
                message = bot_profile_to_string(
                    Profile.objects.get(
                        telegram_user_id=msg['forward_from']['id']))
            except:
                message = "پروفایل این کاربر در سایت ثبت نشده است."
            keyboard = [[]]

        elif telegram_profile.menu_state == MenuState.START:
            message, keyboard = handle_pv_start(telegram_profile, msg)

        elif telegram_profile.menu_state == MenuState.LOGIN_GET_USERNAME:
            message, keyboard = handle_pv_login_get_username(
                telegram_profile, msg)

        elif telegram_profile.menu_state == MenuState.LOGIN_GET_PASSWORD:
            message, keyboard = handle_pv_login_get_password(
                telegram_profile, msg)

        elif telegram_profile.menu_state == MenuState.REGISTER_GET_EMAIL:
            message, keyboard = handle_pv_register_get_email(
                telegram_profile, msg)

        elif telegram_profile.menu_state == MenuState.REGISTER_GET_USERNAME:
            message, keyboard = handle_pv_register_get_username(
                telegram_profile, msg)

        elif telegram_profile.menu_state == MenuState.REGISTER_GET_PASSWORD:
            message, keyboard = handle_pv_register_get_password(
                telegram_profile, msg)

        elif telegram_profile.menu_state == MenuState.ADD_PROJECT_JOB:
            message, keyboard = handle_pv_add_project(telegram_profile, msg)

        elif telegram_profile.menu_state == MenuState.ADD_PROJECT_JOB_GET_SKILLS:
            message, keyboard = handle_pv_add_project_get_skills(
                telegram_profile, msg)

        elif telegram_profile.menu_state == MenuState.EDIT_PROFILE:
            message, keyboard = handle_pv_edit_profile(telegram_profile, msg)

        elif telegram_profile.menu_state == MenuState.EDIT_PROFILE_NAME:
            message, keyboard = handle_pv_edit_profile_name(
                telegram_profile, msg)

        elif telegram_profile.menu_state == MenuState.EDIT_PROFILE_BIO:
            message, keyboard = handle_pv_edit_profile_bio(
                telegram_profile, msg)

        elif telegram_profile.menu_state == MenuState.EDIT_PROFILE_SKILLS:
            message, keyboard = handle_pv_edit_profile_skills(
                telegram_profile, msg)

        else:
            message = "Unknown app state"
            keyboard = [[]]

        return Response(
            {
                "chat_id": msg['chat']['id'],
                "message": message,
                "keyboard": keyboard,
            },
            status=status.HTTP_200_OK)