Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Change attachment voice extension #446

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion pybotx/models/attachments.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,13 @@ def convert_api_attachment_to_domain( # noqa: WPS212
attachment_type = cast(Literal[AttachmentTypes.VOICE], attachment_type)
api_attachment = cast(BotAPIAttachmentVoice, api_attachment)
content = decode_rfc2397(api_attachment.data.content)
attachment_extension = get_attachment_extension_from_encoded_content(
api_attachment.data.content,
)

return AttachmentVoice(
type=attachment_type,
filename="record.mp3",
filename=f"record.{attachment_extension}",
size=len(content),
is_async_file=False,
content=content,
Expand Down Expand Up @@ -317,6 +320,12 @@ def convert_api_attachment_to_domain( # noqa: WPS212
raise NotImplementedError(f"Unsupported attachment type: {attachment_type}")


def get_attachment_extension_from_encoded_content(
encoded_content: str,
) -> str:
return encoded_content.split(";")[0].split("/")[1]


def decode_rfc2397(encoded_content: str) -> bytes:
# "data:image/gif;base64,aGVsbG8=" -> b"hello"
if not encoded_content:
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 = "pybotx"
version = "0.62.0"
version = "0.62.1"
description = "A python library for interacting with eXpress BotX API"
authors = [
"Sidnev Nikolay <nsidnev@ccsteam.ru>",
Expand Down
19 changes: 18 additions & 1 deletion tests/test_attachments.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ async def default_handler(message: IncomingMessage, bot: Bot) -> None:
(
{
"data": {
"content": "data:audio/mpeg3;base64,SGVsbG8sIHdvcmxkIQo=",
"content": "data:audio/mp3;base64,SGVsbG8sIHdvcmxkIQo=",
"duration": 10,
},
"type": "voice",
Expand All @@ -250,6 +250,23 @@ async def default_handler(message: IncomingMessage, bot: Bot) -> None:
duration=10,
),
),
(
{
"data": {
"content": "data:audio/m4a;base64,SGVsbG8sIHdvcmxkIQo=",
"duration": 10,
},
"type": "voice",
},
AttachmentVoice(
type=AttachmentTypes.VOICE,
filename="record.m4a",
size=len(b"Hello, world!\n"),
is_async_file=False,
content=b"Hello, world!\n",
duration=10,
),
),
)


Expand Down
Loading