mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Add default parse mode
This commit is contained in:
parent
62bce34b49
commit
aa7c5796dd
9 changed files with 54 additions and 6 deletions
|
|
@ -1,6 +1,6 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from typing import Any, TypeVar
|
||||
from typing import Any, Optional, TypeVar
|
||||
|
||||
from ...utils.mixins import ContextInstanceMixin
|
||||
from ...utils.token import extract_bot_id, validate_token
|
||||
|
|
@ -12,13 +12,14 @@ T = TypeVar("T")
|
|||
|
||||
|
||||
class BaseBot(ContextInstanceMixin):
|
||||
def __init__(self, token: str, session: BaseSession = None):
|
||||
def __init__(self, token: str, session: BaseSession = None, parse_mode: Optional[str] = None):
|
||||
validate_token(token)
|
||||
|
||||
if session is None:
|
||||
session = AiohttpSession()
|
||||
|
||||
self.session = session
|
||||
self.parse_mode = parse_mode
|
||||
self.__token = token
|
||||
|
||||
@property
|
||||
|
|
|
|||
|
|
@ -38,5 +38,6 @@ class AnswerInlineQuery(TelegramMethod[bool]):
|
|||
|
||||
def build_request(self) -> Request:
|
||||
data: Dict[str, Any] = self.dict()
|
||||
self.prepare_parse_mode(data["results"])
|
||||
|
||||
return Request(method="answerInlineQuery", data=data)
|
||||
|
|
|
|||
|
|
@ -65,6 +65,23 @@ class TelegramMethod(abc.ABC, BaseModel, Generic[T]):
|
|||
else:
|
||||
data[name] = value
|
||||
|
||||
def prepare_parse_mode(self, root: Any) -> None:
|
||||
if isinstance(root, list):
|
||||
for item in root:
|
||||
self.prepare_parse_mode(item)
|
||||
return
|
||||
|
||||
if "parse_mode" not in root:
|
||||
return
|
||||
|
||||
from ..client.bot import Bot
|
||||
|
||||
bot = Bot.get_current(no_error=True)
|
||||
if bot and bot.parse_mode:
|
||||
root["parse_mode"] = bot.parse_mode
|
||||
return
|
||||
return
|
||||
|
||||
async def emit(self, bot: Bot) -> T:
|
||||
return await bot.emit(self)
|
||||
|
||||
|
|
|
|||
|
|
@ -31,5 +31,6 @@ class EditMessageCaption(TelegramMethod[Union[Message, bool]]):
|
|||
|
||||
def build_request(self) -> Request:
|
||||
data: Dict[str, Any] = self.dict()
|
||||
self.prepare_parse_mode(data)
|
||||
|
||||
return Request(method="editMessageCaption", data=data)
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@ class EditMessageMedia(TelegramMethod[Union[Message, bool]]):
|
|||
|
||||
def build_request(self) -> Request:
|
||||
data: Dict[str, Any] = self.dict()
|
||||
self.prepare_parse_mode(data["media"])
|
||||
|
||||
files: Dict[str, InputFile] = {}
|
||||
self.prepare_media_file(data=data, files=files)
|
||||
|
|
|
|||
|
|
@ -27,8 +27,9 @@ class SendMediaGroup(TelegramMethod[List[Message]]):
|
|||
|
||||
def build_request(self) -> Request:
|
||||
data: Dict[str, Any] = self.dict()
|
||||
files: Dict[str, InputFile] = {}
|
||||
self.prepare_parse_mode(data["media"])
|
||||
|
||||
files: Dict[str, InputFile] = {}
|
||||
self.prepare_input_media(data, files)
|
||||
|
||||
return Request(method="sendMediaGroup", data=data, files=files)
|
||||
|
|
|
|||
|
|
@ -41,5 +41,6 @@ class SendMessage(TelegramMethod[Message]):
|
|||
|
||||
def build_request(self) -> Request:
|
||||
data: Dict[str, Any] = self.dict()
|
||||
self.prepare_parse_mode(data)
|
||||
|
||||
return Request(method="sendMessage", data=data)
|
||||
|
|
|
|||
|
|
@ -21,6 +21,15 @@ In this place makes some more differences with official documentations:
|
|||
- timestamps has `datetime.datetime` type instead of `int`
|
||||
- InputFile is used for sending files and is not use `pydantic.BaseModel` as base class
|
||||
|
||||
## Client
|
||||
## Bot instance
|
||||
|
||||
Bot instance can be created from `aiogram.Bot` (`#!python3 from aiogram import Bot`)
|
||||
|
||||
Constructor specification:
|
||||
|
||||
| Argument | Type | Description |
|
||||
| --- | --- | --- |
|
||||
| `token` | `#!python3 str` | Telegram Bot token (Obtained from [@BotFather](https://t.me/BotFather)). |
|
||||
| `session` | `#!python3 Optional[BaseSession]` | HTTP Client session (For example AiohttpSession). If not specified it will be automatically created. |
|
||||
| `parse_mode` | `#!python3 Optional[str]` | Default parse mode. If specified it will be propagated into the API methods at runtime. |
|
||||
|
||||
...
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
import pytest
|
||||
|
||||
from aiogram import Bot
|
||||
from aiogram.api.methods import AnswerInlineQuery, Request
|
||||
from aiogram.api.types import InlineQueryResult
|
||||
from aiogram.api.types import InlineQueryResult, InlineQueryResultPhoto
|
||||
from tests.mocked_bot import MockedBot
|
||||
|
||||
|
||||
|
|
@ -27,3 +28,18 @@ class TestAnswerInlineQuery:
|
|||
request: Request = bot.get_request()
|
||||
assert request.method == "answerInlineQuery"
|
||||
assert response == prepare_result.result
|
||||
|
||||
def test_parse_mode(self):
|
||||
query = AnswerInlineQuery(
|
||||
inline_query_id="query id",
|
||||
results=[InlineQueryResultPhoto(id="result id", photo_url="photo", thumb_url="thumb")],
|
||||
)
|
||||
request = query.build_request()
|
||||
assert request.data["results"][0]["parse_mode"] is None
|
||||
|
||||
token = Bot.set_current(Bot(token="42:TEST", parse_mode="HTML"))
|
||||
try:
|
||||
request = query.build_request()
|
||||
assert request.data["results"][0]["parse_mode"] == "HTML"
|
||||
finally:
|
||||
Bot.reset_current(token)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue