diff --git a/tgram/__init__.py b/tgram/__init__.py index 9d55650..2fe277a 100644 --- a/tgram/__init__.py +++ b/tgram/__init__.py @@ -1,38 +1,15 @@ +import aiohttp + from .methods import Methods from .decorators import Decorators - +from .filters import Filter +from .handlers import Handlers, Handler from . import types -from typing import List +from typing import List, Callable __all__ = ["types", "TgBot", "Handlers"] - -class Handlers: - MESSAGE = "message" - EDITED_MESSAGE = "edited_message" - CHANNEL_POST = "channel_post" - EDITED_CHANNEL_POST = "edited_channel_post" - BUSINESS_CONNECTION = "business_connection" - BUSINESS_MESSAGE = "business_message" - EDITED_BUSINESS_MESSAGE = "edited_business_message" - DELETED_BUSINESS_MESSAGES = "deleted_business_messages" - MESSAGE_REACTION = "message_reaction" - MESSAGE_REACTION_COUNT = "message_reaction_count" - INLINE_QUERY = "inline_query" - CHOSEN_INLINE_RESULT = "chosen_inline_result" - CALLBACK_QUERY = "callback_query" - SHIPPING_QUERY = "shipping_query" - PRE_CHECKOUT_QUERY = "pre_checkout_query" - POLL = "poll" - POLL_ANSWER = "poll_answer" - MY_CHAT_MEMBER = "my_chat_member" - CHAT_MEMBER = "chat_member" - CHAT_JOIN_REQUEST = "chat_join_request" - CHAT_BOOST = "chat_boost" - REMOVED_CHAT_BOOST = "removed_chat_boost" - - API_URL = "https://api.telegram.org/" ALL_UPDATES = [ getattr(Handlers, i) @@ -41,9 +18,40 @@ class Handlers: class TgBot(Methods, Decorators): + _session: "aiohttp.ClientSession" = None + _handlers: List["Handler"] = [] + _api_url: str = None + def __init__( self, bot_token: str, api_url: str = API_URL, allowed_updates: List[str] = [] ) -> None: self.bot_token = bot_token self.api_url = api_url self.allowed_updates = allowed_updates + + if not api_url.endswith("/"): + api_url += "/" + + self._api_url = f"{api_url}bot{bot_token}/" + + def add_handler(self, func: Callable, handler: Handlers, filters: Filter) -> bool: + self._handlers.append(Handler(func, handler, filters)) + return True + + async def _new_session(self) -> None: + session = aiohttp.ClientSession(connector=aiohttp.TCPConnector(limit=50)) + self._session = session + + async def _get_session(self) -> "aiohttp.ClientSession": + if self._session is None or self._session.closed: + await self._new_session() + elif not self._session.loop.is_running(): + await self._session.close() + await self._new_session() + + return self._session + + async def _send_request(self, method: str, data: dict): + request_url = self._api_url + method + session = await self._get_session() + # TODO diff --git a/tgram/filters.py b/tgram/filters.py new file mode 100644 index 0000000..8555cb7 --- /dev/null +++ b/tgram/filters.py @@ -0,0 +1,98 @@ +from typing import Callable, Any + + +class Filter: + def __init__(self, filter_: Callable[[Any], bool]) -> None: + self._filter = filter_ + + def __call__(self, arg: Any) -> bool: + return self._filter(arg) + + def __invert__(self) -> "Filter": + return Filter(lambda v: not self(v)) + + def __and__(self, other: "Filter") -> "Filter": + return Filter(lambda v: self(v) and other(v)) + + def __or__(self, other: "Filter") -> "Filter": + return Filter(lambda v: self(v) or other(v)) + + +threaded = Filter(lambda m: getattr(m, "message_thread_id")) +from_user = Filter(lambda m: getattr(m, "from_user")) +sender_chat = Filter(lambda m: getattr(m, "sender_chat")) +business_connection_id = Filter(lambda m: getattr(m, "business_connection_id")) +forward = Filter(lambda m: getattr(m, "forward_origin")) +topic_message = Filter(lambda m: getattr(m, "is_topic_message")) +automatic_forward = Filter(lambda m: getattr(m, "is_automatic_forward")) +reply = Filter(lambda m: getattr(m, "reply_to_message")) +quote = Filter(lambda m: getattr(m, "quote")) +reply_to_story = Filter(lambda m: getattr(m, "reply_to_story")) +via_bot = Filter(lambda m: getattr(m, "via_bot")) +protected_content = Filter(lambda m: getattr(m, "has_protected_content")) +from_offline = Filter(lambda m: getattr(m, "is_from_offline")) +media_group = Filter(lambda m: getattr(m, "media_group_id")) +text = Filter(lambda m: getattr(m, "text")) +entities = Filter(lambda m: getattr(m, "entities")) +effected_message = Filter(lambda m: getattr(m, "effect_id")) +animation = Filter(lambda m: getattr(m, "animation")) +audio = Filter(lambda m: getattr(m, "audio")) +document = Filter(lambda m: getattr(m, "document")) +photo = Filter(lambda m: getattr(m, "photo")) +sticker = Filter(lambda m: getattr(m, "sticker")) +story = Filter(lambda m: getattr(m, "story")) +video = Filter(lambda m: getattr(m, "video")) +video_note = Filter(lambda m: getattr(m, "video_note")) +voice = Filter(lambda m: getattr(m, "voice")) +caption = Filter(lambda m: getattr(m, "caption")) +media_spoiler = Filter(lambda m: getattr(m, "has_media_spoiler")) +contact = Filter(lambda m: getattr(m, "contact")) +dice = Filter(lambda m: getattr(m, "dice")) +game = Filter(lambda m: getattr(m, "game")) +poll = Filter(lambda m: getattr(m, "poll")) +venue = Filter(lambda m: getattr(m, "venue")) +location = Filter(lambda m: getattr(m, "location")) +new_chat_members = Filter(lambda m: getattr(m, "new_chat_members")) +left_chat_member = Filter(lambda m: getattr(m, "left_chat_member")) +new_chat_title = Filter(lambda m: getattr(m, "new_chat_title")) +new_chat_photo = Filter(lambda m: getattr(m, "new_chat_photo")) +delete_chat_photo = Filter(lambda m: getattr(m, "delete_chat_photo")) +group_chat_created = Filter(lambda m: getattr(m, "group_chat_created")) +supergroup_chat_created = Filter(lambda m: getattr(m, "supergroup_chat_created")) +channel_chat_created = Filter(lambda m: getattr(m, "channel_chat_created")) +message_auto_delete_timer_changed = Filter( + lambda m: getattr(m, "message_auto_delete_timer_changed") +) +migrate_to_chat_id = Filter(lambda m: getattr(m, "migrate_to_chat_id")) +migrate_from_chat_id = Filter(lambda m: getattr(m, "migrate_from_chat_id")) +pinned_message = Filter(lambda m: getattr(m, "pinned_message")) +invoice = Filter(lambda m: getattr(m, "invoice")) +successful_payment = Filter(lambda m: getattr(m, "successful_payment")) +users_shared = Filter(lambda m: getattr(m, "users_shared")) +chat_shared = Filter(lambda m: getattr(m, "chat_shared")) +connected_website = Filter(lambda m: getattr(m, "connected_website")) +write_access_allowed = Filter(lambda m: getattr(m, "write_access_allowed")) +passport_data = Filter(lambda m: getattr(m, "passport_data")) +proximity_alert_triggered = Filter(lambda m: getattr(m, "proximity_alert_triggered")) +boost_added = Filter(lambda m: getattr(m, "boost_added")) +chat_background_set = Filter(lambda m: getattr(m, "chat_background_set")) +forum_topic_created = Filter(lambda m: getattr(m, "forum_topic_created")) +forum_topic_edited = Filter(lambda m: getattr(m, "forum_topic_edited")) +forum_topic_closed = Filter(lambda m: getattr(m, "forum_topic_closed")) +forum_topic_reopened = Filter(lambda m: getattr(m, "forum_topic_reopened")) +general_forum_topic_hidden = Filter(lambda m: getattr(m, "general_forum_topic_hidden")) +general_forum_topic_unhidden = Filter( + lambda m: getattr(m, "general_forum_topic_unhidden") +) +giveaway_created = Filter(lambda m: getattr(m, "giveaway_created")) +giveaway = Filter(lambda m: getattr(m, "giveaway")) +giveaway_winners = Filter(lambda m: getattr(m, "giveaway_winners")) +giveaway_completed = Filter(lambda m: getattr(m, "giveaway_completed")) +video_chat_scheduled = Filter(lambda m: getattr(m, "video_chat_scheduled")) +video_chat_started = Filter(lambda m: getattr(m, "video_chat_started")) +video_chat_ended = Filter(lambda m: getattr(m, "video_chat_ended")) +video_chat_participants_invited = Filter( + lambda m: getattr(m, "video_chat_participants_invited") +) +web_app_data = Filter(lambda m: getattr(m, "web_app_data")) +reply_markup = Filter(lambda m: getattr(m, "reply_markup")) diff --git a/tgram/handlers.py b/tgram/handlers.py new file mode 100644 index 0000000..71da5a8 --- /dev/null +++ b/tgram/handlers.py @@ -0,0 +1,35 @@ +from .filters import Filter + +from typing import Callable + + +class Handlers: + MESSAGE = "message" + EDITED_MESSAGE = "edited_message" + CHANNEL_POST = "channel_post" + EDITED_CHANNEL_POST = "edited_channel_post" + BUSINESS_CONNECTION = "business_connection" + BUSINESS_MESSAGE = "business_message" + EDITED_BUSINESS_MESSAGE = "edited_business_message" + DELETED_BUSINESS_MESSAGES = "deleted_business_messages" + MESSAGE_REACTION = "message_reaction" + MESSAGE_REACTION_COUNT = "message_reaction_count" + INLINE_QUERY = "inline_query" + CHOSEN_INLINE_RESULT = "chosen_inline_result" + CALLBACK_QUERY = "callback_query" + SHIPPING_QUERY = "shipping_query" + PRE_CHECKOUT_QUERY = "pre_checkout_query" + POLL = "poll" + POLL_ANSWER = "poll_answer" + MY_CHAT_MEMBER = "my_chat_member" + CHAT_MEMBER = "chat_member" + CHAT_JOIN_REQUEST = "chat_join_request" + CHAT_BOOST = "chat_boost" + REMOVED_CHAT_BOOST = "removed_chat_boost" + + +class Handler: + def __init__(self, callback: Callable, type: str, filters: Filter) -> None: + self.callback = callback + self.type = type + self.filter = filters diff --git a/tgram/methods.py b/tgram/methods.py index afc9aaa..8d855a2 100644 --- a/tgram/methods.py +++ b/tgram/methods.py @@ -13,7 +13,7 @@ async def get_updates( offset: int = None, limit: int = None, timeout: int = None, - allowed_updates: List[str] = None, + allowed_updates: List[str] = [], ) -> List[Update]: """https://core.telegram.org/bots/api/#getupdates""" ...