Visitar URL original
Merge pull request #238 from pyrogram/bot-api-4.2 · alistvt/pyrogram@6c140fd · GitHub
Skip to content

Commit 6c140fd

Browse files
authored
Merge pull request pyrogram#238 from pyrogram/bot-api-4.2
Bot API 4.2
2 parents 80081a2 + 5848291 commit 6c140fd

26 files changed

Lines changed: 235 additions & 102 deletions

‎.gitignore‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
config.ini
44

55
# Pyrogram generated code
6-
pyrogram/api/errors/exceptions/
6+
pyrogram/errors/exceptions/
77
pyrogram/api/functions/
88
pyrogram/api/types/
99
pyrogram/api/all.py

‎pyrogram/client/ext/dispatcher.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from pyrogram.api import types
2727
from ..handlers import (
2828
CallbackQueryHandler, MessageHandler, DeletedMessagesHandler,
29-
UserStatusHandler, RawUpdateHandler, InlineQueryHandler
29+
UserStatusHandler, RawUpdateHandler, InlineQueryHandler, PollHandler
3030
)
3131

3232
log = logging.getLogger(__name__)
@@ -79,7 +79,10 @@ def __init__(self, client, workers: int):
7979
),
8080

8181
(types.UpdateBotInlineQuery,):
82-
lambda upd, usr, cht: (pyrogram.InlineQuery._parse(self.client, upd, usr), InlineQueryHandler)
82+
lambda upd, usr, cht: (pyrogram.InlineQuery._parse(self.client, upd, usr), InlineQueryHandler),
83+
84+
(types.UpdateMessagePoll,):
85+
lambda upd, usr, cht: (pyrogram.Poll._parse(self.client, upd), PollHandler)
8386
}
8487

8588
self.update_parsers = {key: value for key_tuple, value in self.update_parsers.items() for key in key_tuple}

‎pyrogram/client/handlers/__init__.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121
from .disconnect_handler import DisconnectHandler
2222
from .inline_query_handler import InlineQueryHandler
2323
from .message_handler import MessageHandler
24+
from .poll_handler import PollHandler
2425
from .raw_update_handler import RawUpdateHandler
2526
from .user_status_handler import UserStatusHandler
2627

2728
__all__ = [
2829
"MessageHandler", "DeletedMessagesHandler", "CallbackQueryHandler", "RawUpdateHandler", "DisconnectHandler",
29-
"UserStatusHandler", "InlineQueryHandler"
30+
"UserStatusHandler", "InlineQueryHandler", "PollHandler"
3031
]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2019 Dan Tès <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from .handler import Handler
20+
21+
22+
class PollHandler(Handler):
23+
"""The Poll handler class. Used to handle polls updates.
24+
25+
It is intended to be used with :meth:`add_handler() <pyrogram.Client.add_handler>`
26+
27+
For a nicer way to register this handler, have a look at the
28+
:meth:`on_poll() <pyrogram.Client.on_poll>` decorator.
29+
30+
Args:
31+
callback (``callable``):
32+
Pass a function that will be called when a new poll update arrives. It takes *(client, poll)*
33+
as positional arguments (look at the section below for a detailed description).
34+
35+
filters (:obj:`Filters <pyrogram.Filters>`):
36+
Pass one or more filters to allow only a subset of polls to be passed
37+
in your callback function.
38+
39+
Other parameters:
40+
client (:obj:`Client <pyrogram.Client>`):
41+
The Client itself, useful when you want to call other API methods inside the poll handler.
42+
43+
poll (:obj:`Poll <pyrogram.Poll>`):
44+
The received poll.
45+
"""
46+
47+
def __init__(self, callback: callable, filters=None):
48+
super().__init__(callback, filters)

‎pyrogram/client/methods/decorators/__init__.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from .on_disconnect import OnDisconnect
2222
from .on_inline_query import OnInlineQuery
2323
from .on_message import OnMessage
24+
from .on_poll import OnPoll
2425
from .on_raw_update import OnRawUpdate
2526
from .on_user_status import OnUserStatus
2627

@@ -32,6 +33,7 @@ class Decorators(
3233
OnRawUpdate,
3334
OnDisconnect,
3435
OnUserStatus,
35-
OnInlineQuery
36+
OnInlineQuery,
37+
OnPoll
3638
):
3739
pass
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2019 Dan Tès <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from typing import Tuple
20+
21+
import pyrogram
22+
from pyrogram.client.filters.filter import Filter
23+
from pyrogram.client.handlers.handler import Handler
24+
from ...ext import BaseClient
25+
26+
27+
class OnPoll(BaseClient):
28+
def on_poll(
29+
self=None,
30+
filters=None,
31+
group: int = 0
32+
) -> callable:
33+
"""Use this decorator to automatically register a function for handling poll updates.
34+
This does the same thing as :meth:`add_handler` using the :class:`PollHandler`.
35+
36+
Args:
37+
filters (:obj:`Filters <pyrogram.Filters>`):
38+
Pass one or more filters to allow only a subset of polls to be passed
39+
in your function.
40+
41+
group (``int``, *optional*):
42+
The group identifier, defaults to 0.
43+
"""
44+
45+
def decorator(func: callable) -> Tuple[Handler, int]:
46+
if isinstance(func, tuple):
47+
func = func[0].callback
48+
49+
handler = pyrogram.PollHandler(func, filters)
50+
51+
if isinstance(self, Filter):
52+
return pyrogram.PollHandler(func, self), group if filters is None else filters
53+
54+
if self is not None:
55+
self.add_handler(handler, group)
56+
57+
return handler, group
58+
59+
return decorator

‎pyrogram/client/methods/messages/__init__.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19-
from .close_poll import ClosePoll
19+
from .stop_poll import StopPoll
2020
from .delete_messages import DeleteMessages
2121
from .download_media import DownloadMedia
2222
from .edit_message_caption import EditMessageCaption
@@ -72,7 +72,7 @@ class Messages(
7272
SendVoice,
7373
SendPoll,
7474
VotePoll,
75-
ClosePoll,
75+
StopPoll,
7676
RetractVote,
7777
DownloadMedia,
7878
IterHistory,

‎pyrogram/client/methods/messages/retract_vote.py‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from typing import Union
2020

21+
import pyrogram
2122
from pyrogram.api import functions
2223
from pyrogram.client.ext import BaseClient
2324

@@ -26,8 +27,8 @@ class RetractVote(BaseClient):
2627
def retract_vote(
2728
self,
2829
chat_id: Union[int, str],
29-
message_id: id
30-
) -> bool:
30+
message_id: int
31+
) -> "pyrogram.Poll":
3132
"""Use this method to retract your vote in a poll.
3233
3334
Args:
@@ -37,20 +38,20 @@ def retract_vote(
3738
For a contact that exists in your Telegram address book you can use his phone number (str).
3839
3940
message_id (``int``):
40-
Unique poll message identifier inside this chat.
41+
Identifier of the original message with the poll.
4142
4243
Returns:
43-
On success, True is returned.
44+
On success, the :obj:`Poll <pyrogram.Poll>` with the retracted vote is returned.
4445
4546
Raises:
4647
:class:`RPCError <pyrogram.RPCError>` in case of a Telegram RPC error.
4748
"""
48-
self.send(
49+
r = self.send(
4950
functions.messages.SendVote(
5051
peer=self.resolve_peer(chat_id),
5152
msg_id=message_id,
5253
options=[]
5354
)
5455
)
5556

56-
return True
57+
return pyrogram.Poll._parse(self, r.updates[0])

‎pyrogram/client/methods/messages/send_animation.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def send_animation(
8383
thumb (``str``, *optional*):
8484
Thumbnail of the animation file sent.
8585
The thumbnail should be in JPEG format and less than 200 KB in size.
86-
A thumbnail's width and height should not exceed 90 pixels.
86+
A thumbnail's width and height should not exceed 320 pixels.
8787
Thumbnails can't be reused and can be only uploaded as a new file.
8888
8989
disable_notification (``bool``, *optional*):

‎pyrogram/client/methods/messages/send_audio.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def send_audio(
8585
thumb (``str``, *optional*):
8686
Thumbnail of the music file album cover.
8787
The thumbnail should be in JPEG format and less than 200 KB in size.
88-
A thumbnail's width and height should not exceed 90 pixels.
88+
A thumbnail's width and height should not exceed 320 pixels.
8989
Thumbnails can't be reused and can be only uploaded as a new file.
9090
9191
disable_notification (``bool``, *optional*):

0 commit comments

Comments
 (0)