Skip to content

Commit

Permalink
Fix/add attachments meta (#154)
Browse files Browse the repository at this point in the history
* chore: autoformatter changes

* fix: make the `body` attribute of `Reply` event optional

* fix: add attachments meta models to `Reply` event

* docs: add release changes
  • Loading branch information
rrrrs09 committed Jul 28, 2021
1 parent 97e0962 commit 71eedab
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 18 deletions.
87 changes: 87 additions & 0 deletions botx/models/attachments_meta.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
"""Module with attachments meta for botx."""

from typing import Optional, Union

from pydantic import Field

from botx.models.base import BotXBaseModel
from botx.models.enums import AttachmentsTypes

try:
from typing import Literal # noqa: WPS433
except ImportError:
from typing_extensions import Literal # type: ignore # noqa: WPS433, WPS440, F401


class FileAttachmentMeta(BotXBaseModel):
"""Common metadata of file."""

#: type of attachment
type: str

#: name of file.
file_name: str

#: mime type of file
file_mime_type: Optional[str]

#: file preview in RFC 2397 format.
file_preview_base64: Optional[str]


class ImageAttachmentMeta(FileAttachmentMeta):
"""BotX API image attachment meta container."""

#: type of attachment
type: Literal[AttachmentsTypes.image] = Field(default=AttachmentsTypes.image)


class VideoAttachmentMeta(FileAttachmentMeta):
"""BotX API video attachment meta container."""

#: type of attachment
type: Literal[AttachmentsTypes.video] = Field(default=AttachmentsTypes.video)


class DocumentAttachmentMeta(FileAttachmentMeta):
"""BotX API document attachment meta container."""

#: type of attachment
type: Literal[AttachmentsTypes.document] = Field(default=AttachmentsTypes.document)


class VoiceAttachmentMeta(FileAttachmentMeta):
"""BotX API voice attachment meta container."""

#: type of attachment
type: Literal[AttachmentsTypes.voice] = Field(default=AttachmentsTypes.voice)


class ContactAttachmentMeta(BotXBaseModel):
"""BotX API contact attachment meta container."""

#: type of attachment
type: Literal[AttachmentsTypes.contact] = Field(default=AttachmentsTypes.contact)

#: name of contact
contact_name: str


class LocationAttachmentMeta(BotXBaseModel):
"""BotX API location attachment meta container."""

#: type of attachment
type: Literal[AttachmentsTypes.location] = Field(default=AttachmentsTypes.location)

#: address of location
location_address: str


AttachmentMeta = Union[
ImageAttachmentMeta,
VideoAttachmentMeta,
DocumentAttachmentMeta,
VoiceAttachmentMeta,
ContactAttachmentMeta,
LocationAttachmentMeta,
]
13 changes: 8 additions & 5 deletions botx/models/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from typing import Dict, List, Optional, Union, cast
from uuid import UUID, uuid4

from pydantic import validator
from pydantic import Field, validator

from botx.models.attachments import Attachments
from botx.models.attachments_meta import AttachmentMeta
from botx.models.base import BotXBaseModel
from botx.models.enums import ChatTypes, EntityTypes, MentionTypes

Expand Down Expand Up @@ -207,11 +207,11 @@ def to_botx_format(self) -> str:
class Reply(BotXBaseModel):
"""Message that was replied."""

#: array of attachments.
attachment: Optional[List[Attachments]] = []
#: attachment metadata.
attachment_meta: Optional[AttachmentMeta] = Field(alias="attachment")

#: text of source message.
body: str
body: Optional[str]

#: mentions of source message.
mentions: List[Mention] = []
Expand All @@ -231,6 +231,9 @@ class Reply(BotXBaseModel):
#: uuid of source message.
source_sync_id: UUID

class Config:
allow_population_by_field_name = True


class Entity(BotXBaseModel):
"""Additional entity that can be received by bot."""
Expand Down
13 changes: 2 additions & 11 deletions botx/models/messages/sending/message.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
"""Message that is sent from bot."""
import re
from typing import ( # noqa: WPS235
Any,
BinaryIO,
Dict,
List,
Optional,
TextIO,
Tuple,
Union,
cast,
)
from typing import Any # noqa: WPS235
from typing import BinaryIO, Dict, List, Optional, TextIO, Tuple, Union, cast
from uuid import UUID

from botx.models.buttons import ButtonOptions
Expand Down
3 changes: 2 additions & 1 deletion botx/testing/building/entites.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from datetime import datetime
from typing import Optional

from botx.models.attachments_meta import DocumentAttachmentMeta
from botx.models.entities import (
ChatMention,
Entity,
Expand Down Expand Up @@ -98,7 +99,7 @@ def reply(
mentions = message.entities.mentions

reply = Reply(
attachment=message.attachments.__root__, # type: ignore
attachment=DocumentAttachmentMeta(file_name="test.doc"),
body=message.body,
mentions=mentions,
reply_type=ChatTypes(message.chat_type),
Expand Down
8 changes: 8 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 0.21.1 (Jul 28, 2021)

### Fixed

* Make the `body` attribute of `Reply` event optional.
* Add `AttachmentMeta` model to `Reply` event instead of `Attachments`.


## 0.21.0 (Jul 23, 2021)

Tested on BotX 1.42.0-rc4
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 = "0.21.0"
version = "0.21.1"
description = "A little python framework for building bots for eXpress"
license = "MIT"
authors = [
Expand Down

1 comment on commit 71eedab

@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://6101246d342f24ad2388ac2c--pybotx.netlify.app

Please sign in to comment.