From ba189e8bb0c88cfb7d0097b4deb9046fe5c0c3bd Mon Sep 17 00:00:00 2001 From: Sidnev Nikolaj Date: Tue, 26 Mar 2019 13:26:17 +0300 Subject: [PATCH] add new message sending method --- .dockerignore | 20 ++++++++++++ .gitlab-cy.yml | 14 ++++++++ Dockerfile | 16 +++++++++ README.md | 2 +- botx/bot/asyncbot.py | 24 ++++++++++++++ botx/bot/basebot.py | 15 +++++++++ botx/bot/syncbot.py | 24 ++++++++++++++ pyproject.toml | 2 +- pytest.ini | 3 -- tests/conftest.py | 8 ++++- tests/test_bot/test_asyncbot.py | 57 ++++++++++++++++++++------------- tests/test_bot/test_basebot.py | 10 +++--- tests/test_bot/test_syncbot.py | 10 ++++++ 13 files changed, 170 insertions(+), 35 deletions(-) create mode 100644 .dockerignore create mode 100644 .gitlab-cy.yml create mode 100644 Dockerfile delete mode 100644 pytest.ini diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..8ef7e68d --- /dev/null +++ b/.dockerignore @@ -0,0 +1,20 @@ +.dockerignore +Dockerfile +db.sqlite3 +**/__pycache__ +**/*.pyc +**/*.pyo +**/*.pyd +**/.Python +env +pip-log.txt +pip-delete-this-directory.txt +.tox +.coverage +.coverage.* +.cache +coverage.xml +*,cover +*.log +.git +dist \ No newline at end of file diff --git a/.gitlab-cy.yml b/.gitlab-cy.yml new file mode 100644 index 00000000..9a161a86 --- /dev/null +++ b/.gitlab-cy.yml @@ -0,0 +1,14 @@ +image: docker:latest + +before_script: + - docker info + +stages: + - build + +build: + stage: build + tags: + - docker + script: + - docker build --force-rm -t $CI_REGISTRY_IMAGE:${CI_COMMIT_TAG:-$CI_COMMIT_REF_SLUG} . \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..98058de8 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,16 @@ +FROM python:3.7 + +ENV PYTHONUNBUFFERED 1 + +WORKDIR /botx_sdk_python + +COPY poetry.lock . +COPY pyproject.toml . + +RUN pip install poetry && \ + poetry config settings.virtualenvs.create false && \ + poetry install + +COPY . . + +RUN pytest diff --git a/README.md b/README.md index 06204f5a..41d9cfc4 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # pyBotX -Version: 1.0.0a7 +Version: 1.0.0a9 ###### tags: `documentation`, `library`, `botx`, `sdk`, `python`, `bots` ## Содержание diff --git a/botx/bot/asyncbot.py b/botx/bot/asyncbot.py index 2a525c34..63f538aa 100644 --- a/botx/bot/asyncbot.py +++ b/botx/bot/asyncbot.py @@ -13,6 +13,7 @@ File, KeyboardElement, Mention, + Message, ResponseCommand, ResponseFile, ResponseNotification, @@ -144,6 +145,29 @@ async def send_message( keyboard=keyboard, ) + async def answer_message( + self, + text: str, + message: Message, + *, + file: Optional[Union[TextIO, BinaryIO]] = None, + recipients: Union[List[UUID], str] = ResponseRecipientsEnum.all, + mentions: Optional[List[Mention]] = None, + bubble: Optional[List[List[BubbleElement]]] = None, + keyboard: Optional[List[List[KeyboardElement]]] = None, + ) -> Tuple[str, int]: + return await self.send_message( + text, + message.sync_id, + message.bot_id, + message.host, + file=file, + recipients=recipients, + mentions=mentions, + bubble=bubble, + keyboard=keyboard, + ) + async def _send_command_result( self, text: str, diff --git a/botx/bot/basebot.py b/botx/bot/basebot.py index a3a14aea..d196ab31 100644 --- a/botx/bot/basebot.py +++ b/botx/bot/basebot.py @@ -10,6 +10,7 @@ BubbleElement, KeyboardElement, Mention, + Message, ResponseRecipientsEnum, Status, SyncID, @@ -108,6 +109,20 @@ def send_message( ) -> Tuple[str, int]: """Create answer for notification or for command and send it to BotX API""" + @abc.abstractmethod + def answer_message( + self, + text: str, + message: Message, + *, + file: Optional[Union[TextIO, BinaryIO]] = None, + recipients: Union[List[UUID], str] = ResponseRecipientsEnum.all, + mentions: Optional[List[Mention]] = None, + bubble: Optional[List[List[BubbleElement]]] = None, + keyboard: Optional[List[List[KeyboardElement]]] = None, + ): + """Send message with credentials from incoming message""" + @abc.abstractmethod def _send_command_result( self, diff --git a/botx/bot/syncbot.py b/botx/bot/syncbot.py index 3a3413ee..0cc889b7 100644 --- a/botx/bot/syncbot.py +++ b/botx/bot/syncbot.py @@ -14,6 +14,7 @@ File, KeyboardElement, Mention, + Message, ResponseCommand, ResponseFile, ResponseNotification, @@ -141,6 +142,29 @@ def send_message( keyboard=keyboard, ) + def answer_message( + self, + text: str, + message: Message, + *, + file: Optional[Union[TextIO, BinaryIO]] = None, + recipients: Union[List[UUID], str] = ResponseRecipientsEnum.all, + mentions: Optional[List[Mention]] = None, + bubble: Optional[List[List[BubbleElement]]] = None, + keyboard: Optional[List[List[KeyboardElement]]] = None, + ) -> Tuple[str, int]: + return self.send_message( + text, + message.sync_id, + message.bot_id, + message.host, + file=file, + recipients=recipients, + mentions=mentions, + bubble=bubble, + keyboard=keyboard, + ) + def _send_command_result( self, text: str, diff --git a/pyproject.toml b/pyproject.toml index d4bc2cb2..a59ba6c0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "botx" -version = "1.0.0a8" +version = "1.0.0a9" description = "Python implementation for Express BotX API" authors = [ "Michael Morozov ", diff --git a/pytest.ini b/pytest.ini deleted file mode 100644 index 590c0513..00000000 --- a/pytest.ini +++ /dev/null @@ -1,3 +0,0 @@ -[pytest] -log_cli = 1 -log_cli_level = DEBUG \ No newline at end of file diff --git a/tests/conftest.py b/tests/conftest.py index a18eff32..8230283a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -245,7 +245,7 @@ def shutdown(self) -> NoReturn: ... def parse_request( - self, data: Dict[str, Any], request_type: Union[str, RequestTypeEnum] + self, data: Dict[str, Any], request_type: Union[str, RequestTypeEnum] ) -> Union[Status, bool]: ... @@ -337,6 +337,12 @@ def _obtain_token(self, **kwargs): def send_message(self, **kwargs): ... + def answer_message( + self, + **kwargs, + ): + ... + def _send_command_result(self, **kwargs): ... diff --git a/tests/test_bot/test_asyncbot.py b/tests/test_bot/test_asyncbot.py index 2862f425..9b0e067e 100644 --- a/tests/test_bot/test_asyncbot.py +++ b/tests/test_bot/test_asyncbot.py @@ -25,7 +25,7 @@ async def test_async_bot_init(hostname): @pytest.mark.asyncio async def test_async_bot_status_parsing( - custom_router, custom_async_handler, custom_default_async_handler + custom_router, custom_async_handler, custom_default_async_handler ): custom_router.add_handler(custom_async_handler) custom_router.add_handler(custom_default_async_handler) @@ -44,7 +44,7 @@ async def test_async_bot_status_parsing( @pytest.mark.asyncio async def test_async_bot_command_executing( - command_with_text_and_file, custom_async_handler_with_sync_command_body + command_with_text_and_file, custom_async_handler_with_sync_command_body ): bot = AsyncBot() await bot.start() @@ -76,7 +76,7 @@ async def test_async_bot_token_obtaining(hostname, bot_id, async_requests): @pytest.mark.asyncio async def test_async_bot_token_obtaining_with_errored_request( - hostname, bot_id, async_error_requests + hostname, bot_id, async_error_requests ): bot = AsyncBot() await bot.start() @@ -92,20 +92,20 @@ async def test_async_bot_token_obtaining_with_errored_request( @pytest.mark.asyncio async def test_async_bot_message_as_command_sending( - hostname, bot_id, async_requests, command_with_text_and_file + hostname, bot_id, async_requests, command_with_text_and_file ): command_array = [] notification_array = [] async def custom_command_sending( - text, chat_id, bot_id, host, file, recipients, mentions, bubble, keyboard + text, chat_id, bot_id, host, file, recipients, mentions, bubble, keyboard ): command_array.append( (text, chat_id, bot_id, host, file, recipients, mentions, bubble, keyboard) ) async def custom_notification_sending( - text, group_chat_ids, bot_id, host, file, recipients, mentions, bubble, keyboard + text, group_chat_ids, bot_id, host, file, recipients, mentions, bubble, keyboard ): notification_array.append( ( @@ -151,20 +151,20 @@ async def custom_notification_sending( @pytest.mark.asyncio async def test_async_bot_message_as_notification_sending( - hostname, bot_id, async_requests, command_with_text_and_file + hostname, bot_id, async_requests, command_with_text_and_file ): command_array = [] notification_array = [] async def custom_command_sending( - text, chat_id, bot_id, host, file, recipients, mentions, bubble, keyboard + text, chat_id, bot_id, host, file, recipients, mentions, bubble, keyboard ): command_array.append( (text, chat_id, bot_id, host, file, recipients, mentions, bubble, keyboard) ) async def custom_notification_sending( - text, group_chat_ids, bot_id, host, file, recipients, mentions, bubble, keyboard + text, group_chat_ids, bot_id, host, file, recipients, mentions, bubble, keyboard ): notification_array.append( ( @@ -230,7 +230,7 @@ async def custom_notification_sending( @pytest.mark.asyncio async def test_async_bot_requests( - command_with_text_and_file, hostname, bot_id, async_requests + command_with_text_and_file, hostname, bot_id, async_requests ): bot = AsyncBot() await bot.start() @@ -239,20 +239,20 @@ async def test_async_bot_requests( m = Message(**command_with_text_and_file) assert ( - len( - await bot._send_command_result( - m.body, m.sync_id, m.bot_id, m.host, m.file, "all", [], [], [] + len( + await bot._send_command_result( + m.body, m.sync_id, m.bot_id, m.host, m.file, "all", [], [], [] + ) ) - ) - == 2 + == 2 ) assert ( - len( - await bot._send_notification_result( - m.body, [m.group_chat_id], m.bot_id, m.host, m.file, "all", [], [], [] + len( + await bot._send_notification_result( + m.body, [m.group_chat_id], m.bot_id, m.host, m.file, "all", [], [], [] + ) ) - ) - == 2 + == 2 ) assert len(await bot.send_file(m.file.file, m.sync_id, m.bot_id, m.host)) == 2 @@ -261,7 +261,7 @@ async def test_async_bot_requests( @pytest.mark.asyncio async def test_async_bot_message_sending_error_requests( - command_with_text_and_file, hostname, bot_id, async_error_requests + command_with_text_and_file, hostname, bot_id, async_error_requests ): bot = AsyncBot() await bot.start() @@ -276,7 +276,7 @@ async def test_async_bot_message_sending_error_requests( @pytest.mark.asyncio async def test_async_bot_file_sending_error_requests( - command_with_text_and_file, hostname, bot_id, async_error_requests + command_with_text_and_file, hostname, bot_id, async_error_requests ): bot = AsyncBot() await bot.start() @@ -291,7 +291,7 @@ async def test_async_bot_file_sending_error_requests( @pytest.mark.asyncio async def test_sync_bot_work_with_disabled_credentials( - async_requests, command_with_text_and_file + async_requests, command_with_text_and_file ): bot = AsyncBot(disable_credentials=True) await bot.start() @@ -319,3 +319,14 @@ async def cmd(m, b): await bot.parse_command(command_with_text_and_file) await bot.stop() + + +@pytest.mark.asyncio +async def test_async_answer_message(command_with_text_and_file, async_requests): + bot = AsyncBot(disable_credentials=True) + await bot.start() + + message = Message(**command_with_text_and_file) + await bot.answer_message(message.body, message) + + await bot.stop() diff --git a/tests/test_bot/test_basebot.py b/tests/test_bot/test_basebot.py index cfd99b86..6e0c30ec 100644 --- a/tests/test_bot/test_basebot.py +++ b/tests/test_bot/test_basebot.py @@ -23,7 +23,7 @@ def test_cts_registration(custom_base_bot_class, hostname): def test_bot_router_nesting( - custom_base_bot_class, custom_router, custom_handler, custom_default_handler + custom_base_bot_class, custom_router, custom_handler, custom_default_handler ): custom_router.add_handler(custom_default_handler) custom_router.add_handler(custom_handler) @@ -36,7 +36,7 @@ def test_bot_router_nesting( def test_bot_adding_commands_behaviour( - custom_base_bot_class, custom_router, custom_handler + custom_base_bot_class, custom_router, custom_handler ): custom_router.add_handler(custom_handler) @@ -91,8 +91,8 @@ def test_bot_credentials_update(custom_base_bot_class, bot_id, hostname, secret) ) assert ( - bot.get_cts_credentials().known_cts[hostname][1].result - == "result_token_for_operations_replaced" + bot.get_cts_credentials().known_cts[hostname][1].result + == "result_token_for_operations_replaced" ) second_host = hostname + "2" @@ -109,6 +109,4 @@ def test_bot_credentials_update(custom_base_bot_class, bot_id, hostname, secret) ) ) - print(bot.get_cts_credentials()) - assert len(bot.get_cts_credentials().known_cts) == 2 diff --git a/tests/test_bot/test_syncbot.py b/tests/test_bot/test_syncbot.py index 5bee0435..085cf6d7 100644 --- a/tests/test_bot/test_syncbot.py +++ b/tests/test_bot/test_syncbot.py @@ -296,3 +296,13 @@ def cmd(m, b): bot.parse_command(command_with_text_and_file) bot.stop() + + +def test_sync_answer_message(command_with_text_and_file, sync_requests): + bot = Bot(disable_credentials=True) + bot.start() + + message = Message(**command_with_text_and_file) + bot.answer_message(message.body, message) + + bot.stop()