Skip to content

Commit

Permalink
add new message sending method
Browse files Browse the repository at this point in the history
  • Loading branch information
Sidnev Nikolaj committed Mar 26, 2019
1 parent eb6ec56 commit ba189e8
Show file tree
Hide file tree
Showing 13 changed files with 170 additions and 35 deletions.
20 changes: 20 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions .gitlab-cy.yml
Original file line number Diff line number Diff line change
@@ -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} .
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# pyBotX
Version: 1.0.0a7
Version: 1.0.0a9
###### tags: `documentation`, `library`, `botx`, `sdk`, `python`, `bots`

## Содержание
Expand Down
24 changes: 24 additions & 0 deletions botx/bot/asyncbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
File,
KeyboardElement,
Mention,
Message,
ResponseCommand,
ResponseFile,
ResponseNotification,
Expand Down Expand Up @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions botx/bot/basebot.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
BubbleElement,
KeyboardElement,
Mention,
Message,
ResponseRecipientsEnum,
Status,
SyncID,
Expand Down Expand Up @@ -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,
Expand Down
24 changes: 24 additions & 0 deletions botx/bot/syncbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
File,
KeyboardElement,
Mention,
Message,
ResponseCommand,
ResponseFile,
ResponseNotification,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <mmorozov@ccsteam.ru>",
Expand Down
3 changes: 0 additions & 3 deletions pytest.ini

This file was deleted.

8 changes: 7 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
...

Expand Down Expand Up @@ -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):
...

Expand Down
57 changes: 34 additions & 23 deletions tests/test_bot/test_asyncbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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(
(
Expand Down Expand Up @@ -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(
(
Expand Down Expand Up @@ -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()
Expand All @@ -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

Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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()
10 changes: 4 additions & 6 deletions tests/test_bot/test_basebot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down Expand Up @@ -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"
Expand All @@ -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
Loading

0 comments on commit ba189e8

Please sign in to comment.