async def wait_quote( cls, message_id: MessageIdType = ..., bot: BotSession = ..., graph: Graph = std.true, *, timeout: float = 3600, suspend_other: bool = False, suspend_next_priority: bool = False, ): message_id = this.event.message_id if message_id is ... else message_id bot = this.bot if bot is ... else bot return await cls.wait_until( router.message.has(Quote) & std.if_(lambda event, source: event.message.get_first(Quote).id == message_id and source == bot) & graph, timeout=timeout, suspend_other=suspend_other, suspend_next_priority=suspend_next_priority)
async def wait_until( cls, graph: Graph, *, timeout: float = 3600, suspend_other: bool = False, suspend_next_priority: bool = False, ): task = Task.current() task.state[_SUSPEND_OTHER_KEY] = suspend_other task.state[_SUSPEND_NEXT_PRIORITY_KEY] = suspend_next_priority async def _check_timeout(): cur_time = time.time() if cur_time - task.last_active_time > timeout: task.raise_(TimeoutError()) app.engine.unsubscribe_terminals([_dumpy_node]) return False return True async def _add_candidate(_state: RouteState, _store: KeyStore): if _CANDIDATES_KEY not in _store: _store[_CANDIDATES_KEY] = [] _store[_CANDIDATES_KEY].append( (task, _dumpy_node, (_state, _state.build()))) @app.on(std.if_(_check_timeout) & graph & std.process(_add_candidate)) @std.handler(priority=Priority.Never) async def _dumpy_node(): pass async def _set_timeout(): await asyncio.sleep(timeout) await app.handle_event(_scheduler_source, SchedulerEvent()) asyncio.create_task(_set_timeout()) await task.pause()
def pmatch(pattern, flags: pregex.RegexFlag = 0, key='match'): return std.if_(KeyFunctionImpl( lambda event: pregex.match(pattern, event.message.encode(), flags=flags, default_require_post=(MessageElement.decode, None)), key=key, ))
def fullmatch(pattern: Pattern, flags=0, key='match'): return std.if_(KeyFunctionImpl( lambda event: re.fullmatch(pattern, event.message.as_plain(), flags), key=key, ))