From df77ee2436a23a17149c531af727018f8e01a7a5 Mon Sep 17 00:00:00 2001 From: Alex Root Junior Date: Mon, 21 Aug 2023 02:22:39 +0300 Subject: [PATCH] #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. --- aiogram/custom_types.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 aiogram/custom_types.py diff --git a/aiogram/custom_types.py b/aiogram/custom_types.py new file mode 100644 index 00000000..cc600b63 --- /dev/null +++ b/aiogram/custom_types.py @@ -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]