#1277 Add custom types for datetime in Telegram API

This commit adds a new file `custom_types.py` in the aiogram directory.
The inclusion of `DateTime` type ensures better compatibility with the Telegram Bot API. `DateTime` type is further enhanced to work with unixtime.
This commit is contained in:
Alex Root Junior 2023-08-21 02:22:39 +03:00
parent b7be9c2b81
commit df77ee2436
No known key found for this signature in database
GPG key ID: 074C1D455EBEA4AC

17
aiogram/custom_types.py Normal file
View file

@ -0,0 +1,17 @@
import datetime
from typing import Annotated
from pydantic import PlainSerializer
# Make datetime compatible with Telegram Bot API (unixtime)
DateTime = Annotated[
datetime,
PlainSerializer(
func=lambda dt: int(dt.timestamp()),
return_type=int,
when_used="json-unless-none",
),
]
# Make string compatible with custom lazy proxy
# String = Union[str, LazyProxy]