Exemplo n.º 1
0
async def help(message: types.Message, state: FSMContext):
    await state.finish()
    msg = config.RESP['help']
    stats = await db.get_stats()
    for k, v in stats.items():
        msg = msg.replace('{' + k + '}', h(str(v)))
    await message.answer(msg, parse_mode=HTML, disable_web_page_preview=True,
                         reply_markup=get_buttons())
Exemplo n.º 2
0
async def stop_review(query: types.CallbackQuery):
    info = await get_user(query.from_user)
    info.review = None
    info.review_ctx = None
    await delete_msg(bot, query)
    await bot.send_message(query.from_user.id,
                           config.MSG['review']['stopped'],
                           reply_markup=get_buttons())
Exemplo n.º 3
0
async def set_loc(message):
    location = Location(message.location.longitude, message.location.latitude)
    info = await get_user(message.from_user)
    info.location = location
    if info.is_moderator():
        # Suggest review mode
        kbd = types.InlineKeyboardMarkup().add(
            types.InlineKeyboardButton(tr(('review', 'start')),
                                       callback_data='start_review'))
    else:
        kbd = get_buttons()
    await message.answer(tr('location'), reply_markup=kbd)
Exemplo n.º 4
0
async def new_cancel(query: types.CallbackQuery, state: FSMContext):
    await delete_msg(query, state)
    await state.finish()
    user = await get_user(query.from_user)
    if user.review:
        kbd = types.InlineKeyboardMarkup().add(
            types.InlineKeyboardButton('🗒️ ' + tr(('review', 'continue')),
                                       callback_data='continue_review'))
    else:
        kbd = get_buttons()
    await bot.send_message(query.from_user.id,
                           tr(('new_poi', 'cancel')),
                           reply_markup=kbd)
Exemplo n.º 5
0
async def welcome(message: types.Message, state: FSMContext):
    await state.finish()
    await message.answer(config.RESP['start'], reply_markup=get_buttons())
    payload = message.get_args()
    if payload:
        try:
            poi = await db.get_poi_by_id(int(payload))
            await PoiState.poi.set()
            await state.set_data({'poi': poi.id})
            await print_poi(message.from_user, poi)
        except ValueError:
            tokens = split_tokens(payload)
            if tokens:
                await process_query(message, state, tokens)
Exemplo n.º 6
0
async def undelete_poi(message: types.Message, state: FSMContext):
    user = await get_user(message.from_user)
    if not user.is_moderator():
        await message.answer(tr(('editor', 'cant_restore')))
        return
    poi = (await state.get_data())['poi']
    await db.restore_poi(message.from_user.id, poi)
    await state.finish()
    if user.review:
        kbd = types.InlineKeyboardMarkup().add(
            types.InlineKeyboardButton('🗒️ ' + tr(('review', 'continue')),
                                       callback_data='continue_review'))
    else:
        kbd = get_buttons()
    await message.answer(tr(('editor', 'restored')), reply_markup=kbd)
Exemplo n.º 7
0
async def delete_poi_prompt(message: types.Message, state: FSMContext):
    poi = (await state.get_data())['poi']
    if poi.delete_reason:
        user = await get_user(message.from_user)
        if not user.is_moderator():
            await message.answer(tr(('editor', 'delete_twice')))
        else:
            await db.delete_poi_forever(user.id, poi)
            await state.finish()
            await message.answer(tr(('editor', 'deleted2')),
                                 reply_markup=get_buttons())
    else:
        await message.answer(tr(('editor', 'delete')),
                             reply_markup=cancel_attr_kbd())
        await EditState.attr.set()
        await state.update_data(attr='delete')
Exemplo n.º 8
0
async def test_predefined(message, tokens) -> bool:
    all_tokens = ' '.join(tokens)
    query = message.text.lower().strip()
    for resp in config.RESP['responses']:
        kw = [k.lower() for k in resp['keywords']]
        if has_keyword(all_tokens, kw) or has_keyword(query, kw):
            if 'role' in resp:
                user = await get_user(message.from_user)
                if resp['role'] not in user.roles:
                    continue
            content = resp.get('name', '')
            photo = None
            if 'photo' in resp:
                photo_path = os.path.join(config.PHOTOS, resp['photo'])
                if os.path.exists(photo_path):
                    file_ids = await db.find_file_ids(
                        {resp['photo']: os.path.getsize(photo_path)})
                    if file_ids:
                        photo = file_ids[resp['photo']]
                    else:
                        photo = types.InputFile(photo_path)
            if 'message' in resp:
                if content:
                    content += '\n\n'
                content += resp['message']
            kbd = get_buttons(resp.get('buttons'))

            if photo:
                msg = await message.answer_photo(photo,
                                                 caption=content,
                                                 parse_mode=HTML,
                                                 reply_markup=kbd)
                if not isinstance(photo, str):
                    file_id = msg.photo[0].file_id
                    await db.store_file_id(resp['photo'],
                                           os.path.getsize(photo_path),
                                           file_id)
            else:
                await message.answer(content,
                                     parse_mode=HTML,
                                     reply_markup=kbd)
            return True
    return False
Exemplo n.º 9
0
async def store_attr(message: types.Message, state: FSMContext):
    data = await state.get_data()
    poi = data['poi']
    attr = data['attr']
    value = message.text.strip()

    if attr == 'name':
        if value == '-':
            await message.answer(tr(('editor', 'empty_name')),
                                 reply_markup=cancel_attr_kbd())
            return
        poi.name = value
    elif attr == 'desc':
        poi.description = None if value == '-' else value
    elif attr == 'comment':
        poi.comment = None if value == '-' else value
    elif attr == 'floor':
        poi.floor = None if value == '-' else value
    elif attr == 'tag':
        if value == '-':
            poi.tag = None
        else:
            parts = [
                p.strip() for p in re.split(r'[ =]+',
                                            value.lower().replace('-', '_'))
            ]
            if len(parts) != 2 or not re.match(r'^[a-z]+$', parts[0]):
                await message.answer(tr(('editor', 'tag_format'), value),
                                     reply_markup=cancel_attr_kbd())
                return
            poi.tag = '='.join(parts)
    elif attr == 'keywords':
        new_kw = split_tokens(value)
        if new_kw:
            old_kw = [] if not poi.keywords else poi.keywords.split()
            poi.keywords = ' '.join(old_kw + new_kw)
    elif attr == 'address':
        poi.address_part = None if value == '-' else value
    elif attr == 'location':
        loc = parse_location(message)
        if not loc:
            await message.answer(tr(('new_poi', 'no_location')),
                                 reply_markup=edit_loc_kbd(poi))
            return
        if not valid_location(loc):
            await message.answer(tr(('new_poi', 'location_out')),
                                 reply_markup=edit_loc_kbd(poi))
            return
        poi.location = loc
    elif attr == 'hours':
        if value == '-':
            poi.hours = None
            poi.hours_src = None
        else:
            try:
                hours = parse_hours(value)
            except ValueError as e:
                await message.answer(tr(('editor', 'hours_format'), e),
                                     reply_markup=cancel_attr_kbd())
                return
            poi.hours_src = hours
            poi.hours = hoh.OHParser(hours)
    elif attr == 'phones':
        if not value or value == '-':
            poi.phones = []
        else:
            poi.phones = [p.strip() for p in re.split(r'[;,]', value)]
    elif attr == 'links':
        if value:
            parts = parse_link(value)
            if parts:
                if len(parts) == 1:
                    poi.links = [l for l in poi.links if l[0] != parts[0]]
                else:
                    found = False
                    for i, l in enumerate(poi.links):
                        if l[0] == parts[0]:
                            found = True
                            l[1] = parts[1]
                    if not found:
                        poi.links.append(parts)
    elif attr == 'delete':
        await db.delete_poi(message.from_user.id, poi, value)
        await state.finish()
        user = await get_user(message.from_user)
        if user.review:
            kbd = types.InlineKeyboardMarkup().add(
                types.InlineKeyboardButton('🗒️ ' + tr(('review', 'continue')),
                                           callback_data='continue_review'))
        else:
            kbd = get_buttons()
        await message.answer(tr(('editor', 'deleted')), reply_markup=kbd)
        await broadcast_str(
            tr(('editor', 'just_deleted'), id=poi.id, reason=value),
            message.from_user.id)
        return
    else:
        await message.answer(tr(('editor', 'wrong_attr'), attr))

    await delete_msg(message, state)
    await state.set_data({'poi': poi})
    await EditState.confirm.set()
    await print_edit_options(message.from_user, state)