2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-09-05 00:35:10 +00:00

Merge develop -> asyncio-dev

This commit is contained in:
Dan
2020-04-03 17:19:06 +02:00
48 changed files with 474 additions and 18 deletions

View File

@@ -26,12 +26,14 @@ from pyrogram.api.types import (
UpdateEditMessage, UpdateEditChannelMessage,
UpdateDeleteMessages, UpdateDeleteChannelMessages,
UpdateBotCallbackQuery, UpdateInlineBotCallbackQuery,
UpdateUserStatus, UpdateBotInlineQuery, UpdateMessagePoll
UpdateUserStatus, UpdateBotInlineQuery, UpdateMessagePoll,
UpdateBotInlineSend
)
from . import utils
from ..handlers import (
CallbackQueryHandler, MessageHandler, DeletedMessagesHandler,
UserStatusHandler, RawUpdateHandler, InlineQueryHandler, PollHandler
UserStatusHandler, RawUpdateHandler, InlineQueryHandler, PollHandler,
ChosenInlineResultHandler
)
log = logging.getLogger(__name__)
@@ -92,13 +94,17 @@ class Dispatcher:
async def poll_parser(update, users, chats):
return pyrogram.Poll._parse_update(self.client, update), PollHandler
async def chosen_inline_result_parser(update, users, chats):
return pyrogram.ChosenInlineResult._parse(self.client, update, users), ChosenInlineResultHandler
self.update_parsers = {
Dispatcher.MESSAGE_UPDATES: message_parser,
Dispatcher.DELETE_MESSAGES_UPDATES: deleted_messages_parser,
Dispatcher.CALLBACK_QUERY_UPDATES: callback_query_parser,
(UpdateUserStatus,): user_status_parser,
(UpdateBotInlineQuery,): inline_query_parser,
(UpdateMessagePoll,): poll_parser
(UpdateMessagePoll,): poll_parser,
(UpdateBotInlineSend,): chosen_inline_result_parser
}
self.update_parsers = {key: value for key_tuple, value in self.update_parsers.items() for key in key_tuple}

View File

@@ -24,8 +24,9 @@ from .message_handler import MessageHandler
from .poll_handler import PollHandler
from .raw_update_handler import RawUpdateHandler
from .user_status_handler import UserStatusHandler
from .chosen_inline_result_handler import ChosenInlineResultHandler
__all__ = [
"MessageHandler", "DeletedMessagesHandler", "CallbackQueryHandler", "RawUpdateHandler", "DisconnectHandler",
"UserStatusHandler", "InlineQueryHandler", "PollHandler"
"UserStatusHandler", "InlineQueryHandler", "PollHandler", "ChosenInlineResultHandler"
]

View File

@@ -0,0 +1,48 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-2020 Dan <https://github.com/delivrance>
#
# This file is part of Pyrogram.
#
# Pyrogram is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyrogram is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from .handler import Handler
class ChosenInlineResultHandler(Handler):
"""The ChosenInlineResultHandler handler class. Used to handle chosen inline results coming from inline queries.
It is intended to be used with :meth:`~Client.add_handler`
For a nicer way to register this handler, have a look at the
:meth:`~Client.on_chosen_inline_query` decorator.
Parameters:
callback (``callable``):
Pass a function that will be called when a new chosen inline result arrives.
It takes *(client, chosen_inline_result)* as positional arguments (look at the section below for a
detailed description).
filters (:obj:`Filters`):
Pass one or more filters to allow only a subset of chosen inline results to be passed
in your callback function.
Other parameters:
client (:obj:`Client`):
The Client itself, useful when you want to call other API methods inside the message handler.
chosen_inline_result (:obj:`ChosenInlineResult`):
The received chosen inline result.
"""
def __init__(self, callback: callable, filters=None):
super().__init__(callback, filters)

View File

@@ -24,6 +24,7 @@ from .on_message import OnMessage
from .on_poll import OnPoll
from .on_raw_update import OnRawUpdate
from .on_user_status import OnUserStatus
from .on_chosen_inline_result import OnChosenInlineResult
class Decorators(
@@ -34,6 +35,7 @@ class Decorators(
OnDisconnect,
OnUserStatus,
OnInlineQuery,
OnPoll
OnPoll,
OnChosenInlineResult
):
pass

View File

@@ -0,0 +1,56 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-2020 Dan <https://github.com/delivrance>
#
# This file is part of Pyrogram.
#
# Pyrogram is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyrogram is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from typing import Callable
import pyrogram
from pyrogram.client.filters.filter import Filter
from ...ext import BaseClient
class OnChosenInlineResult(BaseClient):
def on_chosen_inline_result(
self=None,
filters=None,
group: int = 0
) -> callable:
"""Decorator for handling chosen inline results.
This does the same thing as :meth:`~pyrogram.Client.add_handler` using the :obj:`~pyrogram.ChosenInlineResult`.
Parameters:
filters (:obj:`~pyrogram.Filters`, *optional*):
Pass one or more filters to allow only a subset of chosen inline results to be passed
in your function.
group (``int``, *optional*):
The group identifier, defaults to 0.
"""
def decorator(func: Callable) -> Callable:
if isinstance(self, pyrogram.Client):
self.add_handler(pyrogram.ChosenInlineResultHandler(func, filters), group)
elif isinstance(self, Filter) or self is None:
func.handler = (
pyrogram.ChosenInlineResultHandler(func, self),
group if filters is None else filters
)
return func
return decorator

View File

@@ -61,7 +61,7 @@ class SendCachedMedia(BaseClient):
A valid file reference obtained by a recently fetched media message.
To be used in combination with a file id in case a file reference is needed.
caption (``bool``, *optional*):
caption (``str``, *optional*):
Media caption, 0-1024 characters.
parse_mode (``str``, *optional*):

View File

@@ -21,8 +21,9 @@ from .inline_query_result import InlineQueryResult
from .inline_query_result_animation import InlineQueryResultAnimation
from .inline_query_result_article import InlineQueryResultArticle
from .inline_query_result_photo import InlineQueryResultPhoto
from .chosen_inline_result import ChosenInlineResult
__all__ = [
"InlineQuery", "InlineQueryResult", "InlineQueryResultArticle", "InlineQueryResultPhoto",
"InlineQueryResultAnimation"
"InlineQueryResultAnimation", "ChosenInlineResult"
]

View File

@@ -0,0 +1,117 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-2020 Dan <https://github.com/delivrance>
#
# This file is part of Pyrogram.
#
# Pyrogram is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyrogram is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
#
# This file is part of Pyrogram.
#
# Pyrogram is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyrogram is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from base64 import b64encode
from struct import pack
from typing import Union
import pyrogram
from pyrogram.api import types
from pyrogram.client.types.object import Object
from pyrogram.client.types.update import Update
from pyrogram.client.types.user_and_chats import User
from pyrogram.client.types.messages_and_media import Location
from pyrogram.client.ext import utils
class ChosenInlineResult(Object, Update):
"""A :doc:`result <InlineQueryResult>` of an inline query chosen by the user and sent to their chat partner.
Parameters:
result_id (``str``):
The unique identifier for the result that was chosen.
from_user (:obj:`User`):
The user that chose the result.
query (``str``):
The query that was used to obtain the result.
location (:obj:`Location`, *optional*):
Sender location, only for bots that require user location.
inline_message_id (``str``, *optional*):
Identifier of the sent inline message.
Available only if there is an :doc:`inline keyboard <InlineKeyboardMarkup>` attached to the message.
Will be also received in :doc:`callback queries <CallbackQuery>` and can be used to edit the message.
.. note::
It is necessary to enable inline feedback via `@Botfather <https://t.me/botfather>`_ in order to receive these
objects in updates.
"""
def __init__(
self,
*,
client: "pyrogram.BaseClient" = None,
result_id: str,
from_user: User,
query: str,
location: "pyrogram.Location" = None,
inline_message_id: str = None,
):
super().__init__(client)
self.result_id = result_id
self.from_user = from_user
self.query = query
self.location = location
self.inline_message_id = inline_message_id
@staticmethod
def _parse(client, chosen_inline_result: types.UpdateBotInlineSend, users) -> "ChosenInlineResult":
inline_message_id = None
if isinstance(chosen_inline_result.msg_id, types.InputBotInlineMessageID):
inline_message_id = b64encode(
pack(
"<iqq",
chosen_inline_result.msg_id.dc_id,
chosen_inline_result.msg_id.id,
chosen_inline_result.msg_id.access_hash
),
b"-_"
).decode().rstrip("=")
return ChosenInlineResult(
result_id=str(chosen_inline_result.id),
from_user=User._parse(client, users[chosen_inline_result.user_id]),
query=chosen_inline_result.query,
location=Location(
longitude=chosen_inline_result.geo.long,
latitude=chosen_inline_result.geo.lat,
client=client
) if chosen_inline_result.geo else None,
inline_message_id=inline_message_id
)

View File

@@ -523,7 +523,7 @@ class Message(Object, Update):
if media:
if isinstance(media, types.MessageMediaPhoto):
photo = Photo._parse(client, media.photo)
photo = Photo._parse(client, media)
elif isinstance(media, types.MessageMediaGeo):
location = Location._parse(client, media.geo)
elif isinstance(media, types.MessageMediaContact):

View File

@@ -42,6 +42,9 @@ class Photo(Object):
height (``int``):
Photo height.
ttl_seconds (``int``):
Time-to-live seconds, for secret photos.
file_size (``int``):
File size.
@@ -60,6 +63,7 @@ class Photo(Object):
file_ref: str,
width: int,
height: int,
ttl_seconds: int,
file_size: int,
date: int,
thumbs: List[Thumbnail]
@@ -70,12 +74,15 @@ class Photo(Object):
self.file_ref = file_ref
self.width = width
self.height = height
self.ttl_seconds = ttl_seconds
self.file_size = file_size
self.date = date
self.thumbs = thumbs
@staticmethod
def _parse(client, photo: types.Photo) -> "Photo":
def _parse(client, media_photo: types.MessageMediaPhoto) -> "Photo":
photo = media_photo.photo
if isinstance(photo, types.Photo):
big = photo.sizes[-1]
@@ -91,6 +98,7 @@ class Photo(Object):
file_ref=encode_file_ref(photo.file_reference),
width=big.w,
height=big.h,
ttl_seconds=media_photo.ttl_seconds,
file_size=big.size,
date=photo.date,
thumbs=Thumbnail._parse(client, photo),