Ejemplo n.º 1
0
def check_in_db_ready_to_change(new_mes, user_id, type_of_messenger):
    session = data_base.get_sqlalchemy_session()
    for row in session.query(User.ready_to_change).filter(
            or_(User.id_vk == user_id, User.id_teleg == user_id)):
        if row.ready_to_change == 'true':
            return try_to_change_platform(new_mes, user_id, type_of_messenger)
    return constants.STR_NO_ANSWER
Ejemplo n.º 2
0
def send_message_in_new_platform(new_mes, user_id, type_of_messenger):
    new_id = cast_to_int(new_mes)
    session = data_base.get_sqlalchemy_session()
    # Прислали из вк айди на телегу
    if type_of_messenger == constants.VK_MESSENGER['messenger_name']:
        rows = session.query(User).filter(User.id_vk == user_id)
        for row in rows:
            if row.ready_to_change == 'true' and row.id_last_message == constants.SYSTEM_CONSTANT_CHANGING_TO_TELEGRAM:
                row.id_teleg = new_id
                row.ready_to_change = 'false'
                session.commit()
                bots.TelegramBot.send_mes_in_new_platform(
                    bots.TelegramBot, new_id)
                return constants.STR_ID_WAS_SAVED

    # Прислали из телеги айди на вк
    elif type_of_messenger == constants.TELEGRAM_MESSENGER['messenger_name']:
        rows = session.query(User).filter(User.id_teleg == user_id)
        for row in rows:
            if row.ready_to_change == 'true' and row.id_last_message == constants.SYSTEM_CONSTANT_CHANGING_TO_VK:
                bots.VKBot.send_mes_in_new_platform(bots.VKBot, new_id)
                row.id_vk = new_id
                row.ready_to_change = 'false'
                session.commit()
                return constants.STR_ID_WAS_SAVED
    return constants.STR_NO_ANSWER
Ejemplo n.º 3
0
def is_user_new(user_id, type_of_messenger):
    session = data_base.get_sqlalchemy_session()
    rows = session.query(User.id_teleg, User.id_vk)
    for row in rows:
        if list(filter((lambda x: x == user_id), row)):
            return constants.STR_START_AGAIN
    if type_of_messenger == constants.VK_MESSENGER['messenger_name']:
        session.add(User(id_vk=user_id, ready_to_change='false'))
    elif type_of_messenger == constants.TELEGRAM_MESSENGER['messenger_name']:
        session.add(User(id_teleg=user_id, ready_to_change='false'))
    session.commit()
    return constants.STR_NEW_USER
Ejemplo n.º 4
0
def get_answer_from_data_base(new_mes, user_id):
    session = data_base.get_sqlalchemy_session()
    rows = session.query(Quest.id_quest, Quest.text_quest,
                         Quest.text_quest_answer)
    for row in rows:
        # Получаем массив элементов из строки text_quest, варианты которой разделены / и ищем сходства
        for i in range(len(row.text_quest.split('/'))):
            if row.text_quest.split('/')[i].startswith(new_mes):
                id_quest = row.id_quest
                save_quest_id(user_id, id_quest)
                return row.text_quest_answer
            i += 1
    return constants.STR_NO_ANSWER
Ejemplo n.º 5
0
def save_quest_id(user_id, quest_id):
    session = data_base.get_sqlalchemy_session()
    # Если запрос на смену платформы
    users = session.query(User).filter(
        or_(User.id_teleg == user_id, User.id_vk == user_id))
    if quest_id == constants.QUEST_ID_TO_START_CHANGE_PLATFORM:
        for user in users:
            user.id_last_message = quest_id
            user.ready_to_change = 'true'
    else:
        for user in users:
            user.id_last_message = quest_id
            user.ready_to_change = 'false'
    session.commit()
Ejemplo n.º 6
0
def try_to_change_platform(new_mes, userd_id, type_of_messenger):
    session = data_base.get_sqlalchemy_session()
    for row in session.query(User).filter(
            or_(User.id_vk == userd_id, User.id_teleg == userd_id)):
        # защита от дурака: пользователь был в том же мессенджере, который выбрал
        if (type_of_messenger == constants.VK_MESSENGER['messenger_name']
                and new_mes == constants.VK_MESSENGER['messenger_choice']
                or type_of_messenger
                == constants.TELEGRAM_MESSENGER['messenger_name'] and new_mes
                == constants.TELEGRAM_MESSENGER['messenger_choice']):
            row.ready_to_change = 'false'
            session.commit()
            return constants.STR_RIGHT_CHANNEL

        # если были в вк и уходим в телегу
        elif type_of_messenger == constants.VK_MESSENGER['messenger_name'] and \
                new_mes == constants.TELEGRAM_MESSENGER['messenger_choice']:
            # при наличии id teleg
            if row.id_teleg is not None:
                # изменяем состояние пользователя
                row.ready_to_change = 'false'
                session.commit()
                # отправляем сообщение в выбранной платформе
                bots.TelegramBot.send_mes_in_new_platform(
                    bots.TelegramBot, row.id_teleg)
                return constants.STR_CHANGED_FROM_VK_TO_TELEG
            else:
                # при отсутствии id teleg запрашиваем ввод id
                row.id_last_message = constants.SYSTEM_CONSTANT_CHANGING_TO_TELEGRAM
                session.commit()
                return constants.STR_INPUT_ID_TELEG

        # если были в телеге и уходим в вк
        elif type_of_messenger == constants.TELEGRAM_MESSENGER['messenger_name'] and \
                new_mes == constants.VK_MESSENGER['messenger_choice']:
            # при наличии id vk
            if row.id_vk is not None:
                # изменяем состояние пользователя
                row.ready_to_change = 'false'
                session.commit()
                # отправляем сообщение в выбранной платформе
                bots.VKBot.send_mes_in_new_platform(bots.VKBot, row.id_vk)
                return constants.STR_CHANGE_FROM_TELEG_TO_VK
            else:
                # при отсутствии id vk запрашиваем ввод id
                row.id_last_message = constants.SYSTEM_CONSTANT_CHANGING_TO_VK
                session.commit()
                return constants.STR_INPUT_ID_VK