Skip to content

Commit

Permalink
Merge pull request #30 from ilya-nikolaev/develop
Browse files Browse the repository at this point in the history
Added aiogram>=3.4.0 support to `AiogramSulgukMiddleware`
  • Loading branch information
Tishka17 authored May 21, 2024
2 parents 1fe0b97 + 93d1bac commit 88f53c7
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 7 deletions.
10 changes: 9 additions & 1 deletion example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os

from aiogram import Bot

from sulguk import transform_html

CHAT_ID = 1
Expand All @@ -10,25 +11,32 @@
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:
print(repr(entity))
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,
entities=result.entities,
disable_web_page_preview=True,
)

await bot.session.close()


asyncio.run(main())
if __name__ == "__main__":
asyncio.run(main())
36 changes: 36 additions & 0 deletions example_poll.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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="<b>You</b> <i>like it</i>.",
correct_option_id=0,
type="quiz",
)

await bot.session.close()


if __name__ == "__main__":
asyncio.run(main())
2 changes: 1 addition & 1 deletion example_transparent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
32 changes: 27 additions & 5 deletions src/sulguk/aiogram_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -12,11 +13,11 @@
EditMessageMedia,
Response,
SendMediaGroup,
SendPoll,
TelegramMethod,
)
from aiogram.methods.base import TelegramType
from aiogram.types import (
UNSET_PARSE_MODE,
InlineQueryResult,
InlineQueryResultArticle,
)
Expand All @@ -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__(
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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", "")
if parse_mode is UNSET_PARSE_MODE:
parse_mode = bot.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

0 comments on commit 88f53c7

Please sign in to comment.