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:
Danipulok 2022-10-22 00:29:29 +03:00 committed by GitHub
parent f4d3dbf3bf
commit 8b0ca520f3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 204 additions and 0 deletions

8
CHANGES/1030.feature.rst Normal file
View 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()``

View file

@ -16,7 +16,11 @@ if TYPE_CHECKING:
DeleteMessage,
EditMessageCaption,
EditMessageReplyMarkup,
EditMessageLiveLocation,
EditMessageMedia,
EditMessageText,
ForwardMessage,
PinChatMessage,
SendAnimation,
SendAudio,
SendContact,
@ -34,6 +38,8 @@ if TYPE_CHECKING:
SendVideo,
SendVideoNote,
SendVoice,
StopMessageLiveLocation,
UnpinChatMessage,
)
from .animation import Animation
from .audio import Audio
@ -45,6 +51,7 @@ if TYPE_CHECKING:
from .game import Game
from .inline_keyboard_markup import InlineKeyboardMarkup
from .input_file import InputFile
from .input_media import InputMedia
from .input_media_audio import InputMediaAudio
from .input_media_document import InputMediaDocument
from .input_media_photo import InputMediaPhoto
@ -1822,6 +1829,36 @@ class Message(TelegramObject):
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(
self,
reply_markup: Optional[InlineKeyboardMarkup] = None,
@ -1837,6 +1874,40 @@ class Message(TelegramObject):
def delete_reply_markup(self) -> EditMessageReplyMarkup:
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(
self,
caption: str,
@ -1860,6 +1931,26 @@ class Message(TelegramObject):
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]:
"""
Returns message URL. Cannot be used in private (one-to-one) chats.

View file

@ -21,6 +21,14 @@ As bot method
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
----------------

View file

@ -21,6 +21,14 @@ As bot method
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
----------------

View file

@ -21,6 +21,14 @@ As bot method
result: Message = await bot.forward_message(...)
As message method
-------------
.. code-block::
result: Message = await message.forward(...)
Method as object
----------------

View file

@ -21,6 +21,14 @@ As bot method
result: bool = await bot.pin_chat_message(...)
As message method
-----------------
.. code-block::
result: bool = await message.pin(...)
Method as object
----------------

View file

@ -21,6 +21,14 @@ As bot method
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
----------------

View file

@ -21,6 +21,14 @@ As bot method
result: bool = await bot.unpin_chat_message(...)
As message method
-----------------
.. code-block::
result: bool = await message.unpin()
Method as object
----------------

View file

@ -7,8 +7,12 @@ from aiogram.methods import (
CopyMessage,
DeleteMessage,
EditMessageCaption,
EditMessageLiveLocation,
EditMessageMedia,
EditMessageReplyMarkup,
EditMessageText,
ForwardMessage,
PinChatMessage,
SendAnimation,
SendAudio,
SendContact,
@ -26,6 +30,8 @@ from aiogram.methods import (
SendVideo,
SendVideoNote,
SendVoice,
StopMessageLiveLocation,
UnpinChatMessage,
TelegramMethod,
)
from aiogram.types import (
@ -39,6 +45,7 @@ from aiogram.types import (
Game,
InlineKeyboardButton,
InlineKeyboardMarkup,
InputMediaPhoto,
Invoice,
Location,
MessageAutoDeleteTimerChanged,
@ -619,6 +626,23 @@ class TestMessage:
assert isinstance(method, EditMessageText)
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):
reply_markup = InlineKeyboardMarkup(
inline_keyboard=[
@ -677,6 +701,22 @@ class TestMessage:
assert method.reply_markup is None
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):
message = Message(
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.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(
"text,entities,mode,expected_value",
[