def setUp(self): self.reserves = [] self.strings = RuReserve self.chat = Chat() self.chat.id = 101 self.user = User() self.user.first_name = "Firstname" self.user.id = 111 dp = Dispatcher() self.data_adapter = MemoryDataAdapter() self.state_manager = StateManager(self.data_adapter) self.processor = ReserveProcessor(dp, self.state_manager, self.strings) message = Message() message.chat = self.chat message.from_user = self.user message.message_id = 121 message.text = "Some text" message.answer = self.answer_mock message.delete = self.delete_mock message.edit_text = self.edit_text_mock self.test_message = message callback = CallbackQuery() callback.bot = Bot() callback.answer = self.callback_answer_mock callback.message = message callback.from_user = User() callback.from_user.id = 101 self.test_callback_query = callback
def bg( self, user_id: Optional[int] = None, chat_id: Optional[int] = None, stack_id: Optional[str] = None, ) -> "BaseDialogManager": if user_id is not None: user = User(id=user_id) else: user = self.user if chat_id is not None: chat = Chat(id=chat_id) else: chat = self.chat same_chat = (user.id == self.user.id and chat.id == self.chat.id) if stack_id is None: if same_chat: stack_id = self.stack_id intent_id = self.intent_id else: stack_id = DEFAULT_STACK_ID intent_id = None else: intent_id = None return BgManager( user=user, chat=chat, bot=self.bot, registry=self.registry, intent_id=intent_id, stack_id=stack_id, )
def bg( self, user_id: Optional[int] = None, chat_id: Optional[int] = None, stack_id: Optional[str] = None, ) -> "BaseDialogManager": if user_id is not None: user = User(id=user_id) else: user = self.event.from_user if chat_id is not None: chat = Chat(id=chat_id) else: chat = get_chat(self.event) same_chat = (user.id == self.event.from_user.id and chat.id == get_chat(self.event).id) intent_id = None if stack_id is None: if same_chat: stack_id = self.current_stack().id if self.current_context(): intent_id = self.current_context().id else: stack_id = DEFAULT_STACK_ID return BgManager( user=user, chat=chat, bot=self.event.bot, registry=self.registry, intent_id=intent_id, stack_id=stack_id, )
def bg(self, user_id: Optional[int] = None, chat_id: Optional[int] = None) -> BgManagerProto: if self.disabled: raise IncorrectBackgroundError("Please call `manager.bg()` " "before starting background task") self.check_disabled() if user_id is not None: user = User(id=user_id) else: user = self.event.from_user if chat_id is not None: chat = Chat(id=chat_id) else: chat = get_chat(self.event) return BgManager( user, chat, self.event.bot, self.registry, self.current_intent(), self.context.state, )
async def check(self, obj: Union[Message, CallbackQuery]): if getattr(obj, "reply_to_message"): logger.info(obj.reply_to_message.from_user.id) if Chat.get_current().id == User.get_current().id: return True return False
async def test_bot_status_awx(patched_bot, sub: Client, chat_id, target): await sub.subscribe(AWXApiSource.params().topic_in) await asyncio.sleep(.05) message_text = f'/status awx {target}' await patched_bot.handle_status( Message(text=message_text, chat=Chat(id=chat_id))) data = await sub.get(.1) assert data == f'{target};1'
def message(self, rand_user): c = TelegramChat() c.title = 'UnitTesting' u = TelegramUser() u.id = rand_user.id u.first_name = rand_user.first_name class _Message: def __init__(self, user, chat): self.from_user = user self.chat = chat @staticmethod async def reply(text): print(text) await asyncio.sleep(0) yield _Message(u, c)
def setUp(self): self.data_adapter = MemoryDataAdapter() self.state_provider.data_adapter = self.data_adapter self.chat = Chat() self.chat.id = 101 self.user = User() self.user.id = 111 state_data1 = {"state": "main", "state_type": "reserve"} state_data2 = {"state": "book", "state_type": "reserve"} self.data_adapter.append_data("101-111-121", state_data1) self.data_adapter.append_data("101-111-122", state_data2)
def bg(self, user_id: Optional[int] = None, chat_id: Optional[int] = None) -> BgManagerProto: if user_id is not None: user = User(id=user_id) else: user = self.user if chat_id is not None: chat = Chat(id=chat_id) else: chat = self.chat return BgManager( user, chat, self.bot, self.registry, self.current_intent(), self.current_state, )
def chat(self): return Chat.get_current()
async def _process_update(self, event: DialogUpdateEvent): Bot.set_current(event.bot) User.set_current(event.from_user) Chat.set_current(event.chat) await self.update_handler.notify(event)
def message_stub(message_text, chat_id): return Message(text=message_text, chat=Chat(id=chat_id))