mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-11 18:01:04 +00:00
Add missing Message methods (#1030)
* Add `Message.forward` method * Add docs * Add other methods * Fix * Add changes * Fix lints * Update CHANGES/1030.feature.rst Co-authored-by: Alex Root Junior <jroot.junior@gmail.com> Co-authored-by: Alex Root Junior <jroot.junior@gmail.com>
This commit is contained in:
parent
f4d3dbf3bf
commit
8b0ca520f3
9 changed files with 204 additions and 0 deletions
8
CHANGES/1030.feature.rst
Normal file
8
CHANGES/1030.feature.rst
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
Added following methods to ``Message`` class:
|
||||||
|
|
||||||
|
- ``Message.forward(...)``
|
||||||
|
- ``Message.edit_media(...)``
|
||||||
|
- ``Message.edit_live_location(...)``
|
||||||
|
- ``Message.stop_live_location(...)``
|
||||||
|
- ``Message.pin(...)``
|
||||||
|
- ``Message.unpin()``
|
||||||
|
|
@ -16,7 +16,11 @@ if TYPE_CHECKING:
|
||||||
DeleteMessage,
|
DeleteMessage,
|
||||||
EditMessageCaption,
|
EditMessageCaption,
|
||||||
EditMessageReplyMarkup,
|
EditMessageReplyMarkup,
|
||||||
|
EditMessageLiveLocation,
|
||||||
|
EditMessageMedia,
|
||||||
EditMessageText,
|
EditMessageText,
|
||||||
|
ForwardMessage,
|
||||||
|
PinChatMessage,
|
||||||
SendAnimation,
|
SendAnimation,
|
||||||
SendAudio,
|
SendAudio,
|
||||||
SendContact,
|
SendContact,
|
||||||
|
|
@ -34,6 +38,8 @@ if TYPE_CHECKING:
|
||||||
SendVideo,
|
SendVideo,
|
||||||
SendVideoNote,
|
SendVideoNote,
|
||||||
SendVoice,
|
SendVoice,
|
||||||
|
StopMessageLiveLocation,
|
||||||
|
UnpinChatMessage,
|
||||||
)
|
)
|
||||||
from .animation import Animation
|
from .animation import Animation
|
||||||
from .audio import Audio
|
from .audio import Audio
|
||||||
|
|
@ -45,6 +51,7 @@ if TYPE_CHECKING:
|
||||||
from .game import Game
|
from .game import Game
|
||||||
from .inline_keyboard_markup import InlineKeyboardMarkup
|
from .inline_keyboard_markup import InlineKeyboardMarkup
|
||||||
from .input_file import InputFile
|
from .input_file import InputFile
|
||||||
|
from .input_media import InputMedia
|
||||||
from .input_media_audio import InputMediaAudio
|
from .input_media_audio import InputMediaAudio
|
||||||
from .input_media_document import InputMediaDocument
|
from .input_media_document import InputMediaDocument
|
||||||
from .input_media_photo import InputMediaPhoto
|
from .input_media_photo import InputMediaPhoto
|
||||||
|
|
@ -1822,6 +1829,36 @@ class Message(TelegramObject):
|
||||||
reply_markup=reply_markup,
|
reply_markup=reply_markup,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def forward(
|
||||||
|
self,
|
||||||
|
chat_id: Union[int, str],
|
||||||
|
disable_notification: Optional[bool] = None,
|
||||||
|
protect_content: Optional[bool] = None,
|
||||||
|
) -> ForwardMessage:
|
||||||
|
from ..methods import ForwardMessage
|
||||||
|
|
||||||
|
return ForwardMessage(
|
||||||
|
chat_id=chat_id,
|
||||||
|
from_chat_id=self.chat.id,
|
||||||
|
message_id=self.message_id,
|
||||||
|
disable_notification=disable_notification,
|
||||||
|
protect_content=protect_content,
|
||||||
|
)
|
||||||
|
|
||||||
|
def edit_media(
|
||||||
|
self,
|
||||||
|
media: InputMedia,
|
||||||
|
reply_markup: Optional[InlineKeyboardMarkup] = None,
|
||||||
|
) -> EditMessageMedia:
|
||||||
|
from ..methods import EditMessageMedia
|
||||||
|
|
||||||
|
return EditMessageMedia(
|
||||||
|
media=media,
|
||||||
|
chat_id=self.chat.id,
|
||||||
|
message_id=self.message_id,
|
||||||
|
reply_markup=reply_markup,
|
||||||
|
)
|
||||||
|
|
||||||
def edit_reply_markup(
|
def edit_reply_markup(
|
||||||
self,
|
self,
|
||||||
reply_markup: Optional[InlineKeyboardMarkup] = None,
|
reply_markup: Optional[InlineKeyboardMarkup] = None,
|
||||||
|
|
@ -1837,6 +1874,40 @@ class Message(TelegramObject):
|
||||||
def delete_reply_markup(self) -> EditMessageReplyMarkup:
|
def delete_reply_markup(self) -> EditMessageReplyMarkup:
|
||||||
return self.edit_reply_markup(reply_markup=None)
|
return self.edit_reply_markup(reply_markup=None)
|
||||||
|
|
||||||
|
def edit_live_location(
|
||||||
|
self,
|
||||||
|
latitude: float,
|
||||||
|
longitude: float,
|
||||||
|
horizontal_accuracy: Optional[float] = None,
|
||||||
|
heading: Optional[int] = None,
|
||||||
|
proximity_alert_radius: Optional[int] = None,
|
||||||
|
reply_markup: Optional[InlineKeyboardMarkup] = None,
|
||||||
|
) -> EditMessageLiveLocation:
|
||||||
|
from ..methods import EditMessageLiveLocation
|
||||||
|
|
||||||
|
return EditMessageLiveLocation(
|
||||||
|
latitude=latitude,
|
||||||
|
longitude=longitude,
|
||||||
|
chat_id=self.chat.id,
|
||||||
|
message_id=self.message_id,
|
||||||
|
horizontal_accuracy=horizontal_accuracy,
|
||||||
|
heading=heading,
|
||||||
|
proximity_alert_radius=proximity_alert_radius,
|
||||||
|
reply_markup=reply_markup,
|
||||||
|
)
|
||||||
|
|
||||||
|
def stop_live_location(
|
||||||
|
self,
|
||||||
|
reply_markup: Optional[InlineKeyboardMarkup] = None,
|
||||||
|
) -> StopMessageLiveLocation:
|
||||||
|
from ..methods import StopMessageLiveLocation
|
||||||
|
|
||||||
|
return StopMessageLiveLocation(
|
||||||
|
chat_id=self.chat.id,
|
||||||
|
message_id=self.message_id,
|
||||||
|
reply_markup=reply_markup,
|
||||||
|
)
|
||||||
|
|
||||||
def edit_caption(
|
def edit_caption(
|
||||||
self,
|
self,
|
||||||
caption: str,
|
caption: str,
|
||||||
|
|
@ -1860,6 +1931,26 @@ class Message(TelegramObject):
|
||||||
|
|
||||||
return DeleteMessage(chat_id=self.chat.id, message_id=self.message_id)
|
return DeleteMessage(chat_id=self.chat.id, message_id=self.message_id)
|
||||||
|
|
||||||
|
def pin(
|
||||||
|
self,
|
||||||
|
disable_notification: Optional[bool] = None,
|
||||||
|
) -> PinChatMessage:
|
||||||
|
from ..methods import PinChatMessage
|
||||||
|
|
||||||
|
return PinChatMessage(
|
||||||
|
chat_id=self.chat.id,
|
||||||
|
message_id=self.message_id,
|
||||||
|
disable_notification=disable_notification,
|
||||||
|
)
|
||||||
|
|
||||||
|
def unpin(self) -> UnpinChatMessage:
|
||||||
|
from ..methods import UnpinChatMessage
|
||||||
|
|
||||||
|
return UnpinChatMessage(
|
||||||
|
chat_id=self.chat.id,
|
||||||
|
message_id=self.message_id,
|
||||||
|
)
|
||||||
|
|
||||||
def get_url(self, force_private: bool = False) -> Optional[str]:
|
def get_url(self, force_private: bool = False) -> Optional[str]:
|
||||||
"""
|
"""
|
||||||
Returns message URL. Cannot be used in private (one-to-one) chats.
|
Returns message URL. Cannot be used in private (one-to-one) chats.
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,14 @@ As bot method
|
||||||
result: Union[Message, bool] = await bot.edit_message_live_location(...)
|
result: Union[Message, bool] = await bot.edit_message_live_location(...)
|
||||||
|
|
||||||
|
|
||||||
|
As message method
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
.. code-block::
|
||||||
|
|
||||||
|
result: Union[Message, bool] = await message.edit_live_location(...)
|
||||||
|
|
||||||
|
|
||||||
Method as object
|
Method as object
|
||||||
----------------
|
----------------
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,14 @@ As bot method
|
||||||
result: Union[Message, bool] = await bot.edit_message_media(...)
|
result: Union[Message, bool] = await bot.edit_message_media(...)
|
||||||
|
|
||||||
|
|
||||||
|
As message method
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
.. code-block::
|
||||||
|
|
||||||
|
result: Union[Message, bool] = await message.edit_media(...)
|
||||||
|
|
||||||
|
|
||||||
Method as object
|
Method as object
|
||||||
----------------
|
----------------
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,14 @@ As bot method
|
||||||
result: Message = await bot.forward_message(...)
|
result: Message = await bot.forward_message(...)
|
||||||
|
|
||||||
|
|
||||||
|
As message method
|
||||||
|
-------------
|
||||||
|
|
||||||
|
.. code-block::
|
||||||
|
|
||||||
|
result: Message = await message.forward(...)
|
||||||
|
|
||||||
|
|
||||||
Method as object
|
Method as object
|
||||||
----------------
|
----------------
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,14 @@ As bot method
|
||||||
result: bool = await bot.pin_chat_message(...)
|
result: bool = await bot.pin_chat_message(...)
|
||||||
|
|
||||||
|
|
||||||
|
As message method
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
.. code-block::
|
||||||
|
|
||||||
|
result: bool = await message.pin(...)
|
||||||
|
|
||||||
|
|
||||||
Method as object
|
Method as object
|
||||||
----------------
|
----------------
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,14 @@ As bot method
|
||||||
result: Union[Message, bool] = await bot.stop_message_live_location(...)
|
result: Union[Message, bool] = await bot.stop_message_live_location(...)
|
||||||
|
|
||||||
|
|
||||||
|
As message method
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
.. code-block::
|
||||||
|
|
||||||
|
result: Union[Message, bool] = await message.stop_live_location(...)
|
||||||
|
|
||||||
|
|
||||||
Method as object
|
Method as object
|
||||||
----------------
|
----------------
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,14 @@ As bot method
|
||||||
result: bool = await bot.unpin_chat_message(...)
|
result: bool = await bot.unpin_chat_message(...)
|
||||||
|
|
||||||
|
|
||||||
|
As message method
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
.. code-block::
|
||||||
|
|
||||||
|
result: bool = await message.unpin()
|
||||||
|
|
||||||
|
|
||||||
Method as object
|
Method as object
|
||||||
----------------
|
----------------
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,8 +7,12 @@ from aiogram.methods import (
|
||||||
CopyMessage,
|
CopyMessage,
|
||||||
DeleteMessage,
|
DeleteMessage,
|
||||||
EditMessageCaption,
|
EditMessageCaption,
|
||||||
|
EditMessageLiveLocation,
|
||||||
|
EditMessageMedia,
|
||||||
EditMessageReplyMarkup,
|
EditMessageReplyMarkup,
|
||||||
EditMessageText,
|
EditMessageText,
|
||||||
|
ForwardMessage,
|
||||||
|
PinChatMessage,
|
||||||
SendAnimation,
|
SendAnimation,
|
||||||
SendAudio,
|
SendAudio,
|
||||||
SendContact,
|
SendContact,
|
||||||
|
|
@ -26,6 +30,8 @@ from aiogram.methods import (
|
||||||
SendVideo,
|
SendVideo,
|
||||||
SendVideoNote,
|
SendVideoNote,
|
||||||
SendVoice,
|
SendVoice,
|
||||||
|
StopMessageLiveLocation,
|
||||||
|
UnpinChatMessage,
|
||||||
TelegramMethod,
|
TelegramMethod,
|
||||||
)
|
)
|
||||||
from aiogram.types import (
|
from aiogram.types import (
|
||||||
|
|
@ -39,6 +45,7 @@ from aiogram.types import (
|
||||||
Game,
|
Game,
|
||||||
InlineKeyboardButton,
|
InlineKeyboardButton,
|
||||||
InlineKeyboardMarkup,
|
InlineKeyboardMarkup,
|
||||||
|
InputMediaPhoto,
|
||||||
Invoice,
|
Invoice,
|
||||||
Location,
|
Location,
|
||||||
MessageAutoDeleteTimerChanged,
|
MessageAutoDeleteTimerChanged,
|
||||||
|
|
@ -619,6 +626,23 @@ class TestMessage:
|
||||||
assert isinstance(method, EditMessageText)
|
assert isinstance(method, EditMessageText)
|
||||||
assert method.chat_id == message.chat.id
|
assert method.chat_id == message.chat.id
|
||||||
|
|
||||||
|
def test_forward(self):
|
||||||
|
message = Message(
|
||||||
|
message_id=42, chat=Chat(id=42, type="private"), date=datetime.datetime.now()
|
||||||
|
)
|
||||||
|
method = message.forward(chat_id=69)
|
||||||
|
assert isinstance(method, ForwardMessage)
|
||||||
|
assert method.chat_id == 69
|
||||||
|
assert method.from_chat_id == message.chat.id
|
||||||
|
|
||||||
|
def test_edit_media(self):
|
||||||
|
message = Message(
|
||||||
|
message_id=42, chat=Chat(id=42, type="private"), date=datetime.datetime.now()
|
||||||
|
)
|
||||||
|
method = message.edit_media(media=InputMediaPhoto(media="photo.jpg"))
|
||||||
|
assert isinstance(method, EditMessageMedia)
|
||||||
|
assert method.chat_id == message.chat.id
|
||||||
|
|
||||||
def test_edit_reply_markup(self):
|
def test_edit_reply_markup(self):
|
||||||
reply_markup = InlineKeyboardMarkup(
|
reply_markup = InlineKeyboardMarkup(
|
||||||
inline_keyboard=[
|
inline_keyboard=[
|
||||||
|
|
@ -677,6 +701,22 @@ class TestMessage:
|
||||||
assert method.reply_markup is None
|
assert method.reply_markup is None
|
||||||
assert method.chat_id == message.chat.id
|
assert method.chat_id == message.chat.id
|
||||||
|
|
||||||
|
def test_edit_live_location(self):
|
||||||
|
message = Message(
|
||||||
|
message_id=42, chat=Chat(id=42, type="private"), date=datetime.datetime.now()
|
||||||
|
)
|
||||||
|
method = message.edit_live_location(latitude=42, longitude=69)
|
||||||
|
assert isinstance(method, EditMessageLiveLocation)
|
||||||
|
assert method.chat_id == message.chat.id
|
||||||
|
|
||||||
|
def test_stop_live_location(self):
|
||||||
|
message = Message(
|
||||||
|
message_id=42, chat=Chat(id=42, type="private"), date=datetime.datetime.now()
|
||||||
|
)
|
||||||
|
method = message.stop_live_location()
|
||||||
|
assert isinstance(method, StopMessageLiveLocation)
|
||||||
|
assert method.chat_id == message.chat.id
|
||||||
|
|
||||||
def test_edit_caption(self):
|
def test_edit_caption(self):
|
||||||
message = Message(
|
message = Message(
|
||||||
message_id=42, chat=Chat(id=42, type="private"), date=datetime.datetime.now()
|
message_id=42, chat=Chat(id=42, type="private"), date=datetime.datetime.now()
|
||||||
|
|
@ -694,6 +734,23 @@ class TestMessage:
|
||||||
assert method.chat_id == message.chat.id
|
assert method.chat_id == message.chat.id
|
||||||
assert method.message_id == message.message_id
|
assert method.message_id == message.message_id
|
||||||
|
|
||||||
|
def test_pin(self):
|
||||||
|
message = Message(
|
||||||
|
message_id=42, chat=Chat(id=42, type="private"), date=datetime.datetime.now()
|
||||||
|
)
|
||||||
|
method = message.pin()
|
||||||
|
assert isinstance(method, PinChatMessage)
|
||||||
|
assert method.chat_id == message.chat.id
|
||||||
|
assert method.message_id == message.message_id
|
||||||
|
|
||||||
|
def test_unpin(self):
|
||||||
|
message = Message(
|
||||||
|
message_id=42, chat=Chat(id=42, type="private"), date=datetime.datetime.now()
|
||||||
|
)
|
||||||
|
method = message.unpin()
|
||||||
|
assert isinstance(method, UnpinChatMessage)
|
||||||
|
assert method.chat_id == message.chat.id
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"text,entities,mode,expected_value",
|
"text,entities,mode,expected_value",
|
||||||
[
|
[
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue