mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-12 18:19:34 +00:00
Add payload generator
This commit is contained in:
parent
7ac1896e81
commit
bff535ddab
2 changed files with 11 additions and 19 deletions
|
|
@ -3,6 +3,7 @@ import signal
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
|
||||||
|
from aiogram.utils.payload import generate_payload
|
||||||
from . import api
|
from . import api
|
||||||
from .api import ApiMethods
|
from .api import ApiMethods
|
||||||
from .types.chat import Chat
|
from .types.chat import Chat
|
||||||
|
|
@ -33,10 +34,6 @@ class AIOGramBot:
|
||||||
def _on_exit(self):
|
def _on_exit(self):
|
||||||
self.session.close()
|
self.session.close()
|
||||||
|
|
||||||
def _prepare_object(self, obj):
|
|
||||||
obj.bot = self
|
|
||||||
return obj
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
async def me(self) -> User:
|
async def me(self) -> User:
|
||||||
if not hasattr(self, '_me'):
|
if not hasattr(self, '_me'):
|
||||||
|
|
@ -48,12 +45,12 @@ class AIOGramBot:
|
||||||
|
|
||||||
async def get_me(self) -> User:
|
async def get_me(self) -> User:
|
||||||
raw = await self.request(ApiMethods.GET_ME)
|
raw = await self.request(ApiMethods.GET_ME)
|
||||||
return self._prepare_object(User.de_json(raw))
|
return User.de_json(raw)
|
||||||
|
|
||||||
async def get_chat(self, chat_id) -> Chat:
|
async def get_chat(self, chat_id) -> Chat:
|
||||||
payload = {'chat_id': chat_id}
|
payload = generate_payload(**locals())
|
||||||
raw = await self.request(ApiMethods.GET_CHAT, payload)
|
raw = await self.request(ApiMethods.GET_CHAT, payload)
|
||||||
return self._prepare_object(Chat.de_json(raw))
|
return Chat.de_json(raw)
|
||||||
|
|
||||||
async def get_updates(self, offset=None, limit=None, timeout=None, allowed_updates=None):
|
async def get_updates(self, offset=None, limit=None, timeout=None, allowed_updates=None):
|
||||||
"""
|
"""
|
||||||
|
|
@ -65,16 +62,6 @@ class AIOGramBot:
|
||||||
Please note that this parameter doesn't affect updates created before the call to the getUpdates, so unwanted updates may be received for a short period of time.
|
Please note that this parameter doesn't affect updates created before the call to the getUpdates, so unwanted updates may be received for a short period of time.
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
payload = {}
|
payload = generate_payload(**locals())
|
||||||
|
|
||||||
if offset:
|
|
||||||
payload['offset'] = offset
|
|
||||||
if limit:
|
|
||||||
payload['limit'] = limit
|
|
||||||
if timeout:
|
|
||||||
payload['timeout'] = timeout
|
|
||||||
if allowed_updates:
|
|
||||||
payload['allowed_updates'] = allowed_updates
|
|
||||||
|
|
||||||
raw = await self.request(ApiMethods.GET_UPDATES, payload)
|
raw = await self.request(ApiMethods.GET_UPDATES, payload)
|
||||||
return [self._prepare_object(Update.de_json(raw_update)) for raw_update in raw]
|
return [Update.de_json(raw_update) for raw_update in raw]
|
||||||
|
|
|
||||||
5
aiogram/utils/payload.py
Normal file
5
aiogram/utils/payload.py
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
DEFAULT_FILTER = ['self']
|
||||||
|
|
||||||
|
|
||||||
|
def generate_payload(skip_items=DEFAULT_FILTER, **kwargs):
|
||||||
|
return {key: value for key, value in kwargs.items() if key not in skip_items and value}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue