Skip to content

Commit

Permalink
feat: Add SmartApps main functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
lxmnk committed Nov 22, 2021
1 parent e234775 commit 05c7877
Show file tree
Hide file tree
Showing 28 changed files with 770 additions and 18 deletions.
3 changes: 3 additions & 0 deletions botx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
from botx.models.messages.sending.message import SendingMessage
from botx.models.messages.sending.options import MessageOptions, NotificationOptions
from botx.models.messages.sending.payload import MessagePayload, UpdatePayload
from botx.models.smartapps import SendingSmartAppEvent, SendingSmartAppNotification
from botx.models.status import StatusRecipient
from botx.models.stickers import (
Pagination,
Expand Down Expand Up @@ -146,6 +147,8 @@
"StickerPackList",
"StickerFromPack",
"StickerPackPreview",
"SendingSmartAppEvent",
"SendingSmartAppNotification",
# testing
"TestClient",
"MessageBuilder",
Expand Down
24 changes: 24 additions & 0 deletions botx/bots/mixins/collecting/system_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,27 @@ def cts_logout(
dependencies=dependencies,
dependency_overrides_provider=dependency_overrides_provider,
)

def smartapp_event(
self,
handler: Optional[Callable] = None,
*,
dependencies: Optional[Sequence[Depends]] = None,
dependency_overrides_provider: Any = None,
) -> Callable:
"""Register handler for `smartapp_event` event.
Arguments:
handler: callable that will be used for executing handler.
dependencies: sequence of dependencies that should be executed before
handler.
dependency_overrides_provider: mock of callable for handler.
Returns:
Passed in `handler` callable.
"""
return self.collector.smartapp_event(
handler=handler,
dependencies=dependencies,
dependency_overrides_provider=dependency_overrides_provider,
)
2 changes: 2 additions & 0 deletions botx/bots/mixins/requests/mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
files,
internal_bot_notification,
notification,
smartapps,
stickers,
users,
)
Expand Down Expand Up @@ -48,6 +49,7 @@ class BotXRequestsMixin( # noqa: WPS215
users.UsersRequestsMixin,
internal_bot_notification.InternalBotNotificationRequestsMixin,
files.FilesRequestsMixin,
smartapps.SmartAppMixin,
stickers.StickersMixin,
):
"""Mixin that defines methods for communicating with BotX API."""
Expand Down
62 changes: 62 additions & 0 deletions botx/bots/mixins/requests/smartapps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""Mixin for shortcut for smartapp."""

from uuid import UUID

from botx.bots.mixins.requests.call_protocol import BotXMethodCallProtocol
from botx.clients.methods.v3.smartapps.smartapp_event import SmartAppEvent
from botx.clients.methods.v3.smartapps.smartapp_notification import SmartAppNotification
from botx.models.messages.sending.credentials import SendingCredentials
from botx.models.smartapps import SendingSmartAppEvent, SendingSmartAppNotification


class SmartAppMixin:
"""Mixin for shortcut for smartapp methods."""

async def send_smartapp_event(
self: BotXMethodCallProtocol,
credentials: SendingCredentials,
smartapp_event: SendingSmartAppEvent,
) -> UUID:
"""Send smartapp event into chat.
Arguments:
credentials: credentials for making request.
smartapp_event: SmartpApp event.
Returns:
ID sent message.
"""
return await self.call_method(
SmartAppEvent(
ref=smartapp_event.ref,
smartapp_id=smartapp_event.smartapp_id,
data=smartapp_event.data,
opts=smartapp_event.opts,
smartapp_api_version=smartapp_event.smartapp_api_version,
group_chat_id=smartapp_event.group_chat_id,
files=smartapp_event.files,
async_files=smartapp_event.async_files,
),
credentials=credentials,
)

async def send_smartapp_notification(
self: BotXMethodCallProtocol,
credentials: SendingCredentials,
smartapp_notification: SendingSmartAppNotification,
) -> None:
"""Send smartapp notification into chat.
Arguments:
credentials: credentials for making request.
smartapp_notification: Smartapp notification.
"""
await self.call_method(
SmartAppNotification(
group_chat_id=smartapp_notification.group_chat_id,
smartapp_counter=smartapp_notification.smartapp_counter,
opts=smartapp_notification.opts,
smartapp_api_version=smartapp_notification.smartapp_api_version,
),
credentials=credentials,
)
1 change: 1 addition & 0 deletions botx/clients/methods/v3/smartapps/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Definition for smartapp methods."""
38 changes: 38 additions & 0 deletions botx/clients/methods/v3/smartapps/smartapp_event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""Method for sending smartapp event."""
from typing import Any, Dict, List, Optional
from uuid import UUID

from botx.clients.methods.base import AuthorizedBotXMethod
from botx.models.files import File, MetaFile


class SmartAppEvent(AuthorizedBotXMethod[str]):
"""Method for sending smartapp events."""

__url__ = "/api/v3/botx/smartapps/event"
__method__ = "POST"
__returning__ = str

#: unique request id
ref: Optional[UUID] = None

#: smartapp id
smartapp_id: UUID

#: event data
data: Dict[str, Any] # noqa: WPS110

#: event options
opts: Dict[str, Any] = {}

#: version of protocol smartapp <-> bot
smartapp_api_version: int

#: smartapp chat
group_chat_id: Optional[UUID]

#: files
files: List[File] = []

#: file's meta to upload
async_files: List[MetaFile] = []
25 changes: 25 additions & 0 deletions botx/clients/methods/v3/smartapps/smartapp_notification.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""Method for sending smartapp event."""
from typing import Any, Dict, Optional
from uuid import UUID

from botx.clients.methods.base import AuthorizedBotXMethod


class SmartAppNotification(AuthorizedBotXMethod[str]):
"""Method for sending smartapp notifications."""

__url__ = "/api/v3/botx/smartapps/notification"
__method__ = "POST"
__returning__ = str

#: smartapp chat
group_chat_id: Optional[UUID]

#: unread notifications count
smartapp_counter: int

#: event options
opts: Dict[str, Any] = {}

#: version of protocol smartapp <-> bot
smartapp_api_version: int
26 changes: 26 additions & 0 deletions botx/collecting/collectors/mixins/system_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,29 @@ def cts_logout(
dependencies=dependencies,
dependency_overrides_provider=dependency_overrides_provider,
)

def smartapp_event(
self,
handler: Optional[Callable] = None,
*,
dependencies: Optional[Sequence[Depends]] = None,
dependency_overrides_provider: Any = None,
) -> Callable:
"""Register handler for `smartapp_event` event.
Arguments:
handler: callable that will be used for executing handler.
dependencies: sequence of dependencies that should be executed before
handler.
dependency_overrides_provider: mock of callable for handler.
Returns:
Passed in `handler` callable.
"""
return self.system_event(
handler=handler,
event=SystemEvents.smartapp_event,
name=SystemEvents.smartapp_event.value,
dependencies=dependencies,
dependency_overrides_provider=dependency_overrides_provider,
)
3 changes: 3 additions & 0 deletions botx/models/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class SystemEvents(Enum):
#: `system:cts_logout` event.
cts_logout = "system:cts_logout"

#: `system:smartapp_event` event.
smartapp_event = "system:smartapp_event"

#: `file_transfer` message.
file_transfer = "file_transfer"

Expand Down
22 changes: 21 additions & 1 deletion botx/models/events.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Definition of different schemas for system events."""

from types import MappingProxyType
from typing import Any, Dict, List, Mapping, Type
from typing import Any, Dict, List, Mapping, Optional, Type
from uuid import UUID

from botx.clients.types.message_payload import InternalBotNotificationPayload
Expand Down Expand Up @@ -80,6 +80,25 @@ class CTSLogoutEvent(BotXBaseModel):
cts_id: UUID


class SmartAppEvent(BotXBaseModel):
"""Shape for `system:smartapp_event` event data."""

#: unique request id
ref: Optional[UUID] = None

#: smartapp id
smartapp_id: UUID

#: event data
data: Dict[str, Any] # noqa: WPS110

#: event options
opts: Dict[str, Any] = {}

#: version of protocol smartapp <-> bot
smartapp_api_version: int


# dict for validating shape for different events
EVENTS_SHAPE_MAP: Mapping[SystemEvents, Type[BotXBaseModel]] = MappingProxyType(
{
Expand All @@ -90,5 +109,6 @@ class CTSLogoutEvent(BotXBaseModel):
SystemEvents.internal_bot_notification: InternalBotNotificationEvent,
SystemEvents.cts_login: CTSLoginEvent,
SystemEvents.cts_logout: CTSLogoutEvent,
SystemEvents.smartapp_event: SmartAppEvent,
},
)
8 changes: 6 additions & 2 deletions botx/models/messages/incoming_message.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Definition of messages received by bot or sent by it."""

from typing import Any, Dict, Optional, Tuple, Union
from typing import Any, Dict, List, Optional, Tuple, Union
from uuid import UUID

from pydantic import BaseConfig, BaseModel, Field, validator
Expand All @@ -9,7 +9,7 @@
from botx.models.attachments import AttachList
from botx.models.entities import EntityList
from botx.models.enums import ChatTypes, ClientPlatformEnum, CommandTypes
from botx.models.files import File
from botx.models.files import File, MetaFile

CommandDataType = Union[
events.ChatCreatedEvent,
Expand All @@ -19,6 +19,7 @@
events.InternalBotNotificationEvent,
events.CTSLoginEvent,
events.CTSLogoutEvent,
events.SmartAppEvent,
Dict[str, Any],
]

Expand Down Expand Up @@ -161,6 +162,9 @@ class IncomingMessage(BaseModel):
#: file attached to message.
file: Optional[File] = None

#: meta info for downloading files
async_files: List[MetaFile] = Field(default_factory=list)

#: information about user from which message was received.
user: Sender = Field(..., alias="from")

Expand Down
7 changes: 5 additions & 2 deletions botx/models/messages/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from __future__ import annotations

from functools import partial
from typing import Any, Dict, Optional, Type
from typing import Any, Dict, List, Optional, Type
from uuid import UUID

from botx.bots import bots
Expand All @@ -11,7 +11,7 @@
from botx.models.datastructures import State
from botx.models.entities import EntityList
from botx.models.enums import CommandTypes
from botx.models.files import File
from botx.models.files import File, MetaFile
from botx.models.messages.incoming_message import Command, IncomingMessage, Sender
from botx.models.messages.sending.credentials import SendingCredentials

Expand Down Expand Up @@ -73,6 +73,9 @@ class Message:
#: file from message.
file: Optional[File] = _message_proxy_property()

#: Meta for download files.
async_files: List[MetaFile] = _message_proxy_property()

#: attachment from message v4+
attachments: AttachList = _message_proxy_property()

Expand Down
Loading

1 comment on commit 05c7877

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 Published on https://pybotx.netlify.app as production
🚀 Deployed on https://619ba64e9e51b20b52659d2e--pybotx.netlify.app

Please sign in to comment.