From 9617f50362b972655b79e5ec9004cc3f03648b6d Mon Sep 17 00:00:00 2001
From: Ilya Nikolaev <65247719+ilya-nikolaev@users.noreply.github.com>
Date: Fri, 17 May 2024 17:32:31 +0300
Subject: [PATCH 1/6] Added aiogram>=3.4.0 support to `AiogramSulgukMiddleware`
---
src/sulguk/aiogram_middleware.py | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/src/sulguk/aiogram_middleware.py b/src/sulguk/aiogram_middleware.py
index fad7410..68e744b 100644
--- a/src/sulguk/aiogram_middleware.py
+++ b/src/sulguk/aiogram_middleware.py
@@ -2,6 +2,7 @@
from typing import Any, Callable, Dict, Type, TypeVar
from aiogram import Bot
+from aiogram.client.default import Default
from aiogram.client.session.middlewares.base import (
BaseRequestMiddleware,
NextRequestMiddlewareType,
@@ -16,7 +17,6 @@
)
from aiogram.methods.base import TelegramType
from aiogram.types import (
- UNSET_PARSE_MODE,
InlineQueryResult,
InlineQueryResultArticle,
)
@@ -113,6 +113,6 @@ def _transform_text_caption(
def _is_parse_mode_supported(self, method: Any, bot: Bot) -> bool:
parse_mode = getattr(method, "parse_mode", "")
- if parse_mode is UNSET_PARSE_MODE:
- parse_mode = bot.parse_mode
+ if isinstance(parse_mode, Default):
+ parse_mode = bot.default.parse_mode
return parse_mode == SULGUK_PARSE_MODE
From dd4d08f22bbce72fca416187bd5b308931ea6be6 Mon Sep 17 00:00:00 2001
From: Ilya Nikolaev <65247719+ilya-nikolaev@users.noreply.github.com>
Date: Sun, 19 May 2024 06:28:38 +0300
Subject: [PATCH 2/6] Added example for poll
---
example_poll.py | 34 ++++++++++++++++++++++++++++++++++
1 file changed, 34 insertions(+)
create mode 100644 example_poll.py
diff --git a/example_poll.py b/example_poll.py
new file mode 100644
index 0000000..0e22ad5
--- /dev/null
+++ b/example_poll.py
@@ -0,0 +1,34 @@
+import asyncio
+import os
+
+from aiogram import Bot
+from aiogram.client.default import DefaultBotProperties
+
+from sulguk import SULGUK_PARSE_MODE, AiogramSulgukMiddleware
+
+CHAT_ID = 1
+
+
+async def main():
+ bot = Bot(
+ token=os.getenv("BOT_TOKEN"),
+ default=DefaultBotProperties(parse_mode=SULGUK_PARSE_MODE),
+ )
+ bot.session.middleware(AiogramSulgukMiddleware())
+
+ await bot.send_poll(
+ CHAT_ID,
+ question="Do you like Python?",
+ options=[
+ "Yes!",
+ "No",
+ "Probably...",
+ ],
+ explanation="You like.",
+ correct_option_id=0,
+ type="quiz",
+ )
+
+
+if __name__ == "__main__":
+ asyncio.run(main())
From ae71ee14cfe1e9c689a60d11824fe644b6a36b1f Mon Sep 17 00:00:00 2001
From: Ilya Nikolaev <65247719+ilya-nikolaev@users.noreply.github.com>
Date: Sun, 19 May 2024 06:31:18 +0300
Subject: [PATCH 3/6] Added support for polls
---
src/sulguk/aiogram_middleware.py | 26 ++++++++++++++++++++++++--
1 file changed, 24 insertions(+), 2 deletions(-)
diff --git a/src/sulguk/aiogram_middleware.py b/src/sulguk/aiogram_middleware.py
index 68e744b..f5447f1 100644
--- a/src/sulguk/aiogram_middleware.py
+++ b/src/sulguk/aiogram_middleware.py
@@ -13,6 +13,7 @@
EditMessageMedia,
Response,
SendMediaGroup,
+ SendPoll,
TelegramMethod,
)
from aiogram.methods.base import TelegramType
@@ -37,6 +38,7 @@ def __init__(self):
SendMediaGroup: self._process_send_media_group,
AnswerWebAppQuery: self._process_answer_web_app_query,
AnswerInlineQuery: self._process_answer_inline_query,
+ SendPoll: self._process_send_poll,
}
async def __call__(
@@ -80,6 +82,9 @@ def _process_send_media_group(
for media in method.media:
self._transform_text_caption(media, bot)
+ def _process_send_poll(self, method: SendPoll, bot: Bot):
+ self._transform_poll(method, bot)
+
def _process_generic(
self, method: Any, bot: Bot,
) -> None:
@@ -111,8 +116,25 @@ def _transform_text_caption(
method.parse_mode = None
- def _is_parse_mode_supported(self, method: Any, bot: Bot) -> bool:
- parse_mode = getattr(method, "parse_mode", "")
+ def _transform_poll(self, method: SendPoll, bot: Bot):
+ if not self._is_parse_mode_supported(
+ method, bot, "explanation_parse_mode"):
+ return
+
+ explanation_result = transform_html(method.explanation)
+ method.explanation = explanation_result.text
+ method.explanation_entities = explanation_result.entities
+ method.explanation_parse_mode = None
+
+ question_result = transform_html(method.question)
+ method.question = question_result.text
+ method.question_entities = question_result.entities
+ method.question_parse_mode = None
+
+ def _is_parse_mode_supported(
+ self, method: Any, bot: Bot, parse_mode_attr: str = "parse_mode",
+ ) -> bool:
+ parse_mode = getattr(method, parse_mode_attr, "")
if isinstance(parse_mode, Default):
parse_mode = bot.default.parse_mode
return parse_mode == SULGUK_PARSE_MODE
From 0519735d526ca702eefd31cc59fdbf03fd9ab9a4 Mon Sep 17 00:00:00 2001
From: Ilya Nikolaev <65247719+ilya-nikolaev@users.noreply.github.com>
Date: Sun, 19 May 2024 06:36:21 +0300
Subject: [PATCH 4/6] Added formatting and session closing to example
---
example_poll.py | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/example_poll.py b/example_poll.py
index 0e22ad5..1de33f8 100644
--- a/example_poll.py
+++ b/example_poll.py
@@ -24,11 +24,13 @@ async def main():
"No",
"Probably...",
],
- explanation="You like.",
+ explanation="You like it.",
correct_option_id=0,
type="quiz",
)
+ await bot.session.close()
+
if __name__ == "__main__":
asyncio.run(main())
From 615d654250fb487cd082d3a3d4925670fb7be303 Mon Sep 17 00:00:00 2001
From: Ilya Nikolaev <65247719+ilya-nikolaev@users.noreply.github.com>
Date: Sun, 19 May 2024 06:38:46 +0300
Subject: [PATCH 5/6] Example formatted
---
example.py | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/example.py b/example.py
index 5d24181..fb1c751 100644
--- a/example.py
+++ b/example.py
@@ -2,6 +2,7 @@
import os
from aiogram import Bot
+
from sulguk import transform_html
CHAT_ID = 1
@@ -10,7 +11,9 @@
async def main():
with open("example.html") as f:
raw_html = f.read()
+
result = transform_html(raw_html)
+
print("Text:")
print(result.text)
for entity in result.entities:
@@ -18,11 +21,13 @@ async def main():
print()
bot = Bot(token=os.getenv("BOT_TOKEN"))
+
await bot.send_message(
chat_id=CHAT_ID,
text=raw_html,
disable_web_page_preview=True,
)
+
await bot.send_message(
chat_id=CHAT_ID,
text=result.text,
@@ -30,5 +35,8 @@ async def main():
disable_web_page_preview=True,
)
+ await bot.session.close()
+
-asyncio.run(main())
+if __name__ == "__main__":
+ asyncio.run(main())
From 93d1bacfada9459f54437f0c17ea82ad093bc3a0 Mon Sep 17 00:00:00 2001
From: Ilya Nikolaev <65247719+ilya-nikolaev@users.noreply.github.com>
Date: Sun, 19 May 2024 06:40:19 +0300
Subject: [PATCH 6/6] Example formatted
---
example_transparent.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/example_transparent.py b/example_transparent.py
index dbc6408..c9a9bc6 100644
--- a/example_transparent.py
+++ b/example_transparent.py
@@ -3,7 +3,7 @@
from aiogram import Bot
-from sulguk import AiogramSulgukMiddleware, SULGUK_PARSE_MODE
+from sulguk import SULGUK_PARSE_MODE, AiogramSulgukMiddleware
CHAT_ID = 1