Add support for Dice objects

- add send_dice
- add Dice class
This commit is contained in:
Dan 2020-03-30 14:38:57 +02:00
parent 2f975c447e
commit 746a6eb477
6 changed files with 154 additions and 4 deletions

View File

@ -174,6 +174,7 @@ def pyrogram_api():
vote_poll
stop_poll
retract_vote
send_dice
download_media
""",
chats="""
@ -334,6 +335,7 @@ def pyrogram_api():
WebPage
Poll
PollOption
Dice
""",
bots_keyboard="""
Bots & Keyboards

View File

@ -51,6 +51,7 @@ from .send_video_note import SendVideoNote
from .send_voice import SendVoice
from .stop_poll import StopPoll
from .vote_poll import VotePoll
from .send_dice import SendDice
class Messages(
@ -88,6 +89,7 @@ class Messages(
EditInlineText,
EditInlineCaption,
EditInlineMedia,
EditInlineReplyMarkup
EditInlineReplyMarkup,
SendDice
):
pass

View File

@ -0,0 +1,94 @@
# 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 Union
import pyrogram
from pyrogram.api import functions, types
from pyrogram.client.ext import BaseClient
class SendDice(BaseClient):
def send_dice(
self,
chat_id: Union[int, str],
disable_notification: bool = None,
reply_to_message_id: int = None,
schedule_date: int = None,
reply_markup: Union[
"pyrogram.InlineKeyboardMarkup",
"pyrogram.ReplyKeyboardMarkup",
"pyrogram.ReplyKeyboardRemove",
"pyrogram.ForceReply"
] = None
) -> Union["pyrogram.Message", None]:
"""Send a dice.
Parameters:
chat_id (``int`` | ``str``):
Unique identifier (int) or username (str) of the target chat.
For your personal cloud (Saved Messages) you can simply use "me" or "self".
For a contact that exists in your Telegram address book you can use his phone number (str).
disable_notification (``bool``, *optional*):
Sends the message silently.
Users will receive a notification with no sound.
reply_to_message_id (``int``, *optional*):
If the message is a reply, ID of the original message.
schedule_date (``int``, *optional*):
Date when the message will be automatically sent. Unix time.
reply_markup (:obj:`InlineKeyboardMarkup` | :obj:`ReplyKeyboardMarkup` | :obj:`ReplyKeyboardRemove` | :obj:`ForceReply`, *optional*):
Additional interface options. An object for an inline keyboard, custom reply keyboard,
instructions to remove reply keyboard or to force a reply from the user.
Returns:
:obj:`Message`: On success, the sent dice message is returned.
Example:
.. code-block:: python
app.send_dice("pyrogramlounge")
"""
r = self.send(
functions.messages.SendMedia(
peer=self.resolve_peer(chat_id),
media=types.InputMediaDice(),
silent=disable_notification or None,
reply_to_msg_id=reply_to_message_id,
random_id=self.rnd_id(),
schedule_date=schedule_date,
reply_markup=reply_markup.write() if reply_markup else None,
message=""
)
)
for i in r.updates:
if isinstance(
i,
(types.UpdateNewMessage, types.UpdateNewChannelMessage, types.UpdateNewScheduledMessage)
):
return pyrogram.Message._parse(
self, i.message,
{i.id: i for i in r.users},
{i.id: i for i in r.chats},
is_scheduled=isinstance(i, types.UpdateNewScheduledMessage)
)

View File

@ -35,8 +35,9 @@ from .video import Video
from .video_note import VideoNote
from .voice import Voice
from .webpage import WebPage
from .dice import Dice
__all__ = [
"Animation", "Audio", "Contact", "Document", "Game", "Location", "Message", "MessageEntity", "Photo", "Thumbnail",
"StrippedThumbnail", "Poll", "PollOption", "Sticker", "Venue", "Video", "VideoNote", "Voice", "WebPage"
"StrippedThumbnail", "Poll", "PollOption", "Sticker", "Venue", "Video", "VideoNote", "Voice", "WebPage", "Dice"
]

View File

@ -0,0 +1,44 @@
# 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 struct import pack
from typing import List
import pyrogram
from pyrogram.api import types
from .thumbnail import Thumbnail
from ..object import Object
from ...ext.utils import encode_file_id, encode_file_ref
class Dice(Object):
"""A dice containing a value that is randomly generated by Telegram.
Parameters:
value (``int``):
Dice value, 1-6.
"""
def __init__(self, *, client: "pyrogram.BaseClient" = None, value: int):
super().__init__(client)
self.value = value
@staticmethod
def _parse(client, dice: types.MessageMediaDice) -> "Dice":
return Dice(value=dice.value, client=client)

View File

@ -184,6 +184,9 @@ class Message(Object, Update):
poll (:obj:`Poll`, *optional*):
Message is a native poll, information about the poll.
dice (:obj:`Dice`, *optional*):
A dice containing a value that is randomly generated by Telegram.
new_chat_members (List of :obj:`User`, *optional*):
New members that were added to the group or supergroup and information about them
(the bot itself may be one of these members).
@ -306,6 +309,7 @@ class Message(Object, Update):
venue: "pyrogram.Venue" = None,
web_page: bool = None,
poll: "pyrogram.Poll" = None,
dice: "pyrogram.Dice" = None,
new_chat_members: List[User] = None,
left_chat_member: User = None,
new_chat_title: str = None,
@ -370,6 +374,7 @@ class Message(Object, Update):
self.venue = venue
self.web_page = web_page
self.poll = poll
self.dice = dice
self.new_chat_members = new_chat_members
self.left_chat_member = left_chat_member
self.new_chat_title = new_chat_title
@ -512,6 +517,7 @@ class Message(Object, Update):
document = None
web_page = None
poll = None
dice = None
media = message.media
@ -570,10 +576,10 @@ class Message(Object, Update):
web_page = pyrogram.WebPage._parse(client, media.webpage)
else:
media = None
elif isinstance(media, types.MessageMediaPoll):
poll = pyrogram.Poll._parse(client, media)
elif isinstance(media, types.MessageMediaDice):
dice = pyrogram.Dice._parse(client, media)
else:
media = None
@ -643,6 +649,7 @@ class Message(Object, Update):
document=document,
web_page=web_page,
poll=poll,
dice=dice,
views=message.views,
via_bot=User._parse(client, users.get(message.via_bot_id, None)),
outgoing=message.out,