From 746a6eb477c58ef7d643461970d45a497281d482 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 30 Mar 2020 14:38:57 +0200 Subject: [PATCH] Add support for Dice objects - add send_dice - add Dice class --- compiler/docs/compiler.py | 2 + pyrogram/client/methods/messages/__init__.py | 4 +- pyrogram/client/methods/messages/send_dice.py | 94 +++++++++++++++++++ .../types/messages_and_media/__init__.py | 3 +- .../client/types/messages_and_media/dice.py | 44 +++++++++ .../types/messages_and_media/message.py | 11 ++- 6 files changed, 154 insertions(+), 4 deletions(-) create mode 100644 pyrogram/client/methods/messages/send_dice.py create mode 100644 pyrogram/client/types/messages_and_media/dice.py diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index 346b47c7..20e0dd25 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -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 diff --git a/pyrogram/client/methods/messages/__init__.py b/pyrogram/client/methods/messages/__init__.py index 147e225a..eaf6f7b0 100644 --- a/pyrogram/client/methods/messages/__init__.py +++ b/pyrogram/client/methods/messages/__init__.py @@ -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 diff --git a/pyrogram/client/methods/messages/send_dice.py b/pyrogram/client/methods/messages/send_dice.py new file mode 100644 index 00000000..c5f95d4b --- /dev/null +++ b/pyrogram/client/methods/messages/send_dice.py @@ -0,0 +1,94 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-2020 Dan +# +# 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 . + +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) + ) diff --git a/pyrogram/client/types/messages_and_media/__init__.py b/pyrogram/client/types/messages_and_media/__init__.py index e514fb8d..d2424043 100644 --- a/pyrogram/client/types/messages_and_media/__init__.py +++ b/pyrogram/client/types/messages_and_media/__init__.py @@ -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" ] diff --git a/pyrogram/client/types/messages_and_media/dice.py b/pyrogram/client/types/messages_and_media/dice.py new file mode 100644 index 00000000..c579a890 --- /dev/null +++ b/pyrogram/client/types/messages_and_media/dice.py @@ -0,0 +1,44 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-2020 Dan +# +# 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 . + +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) diff --git a/pyrogram/client/types/messages_and_media/message.py b/pyrogram/client/types/messages_and_media/message.py index 897cf345..f566849b 100644 --- a/pyrogram/client/types/messages_and_media/message.py +++ b/pyrogram/client/types/messages_and_media/message.py @@ -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,