Added handling new event type and approve/decline aliases for ChatJoinRequest

This commit is contained in:
Alex Root Junior 2021-11-08 01:51:19 +02:00
parent 7caeeb667f
commit b34934b3c7
6 changed files with 94 additions and 0 deletions

View file

@ -89,6 +89,10 @@ BUILTIN_FILTERS: Dict[str, Tuple[Type[BaseFilter], ...]] = {
*_ALL_EVENTS_FILTERS,
*_TELEGRAM_EVENTS_FILTERS,
),
"chat_join_request": (
*_ALL_EVENTS_FILTERS,
*_TELEGRAM_EVENTS_FILTERS,
),
"error": (
ExceptionMessageFilter,
ExceptionTypeFilter,

View file

@ -58,6 +58,7 @@ class Router:
self.poll_answer = TelegramEventObserver(router=self, event_name="poll_answer")
self.my_chat_member = TelegramEventObserver(router=self, event_name="my_chat_member")
self.chat_member = TelegramEventObserver(router=self, event_name="chat_member")
self.chat_join_request = TelegramEventObserver(router=self, event_name="chat_join_request")
self.errors = TelegramEventObserver(router=self, event_name="error")
@ -78,6 +79,7 @@ class Router:
"poll_answer": self.poll_answer,
"my_chat_member": self.my_chat_member,
"chat_member": self.chat_member,
"chat_join_request": self.chat_join_request,
"error": self.errors,
}

View file

@ -5,6 +5,7 @@ from typing import TYPE_CHECKING, Optional
from pydantic import Field
from ..methods import ApproveChatJoinRequest, DeclineChatJoinRequest
from .base import TelegramObject
if TYPE_CHECKING:
@ -30,3 +31,22 @@ class ChatJoinRequest(TelegramObject):
"""*Optional*. Bio of the user."""
invite_link: Optional[ChatInviteLink] = None
"""*Optional*. Chat invite link that was used by the user to send the join request"""
def approve(self) -> ApproveChatJoinRequest:
"""
Use this method to approve a chat join request.
"""
return ApproveChatJoinRequest(
chat_id=self.chat.id,
user_id=self.from_user.id,
)
def decline(self) -> DeclineChatJoinRequest:
"""
Use this method to decline a chat join request.
"""
return DeclineChatJoinRequest(
chat_id=self.chat.id,
user_id=self.from_user.id,
)

View file

@ -96,6 +96,8 @@ class Update(TelegramObject):
return "my_chat_member"
if self.chat_member:
return "chat_member"
if self.chat_join_request:
return "chat_join_request"
raise UpdateTypeLookupError("Update does not contain any known event type.")

View file

@ -0,0 +1,33 @@
import pytest
from aiogram.api.methods import ApproveChatJoinRequest, Request
from tests.mocked_bot import MockedBot
@pytest.mark.skip
class TestApproveChatJoinRequest:
@pytest.mark.asyncio
async def test_method(self, bot: MockedBot):
prepare_result = bot.add_result_for(ApproveChatJoinRequest, ok=True, result=None)
response: bool = await ApproveChatJoinRequest(
chat_id=...,
user_id=...,
)
request: Request = bot.get_request()
assert request.method == "approveChatJoinRequest"
# assert request.data == {}
assert response == prepare_result.result
@pytest.mark.asyncio
async def test_bot_method(self, bot: MockedBot):
prepare_result = bot.add_result_for(ApproveChatJoinRequest, ok=True, result=None)
response: bool = await bot.approve_chat_join_request(
chat_id=...,
user_id=...,
)
request: Request = bot.get_request()
assert request.method == "approveChatJoinRequest"
# assert request.data == {}
assert response == prepare_result.result

View file

@ -0,0 +1,33 @@
import pytest
from aiogram.api.methods import DeclineChatJoinRequest, Request
from tests.mocked_bot import MockedBot
@pytest.mark.skip
class TestDeclineChatJoinRequest:
@pytest.mark.asyncio
async def test_method(self, bot: MockedBot):
prepare_result = bot.add_result_for(DeclineChatJoinRequest, ok=True, result=None)
response: bool = await DeclineChatJoinRequest(
chat_id=...,
user_id=...,
)
request: Request = bot.get_request()
assert request.method == "declineChatJoinRequest"
# assert request.data == {}
assert response == prepare_result.result
@pytest.mark.asyncio
async def test_bot_method(self, bot: MockedBot):
prepare_result = bot.add_result_for(DeclineChatJoinRequest, ok=True, result=None)
response: bool = await bot.decline_chat_join_request(
chat_id=...,
user_id=...,
)
request: Request = bot.get_request()
assert request.method == "declineChatJoinRequest"
# assert request.data == {}
assert response == prepare_result.result