def set_location(bot, update):
    '''
    '''
    if (update.message.text == 'Москва'
            or update.message.text == 'Санкт-Петербург'):
        users.update_one({'chat_id': update.message.chat_id},
                         {'$set': {
                             'location': update.message.text
                         }})
        cursor_organizations = organizations.find(
            {'location': update.message.text})
        organizations_list = []
        for unit in cursor_organizations:
            organizations_list.append(unit['name'])
            add = 'organization.' + unit['name']
            users.update_one(
                {'chat_id': update.message.chat_id},
                {'$set': {
                    add: {
                        'status': False,
                        'name': unit['name']
                    }
                }})
        organizations_list.append('Закончить')
        keyboard = [organizations_list]
        reply_markup = ReplyKeyboardMarkup(keyboard,
                                           one_time_keyboard=True,
                                           resize_keyboard=True)
        bot.send_message(
            chat_id=update.message.chat_id,
            text=
            'На какие организации ты хочешь подписаться? Выбирай по-очереди!',
            reply_markup=reply_markup)
        return 'set organizations recurrent'
    else:
        keyboard = [locations]
        reply_markup = ReplyKeyboardMarkup(keyboard,
                                           one_time_keyboard=True,
                                           resize_keyboard=True)
        bot.send_message(chat_id=update.message.chat_id,
                         text='Для выбора, нажми на соответствующую кнопку:)',
                         reply_markup=reply_markup)
        return 'set location'
Esempio n. 2
0
def post_vuz(bot, update):
    '''
    '''
    event_posts.update_one(
        {
            'chat_id': update.message.chat_id,
            "application_completed?": False
        }, {'$set': {
            "location": update.message.text
        }})
    text = 'В каком ВУЗе будет проходить мероприятие?'
    cursor_organizations = organizations.find(
        {'location': update.message.text})
    org_list = []
    for unit in cursor_organizations:
        org_list.append(unit['name'])
    keyboard = [org_list]
    reply_markup = ReplyKeyboardMarkup(keyboard,
                                       one_time_keyboard=True,
                                       resize_keyboard=True)
    bot.send_message(chat_id=update.message.chat_id,
                     text=text,
                     reply_markup=reply_markup)
    return 'description'
def set_organizations_recurrent(bot, update):
    '''
    '''
    non_subscribed_organizations_list = []
    subscribed_organizations_list = []
    cursor_user = users.find_one({'chat_id': update.message.chat_id})
    for unit in organizations.find({'location': cursor_user['location']}):
        if cursor_user['organization'][unit['name']]['status'] == False:
            non_subscribed_organizations_list.append(unit['name'])
        else:
            subscribed_organizations_list.append(unit['name'])

    if update.message.text in non_subscribed_organizations_list:
        users.update_one({'chat_id': update.message.chat_id}, {
            '$set': {
                ('organization.' + update.message.text): {
                    'status': True,
                    'name': update.message.text
                }
            }
        })
        bot.send_message(chat_id=update.message.chat_id, text='Добавлено!')
        non_subscribed_organizations_list.remove(update.message.text)
        subscribed_organizations_list.append(update.message.text)

        cursor_organizations = organizations.find({})
        if len(non_subscribed_organizations_list) == 0:
            bot.send_message(
                chat_id=update.message.chat_id,
                text='Ты уже подписался на все доступные организации)')
            return set_preferences_logic(bot, update)
        elif len(subscribed_organizations_list) == 0:
            non_subscribed_organizations_list.append('Закончить')
            keyboard = [non_subscribed_organizations_list]
            reply_markup = ReplyKeyboardMarkup(keyboard,
                                               one_time_keyboard=True,
                                               resize_keyboard=True)
            bot.send_message(
                chat_id=update.message.chat_id,
                text='На какие организации ты хочешь подписаться?',
                reply_markup=reply_markup)
            return 'set organizations recurrent'
        else:
            non_subscribed_organizations_list.append('Закончить')
            keyboard = [non_subscribed_organizations_list]
            reply_markup = ReplyKeyboardMarkup(keyboard,
                                               one_time_keyboard=True,
                                               resize_keyboard=True)
            bot.send_message(
                chat_id=update.message.chat_id,
                text='На какие ещё организации ты хочешь подписаться?',
                reply_markup=reply_markup)
            return 'set organizations recurrent'
    elif update.message.text == 'Закончить':
        if len(subscribed_organizations_list) > 0:
            keyboard = ['ИЛИ', 'ТОЛЬКО']
            reply_markup = ReplyKeyboardMarkup([keyboard],
                                               one_time_keyboard=True,
                                               resize_keyboard=True)
            text = 'Выбери "ИЛИ", если хочешь получать анонсы событий выбранных категорий не только в организации, на которую ты подписался, но и в остальных в твоём городе.\n'
            text += 'Выбери "ТОЛЬКО", если хочешь получать анонсы только по выбранным категориям и только в организациях, на которые ты подписался)'
            bot.send_message(chat_id=update.message.chat_id,
                             text=text,
                             reply_markup=reply_markup)
            return 'set preferences logic'
        else:
            text = """
        А теперь получай анонсы событий в одном месте:). По мере появления, я буду отправлять их тебе.\n
По умолчанию, тебе будут приходить все анонсы. Для большей персонализации, жми /settings :)
        """
            bot.send_message(chat_id=update.message.chat_id, text=text)
            help_function(bot, update)
            return -1
    else:
        cursor_organizations = organizations.find({})
        organizations_list = []
        for unit in cursor_organizations:
            organizations_list.append(unit['name'])
        organizations_list.append('Закончить')
        keyboard = [organizations_list]
        reply_markup = ReplyKeyboardMarkup([keyboard],
                                           one_time_keyboard=True,
                                           resize_keyboard=True)
        bot.send_message(
            chat_id=update.message.chat_id,
            text=
            'Для выбора нажми на соответствующую кнопку. На какие организации ты хочешь подписаться? Выбирай по-очереди!',
            reply_markup=reply_markup)
        return 'set organizations recurrent'
def change_settings_location(bot, update):
    '''
    '''
    users.update_one({
        'chat_id': update.message.chat_id,
        'subscriber': True
    }, {'$set': {
        'location': update.message.text
    }})

    cursor_settings = users.find_one({
        'chat_id': update.message.chat_id,
        'subscriber': True
    })
    cursor_organizations = organizations.find({})
    for unit in cursor_organizations:
        add = 'organization.' + unit['name']
        users.update_one(
            {'chat_id': update.message.chat_id},
            {'$set': {
                add: {
                    'status': False,
                    'name': unit['name']
                }
            }})
    subscriptions_themes = ''
    subscriptions_organization = ''
    count_themes_non_subscribed = 0
    count_themes_subscribed = 0
    count_organizations_non_subscribed = 0
    count_organizations_subscribed = 0

    # getting event themes followed
    count = 0
    for theme in cursor_settings['event_theme']:
        if cursor_settings['event_theme'][theme]['status'] == True:
            count_themes_subscribed += 1
            if count == 0:
                subscriptions_themes += cursor_settings['event_theme'][theme][
                    'RU'].lower()
                count += 1
            else:
                subscriptions_themes += (
                    ', ' + cursor_settings['event_theme'][theme]['RU'].lower())
        else:
            count_themes_non_subscribed += 1
    #getting organizations followed
    count = 0
    for organization in cursor_settings['organization']:
        if cursor_settings['organization'][organization]['status'] == True:
            count_organizations_subscribed += 1
            if count == 0:
                subscriptions_organization += cursor_settings['organization'][
                    organization]['name']
                count += 1
            else:
                subscriptions_organization += (
                    ', ' +
                    cursor_settings['organization'][organization]['name'])
        else:
            count_organizations_non_subscribed += 1
    # getting location name
    if cursor_settings['location'] == 'Москва':
        location = 'Москве'
    elif cursor_settings['location'] == 'Санкт-Петербург':
        location = 'Питере'
    # костыльно меняем текст))))
    count_organizations_subscribed = 0
    if count_themes_non_subscribed != 0:
        text = "Сейчас ты подписан на эти категории событий в " + location + ": " + subscriptions_themes + '.\n'
    elif count_themes_subscribed == 0:
        text = "Сейчас ты не подписан ни на одну из категорий событий  в " + location + ".\n"
    else:
        text = "Сейчас ты подписан на все категории событий в " + location + ": " + subscriptions_themes + '.\n'
    if count_organizations_non_subscribed == 0:
        text += 'Также ты подписан на все организации в ' + location + ': ' + subscriptions_organization + '.\n'
    elif count_organizations_subscribed == 0:
        text += 'Также ты не подписан ни на одну из организаций в ' + location + '.\n'
    else:
        text += 'Также ты подписан на эти организации в ' + location + ': ' + subscriptions_organization + '.\n'
    text += 'Что сделать?:)'
    keyboard = [['Изменить тематики'], ['Изменить организации'],
                ['Изменить регион'], ['Изменить тип персонализации'],
                ['Всё ок!']]
    reply_markup = ReplyKeyboardMarkup(keyboard,
                                       one_time_keyboard=True,
                                       resize_keyboard=True)
    bot.send_message(chat_id=update.message.chat_id,
                     text=text,
                     reply_markup=reply_markup)
    return 'change settings'
def change_settings_organizations(bot, update):
    '''
    '''
    cursor_settings = users.find_one({
        'chat_id': update.message.chat_id,
        'subscriber': True
    })
    if update.message.text == 'Добавить':
        non_subscriptions = []
        for unit in organizations.find(
            {'location': cursor_settings['location']}):
            if cursor_settings['organization'][unit['name']]['status'] != True:
                non_subscriptions.append(
                    cursor_settings['organization'][unit['name']]['name'])
        if len(non_subscriptions) > 0:
            text = 'Ты можешь подписаться на эти организации)'
            keyboard = [non_subscriptions]
            reply_markup = ReplyKeyboardMarkup(keyboard,
                                               one_time_keyboard=True,
                                               resize_keyboard=True)
            bot.send_message(chat_id=update.message.chat_id,
                             text=text,
                             reply_markup=reply_markup)
            return 'change settings organizations add'
        else:
            text = 'Ты уже подписан на все категории)'
            bot.send_message(chat_id=update.message.chat_id, text=text)
            return -1
    elif update.message.text == 'Удалить':
        subscriptions = []
        for unit in organizations.find(
            {'location': cursor_settings['location']}):
            if cursor_settings['organization'][
                    unit['name']]['status'] != False:
                subscriptions.append(
                    cursor_settings['organization'][unit['name']]['name'])
        if len(subscriptions) > 0:
            text = 'Что удалить из текущих подписок?'
            keyboard = [subscriptions]
            reply_markup = ReplyKeyboardMarkup(keyboard,
                                               one_time_keyboard=True,
                                               resize_keyboard=True)
            bot.send_message(chat_id=update.message.chat_id,
                             text=text,
                             reply_markup=reply_markup)
            return 'change settings organizations delete'
        else:
            text = "Хей, ты больше ни на что не подписан. Может стоит на что-то подписаться?"
            keyboard = [["Добавить", 'Всё ок!']]
            reply_markup = ReplyKeyboardMarkup(keyboard,
                                               one_time_keyboard=True,
                                               resize_keyboard=True)
            bot.send_message(chat_id=update.message.chat_id,
                             text=text,
                             reply_markup=reply_markup)
            return 'change settings'
    elif update.message.text == 'Всё ок!':
        subscriptions_themes = ''
        subscriptions_organization = ''
        count_themes_non_subscribed = 0
        count_themes_subscribed = 0
        count_organizations_non_subscribed = 0
        count_organizations_subscribed = 0

        # getting event themes followed
        count = 0
        for theme in cursor_settings['event_theme']:
            if cursor_settings['event_theme'][theme]['status'] == True:
                count_themes_subscribed += 1
                if count == 0:
                    subscriptions_themes += cursor_settings['event_theme'][
                        theme]['RU'].lower()
                    count += 1
                else:
                    subscriptions_themes += (
                        ', ' +
                        cursor_settings['event_theme'][theme]['RU'].lower())
            else:
                count_themes_non_subscribed += 1
        #getting organizations followed
        count = 0
        for organization in cursor_settings['organization']:
            if cursor_settings['organization'][organization]['status'] == True:
                count_organizations_subscribed += 1
                if count == 0:
                    subscriptions_organization += cursor_settings[
                        'organization'][organization]['name']
                    count += 1
                else:
                    subscriptions_organization += cursor_settings[
                        'organization'][organization]['name']
            else:
                count_organizations_non_subscribed += 1
        # getting location name
        if cursor_settings['location'] == 'Москва':
            location = 'Москве'
        elif cursor_settings['location'] == 'Санкт-Петербург':
            location = 'Питере'
        # figuring out the text to send
        if count_themes_non_subscribed != 0:
            text = "Сейчас ты подписан на эти категории событий в " + location + ": " + subscriptions_themes + '.\n'
        elif count_themes_subscribed == 0:
            text = "Сейчас ты не подписан ни на одну из категорий событий  в " + location + ".\n"
        else:
            text = "Сейчас ты подписан на все категории событий в " + location + ": " + subscriptions_themes + '.\n'
        if count_organizations_non_subscribed == 0:
            text += 'Также ты подписан на все организации в ' + location + ': ' + subscriptions_organization + '.\n'
        elif count_organizations_subscribed == 0:
            text += 'Также ты не подписан ни на одну из организаций в ' + location + '.\n'
        else:
            text += 'Также ты подписан на эти организации в ' + location + ': ' + subscriptions_organization + '.\n'
        text += 'Что сделать?:)'
        keyboard = [['Изменить тематики'], ['Изменить организации'],
                    ['Изменить регион'], ['Изменить тип персонализации'],
                    ['Всё ок!']]
        reply_markup = ReplyKeyboardMarkup(keyboard,
                                           one_time_keyboard=True,
                                           resize_keyboard=True)
        bot.send_message(chat_id=update.message.chat_id,
                         text=text,
                         reply_markup=reply_markup)
        return 'change settings'