async def check(self, event: BaseEvent) -> FilterResult: if self.state == ANY_STATE: return FilterResult(True) if self.always_false: from_id = event.object.object.message.from_id peer_id = event.object.object.message.peer_id template = "__vkwave_{for_what}_{peer_id}_{from_id}__" is_pm = from_id == peer_id user_in_chat_state = template.format(for_what="userInChat", peer_id=peer_id, from_id=from_id) if is_pm: user_state = template.format(for_what="user", peer_id=from_id, from_id=from_id) else: user_state = template.format(for_what="chat", peer_id=peer_id, from_id=peer_id) return FilterResult(not ( await self.fsm.storage.contains(Key(user_in_chat_state)) or await self.fsm.storage.contains(Key(user_state)))) current_state = await self.fsm.get_data(event, for_what=self.for_what) return FilterResult( current_state is not None and current_state["__vkwave_fsm_state__"] == str(self.state))
async def ttl_test(storage: AbstractExpiredStorage): await storage.put(Key("my_key"), "my_value", TTL(1)) # one second ttl time.sleep(2) # sleep two second flag = False try: await storage.get(Key("my_key")) # key must not exist except KeyError: flag = True assert flag
async def add_data(self, event: BaseEvent, for_what: ForWhat, state_data: typing.Dict[str, str]) -> None: sid = Key(create_state_id(event, for_what)) data = await self.storage.get(sid) data.update(state_data) return await self.storage.put(sid, data)
async def day_schedule(event: SimpleBotEvent): # TODO: слать инлайн клаву снова schedule, days = await get_schedule_and_days() day_data: str = json.loads(event.object.object.message.payload)["day"] current_schedule = schedule[days.index(day_data)] response = create_text_schedule(current_schedule) kb = await storage.get(Key("current_kb")) await event.answer(f"{day_data}\n{response}", dont_parse_links=True, keyboard=kb.get_keyboard())
async def storage_interact_test( storage: Union[AbstractStorage, AbstractExpiredStorage], test_key=Key("test_key"), test_value="test_value", ): await storage.put(test_key, test_value) # add key r = await storage.get(test_key) assert r == test_value await storage.delete(test_key) assert not await storage.contains(test_key) flag = False try: await storage.get(test_key) # key must not exist except KeyError: flag = True assert flag
async def set_state( self, state: State, event: BaseEvent, for_what: ForWhat, extra_state_data: typing.Optional[typing.Dict[str, str]] = None, ) -> None: sid = Key(create_state_id(event, for_what)) if extra_state_data is None: extra_state_data = {} if await self.storage.contains(sid): current_data = await self.storage.get(sid) current_data["__vkwave_fsm_state__"] = str(state) current_data.update(extra_state_data) return await self.storage.put(sid, current_data) storage_data = {"__vkwave_fsm_state__": str(state)} storage_data.update(extra_state_data) return await self.storage.put(sid, storage_data)
async def finish(self, event: BaseEvent, for_what: ForWhat) -> None: sid = Key(create_state_id(event, for_what)) return await self.storage.delete(sid)
async def get_data(self, event: BaseEvent, for_what: ForWhat) -> typing.Optional[Value]: sid = Key(create_state_id(event, for_what)) return await self.storage.get(sid, default=None)
async def update_all() -> None: schedule, days = await get_week_schedule() await storage.put(key=Key("current_days"), value=days) await storage.put(key=Key("current_schedule"), value=schedule) await storage.put(key=Key("current_kb"), value=await create_current_kb(await get_days()))
async def get_days() -> DAYS: days = await storage.get(Key("current_days")) return days
async def get_schedule() -> WEEK_SCHEDULE: schedule = await storage.get(Key("current_schedule")) return schedule
async def send_schedule(event: SimpleBotEvent): kb = await storage.get(Key("current_kb")) await event.answer(keyboard=kb.get_keyboard(), message="Выберите день")