Skip to content

Commit

Permalink
Pyrofork: Add Giveaway media type
Browse files Browse the repository at this point in the history
Signed-off-by: wulan17 <[email protected]>
  • Loading branch information
wulan17 committed Nov 25, 2023
1 parent f4ba8c2 commit 3de26b3
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 1 deletion.
1 change: 1 addition & 0 deletions compiler/docs/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ def get_title_list(s: str) -> list:
Sticker
StickerSet
Game
Giveaway
MessageStory
WebPage
WebPageEmpty
Expand Down
3 changes: 3 additions & 0 deletions pyrogram/enums/message_media_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,8 @@ class MessageMediaType(AutoName):
GAME = auto()
"Game media"

GIVEAWAY = auto()
"Giveaway media"

STORY = auto()
"Forwarded story media"
3 changes: 2 additions & 1 deletion pyrogram/types/messages_and_media/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
]
86 changes: 86 additions & 0 deletions pyrogram/types/messages_and_media/giveaway.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Pyrofork - Telegram MTProto API Client Library for Python
# Copyright (C) 2022-present Mayuri-Chan <https://github.com/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 <http://www.gnu.org/licenses/>.

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
)
10 changes: 10 additions & 0 deletions pyrogram/types/messages_and_media/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -800,6 +805,7 @@ async def _parse(
contact = None
venue = None
game = None
giveaway = None
story = None
audio = None
voice = None
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -964,6 +973,7 @@ async def _parse(
voice=voice,
animation=animation,
game=game,
giveaway=giveaway,
story=story,
video=video,
video_note=video_note,
Expand Down

0 comments on commit 3de26b3

Please sign in to comment.