From 3de26b3234cd1ef20a2a2f38348bacbc77af3d75 Mon Sep 17 00:00:00 2001 From: wulan17 Date: Sat, 25 Nov 2023 20:04:46 +0700 Subject: [PATCH] Pyrofork: Add Giveaway media type Signed-off-by: wulan17 --- compiler/docs/compiler.py | 1 + pyrogram/enums/message_media_type.py | 3 + pyrogram/types/messages_and_media/__init__.py | 3 +- pyrogram/types/messages_and_media/giveaway.py | 86 +++++++++++++++++++ pyrogram/types/messages_and_media/message.py | 10 +++ 5 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 pyrogram/types/messages_and_media/giveaway.py diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index 6a0397a50..3ec9fb78a 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -446,6 +446,7 @@ def get_title_list(s: str) -> list: Sticker StickerSet Game + Giveaway MessageStory WebPage WebPageEmpty diff --git a/pyrogram/enums/message_media_type.py b/pyrogram/enums/message_media_type.py index 1bd8c5a9e..22d45be57 100644 --- a/pyrogram/enums/message_media_type.py +++ b/pyrogram/enums/message_media_type.py @@ -69,5 +69,8 @@ class MessageMediaType(AutoName): GAME = auto() "Game media" + GIVEAWAY = auto() + "Giveaway media" + STORY = auto() "Forwarded story media" diff --git a/pyrogram/types/messages_and_media/__init__.py b/pyrogram/types/messages_and_media/__init__.py index b68de927d..6fc56931a 100644 --- a/pyrogram/types/messages_and_media/__init__.py +++ b/pyrogram/types/messages_and_media/__init__.py @@ -22,6 +22,7 @@ from .dice import Dice from .document import Document from .game import Game +from .giveaway import Giveaway from .location import Location from .message import Message from .message_entity import MessageEntity @@ -51,7 +52,7 @@ from .exported_story_link import ExportedStoryLink __all__ = [ - "Animation", "Audio", "Contact", "Document", "Game", "Location", "Message", "MessageEntity", "Photo", "Thumbnail", + "Animation", "Audio", "Contact", "Document", "Game", "Giveaway", "Location", "Message", "MessageEntity", "Photo", "Thumbnail", "StrippedThumbnail", "Poll", "PollOption", "Sticker", "StickerSet", "Venue", "Video", "VideoNote", "Voice", "WebPage", "WebPageEmpty", "WebPagePreview", "Dice", "Reaction", "WebAppData", "MessageReactions", "MessageStory", "Story", "StoryDeleted", "StorySkipped", "StoryViews", "StoriesPrivacyRules", "ExportedStoryLink" ] diff --git a/pyrogram/types/messages_and_media/giveaway.py b/pyrogram/types/messages_and_media/giveaway.py new file mode 100644 index 000000000..8a1abce1d --- /dev/null +++ b/pyrogram/types/messages_and_media/giveaway.py @@ -0,0 +1,86 @@ +# Pyrofork - Telegram MTProto API Client Library for Python +# Copyright (C) 2022-present Mayuri-Chan +# +# This file is part of Pyrofork. +# +# Pyrofork 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. +# +# Pyrofork 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 Pyrofork. If not, see . + +import pyrogram + +from datetime import datetime +from pyrogram import raw, types, utils +from ..object import Object +from typing import List + + +class Giveaway(Object): + """A giveaway. + + Parameters: + channel_ids (List of ``int``): + List Unique identifier of the channel(s) which host the giveaway. + + quantity (``int``): + Quantity of the giveaway prize. + + months (``int``): + How long the telegram premium last (in month). + + expire_date (:py:obj:`~datetime.datetime`): + Date the giveaway winner(s) will be choosen. + + new_subscribers (``bool``): + True, if the giveaway only for new subscribers. + + countries_iso2 (List of ``str``, *optional*): + List of ISO country codes which eligible to join the giveaway. + """ + + def __init__( + self, + *, + client: "pyrogram.Client" = None, + channel_ids: List[int], + quantity: int, + months: int, + expire_date: datetime, + new_subscribers : bool, + countries_iso2: List[str] = None + ): + super().__init__(client) + + self.channel_ids = channel_ids + self.quantity = quantity + self.months = months + self.expire_date = expire_date + self.new_subscribers = new_subscribers + self.countries_iso2 = countries_iso2 + + @staticmethod + def _parse(client, message: "raw.types.Message") -> "Giveaway": + giveaway: "raw.types.MessageMediaGiveaway" = message.media + channel_ids = [] + for raw_channel_id in giveaway.channels: + channel_id = utils.get_channel_id(raw_channel_id) + channel_ids.append(channel_id) + + return Giveaway( + channel_ids=channel_ids, + quantity=giveaway.quantity, + months=giveaway.months, + expire_date=utils.timestamp_to_datetime(giveaway.until_date), + new_subscribers=giveaway.only_new_subscribers, + countries_iso2=giveaway.countries_iso2 if len(giveaway.countries_iso2) > 0 else None, + client=client + ) diff --git a/pyrogram/types/messages_and_media/message.py b/pyrogram/types/messages_and_media/message.py index 8339ac850..45fdcce3d 100644 --- a/pyrogram/types/messages_and_media/message.py +++ b/pyrogram/types/messages_and_media/message.py @@ -191,6 +191,9 @@ class Message(Object, Update): game (:obj:`~pyrogram.types.Game`, *optional*): Message is a game, information about the game. + giveaway (:obj:`~pyrogram.types.Giveaway`, *optional*): + Message is a giveaway, information about the giveaway. + story (:obj:`~pyrogram.types.MessageStory`, *optional*): Message is a forwarded story, information about the forwarded story. @@ -402,6 +405,7 @@ def __init__( sticker: "types.Sticker" = None, animation: "types.Animation" = None, game: "types.Game" = None, + giveaway: "types.Giveaway" = None, story: "types.MessageStory" = None, video: "types.Video" = None, voice: "types.Voice" = None, @@ -495,6 +499,7 @@ def __init__( self.sticker = sticker self.animation = animation self.game = game + self.giveaway = giveaway self.story = story self.video = video self.voice = voice @@ -800,6 +805,7 @@ async def _parse( contact = None venue = None game = None + giveaway = None story = None audio = None voice = None @@ -833,6 +839,9 @@ async def _parse( elif isinstance(media, raw.types.MessageMediaGame): game = types.Game._parse(client, message) media_type = enums.MessageMediaType.GAME + elif isinstance(media, raw.types.MessageMediaGiveaway): + giveaway = types.Giveaway._parse(client, message) + media_type = enums.MessageMediaType.GIVEAWAY elif isinstance(media, raw.types.MessageMediaStory): story = types.MessageStory._parse(media) media_type = enums.MessageMediaType.STORY @@ -964,6 +973,7 @@ async def _parse( voice=voice, animation=animation, game=game, + giveaway=giveaway, story=story, video=video, video_note=video_note,