async def process(message: types.Message, state: FSMContext): if message.from_user.is_bot: return if message.reply_to_message and message.reply_to_message.is_forward(): await process_reply(message) return for user_id in prune_users(message.from_user.id): await state.storage.finish(user=user_id) # We used to send a message here, but "disable_notification" only # disables a buzz, not an unread notification. tokens = split_tokens(message.text) if not tokens: write_search_log(message, None, 'empty') return # Reset state await state.finish() # First check for pre-defined replies if await test_predefined(message, tokens): write_search_log(message, tokens, 'predefined') return # Now check for streets if await test_address(message, tokens, state): write_search_log(message, tokens, 'address') return # Finally check keywords await process_query(message, state, tokens)
async def process_building(message: types.Message, state: FSMContext): tokens = split_tokens(message.text, False) if not tokens: return street_name = (await state.get_data())['street'] streets = [s for s in config.ADDR['streets'] if s['name'] == street_name] if streets: street = streets[0] hid = street['buildings'].get(tokens[0]) if hid: await handle_building(message.from_user, street, tokens, state) return # If we fail, process it as without context raise SkipHandler
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)
async def new_keywords(message: types.Message, state: FSMContext): keywords = split_tokens(message.text) if not keywords: await message.answer(tr(('new_poi', 'no_keywords'))) return # Create a POI data = await state.get_data() poi = POI(name=data['name'], location=Location(lat=data['lat'], lon=data['lon']), keywords=' '.join(keywords)) await state.set_data({'poi': poi}) await EditState.confirm.set() await print_edit_options(message.from_user, state, tr(('new_poi', 'confirm')))
async def all_pois(query: types.CallbackQuery, callback_data: Dict[str, str], state: FSMContext): cur_state = None if not state else await state.get_state() if cur_state == PoiState.poi_list.state: data = await state.get_data() txt = data['query'] pois = await db.get_poi_by_ids(data['poi']) else: txt = callback_data['query'] ids = callback_data['ids'] if len(ids) < 2: tokens = split_tokens(txt) pois = await db.find_poi(' '.join(tokens)) else: pois = await db.get_poi_by_ids(unpack_ids(ids)) await print_poi_list(query.from_user, txt, pois, True)
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)