-
Notifications
You must be signed in to change notification settings - Fork 6k
Expand file tree
/
Copy path_reaction.py
More file actions
204 lines (157 loc) · 6.39 KB
/
_reaction.py
File metadata and controls
204 lines (157 loc) · 6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2024
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 Public License for more details.
#
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains objects that represents a Telegram ReactionType."""
from typing import TYPE_CHECKING, Final, Literal, Optional, Union
from telegram import constants
from telegram._telegramobject import TelegramObject
from telegram._utils import enum
from telegram._utils.types import JSONDict
if TYPE_CHECKING:
from telegram import Bot
class ReactionType(TelegramObject):
"""Base class for Telegram ReactionType Objects.
There exist :class:`telegram.ReactionTypeEmoji` and :class:`telegram.ReactionTypeCustomEmoji`.
.. versionadded:: 20.8
Args:
type (:obj:`str`): Type of the reaction. Can be
:attr:`~telegram.ReactionType.EMOJI` or :attr:`~telegram.ReactionType.CUSTOM_EMOJI`.
Attributes:
type (:obj:`str`): Type of the reaction. Can be
:attr:`~telegram.ReactionType.EMOJI` or :attr:`~telegram.ReactionType.CUSTOM_EMOJI`.
"""
__slots__ = ("type",)
EMOJI: Final[constants.ReactionType] = constants.ReactionType.EMOJI
""":const:`telegram.constants.ReactionType.EMOJI`"""
CUSTOM_EMOJI: Final[constants.ReactionType] = constants.ReactionType.CUSTOM_EMOJI
""":const:`telegram.constants.ReactionType.CUSTOM_EMOJI`"""
def __init__(
self,
type: Union[ # pylint: disable=redefined-builtin
Literal["emoji", "custom_emoji"], constants.ReactionType
],
*,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(api_kwargs=api_kwargs)
# Required by all subclasses
self.type: str = enum.get_member(constants.ReactionType, type, type)
self._freeze()
@classmethod
def de_json(cls, data: Optional[JSONDict], bot: "Bot") -> Optional["ReactionType"]:
"""See :meth:`telegram.TelegramObject.de_json`."""
data = cls._parse_data(data)
if not data:
return None
if cls is ReactionType and data.get("type") in [cls.EMOJI, cls.CUSTOM_EMOJI]:
reaction_type = data.pop("type")
if reaction_type == cls.EMOJI:
return ReactionTypeEmoji.de_json(data=data, bot=bot)
return ReactionTypeCustomEmoji.de_json(data=data, bot=bot)
return super().de_json(data=data, bot=bot)
class ReactionTypeEmoji(ReactionType):
"""
Represents a reaction with a normal emoji.
Objects of this class are comparable in terms of equality. Two objects of this class are
considered equal, if the :attr:`emoji` is equal.
.. versionadded:: 20.8
Args:
emoji (:obj:`str`): Reaction emoji. It can be one of
:const:`telegram.constants.ReactionEmoji`.
Attributes:
type (:obj:`str`): Type of the reaction,
always :tg-const:`telegram.ReactionType.EMOJI`.
emoji (:obj:`str`): Reaction emoji. It can be one of
:const:`telegram.constants.ReactionEmoji`.
"""
__slots__ = ("emoji",)
def __init__(
self,
emoji: str,
*,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(type=ReactionType.EMOJI, api_kwargs=api_kwargs)
with self._unfrozen():
self.emoji: str = emoji
self._id_attrs = (self.emoji,)
class ReactionTypeCustomEmoji(ReactionType):
"""
Represents a reaction with a custom emoji.
Objects of this class are comparable in terms of equality. Two objects of this class are
considered equal, if the :attr:`custom_emoji_id` is equal.
.. versionadded:: 20.8
Args:
custom_emoji_id (:obj:`str`): Custom emoji identifier.
Attributes:
type (:obj:`str`): Type of the reaction,
always :tg-const:`telegram.ReactionType.CUSTOM_EMOJI`.
custom_emoji_id (:obj:`str`): Custom emoji identifier.
"""
__slots__ = ("custom_emoji_id",)
def __init__(
self,
custom_emoji_id: str,
*,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(type=ReactionType.CUSTOM_EMOJI, api_kwargs=api_kwargs)
with self._unfrozen():
self.custom_emoji_id: str = custom_emoji_id
self._id_attrs = (self.custom_emoji_id,)
class ReactionCount(TelegramObject):
"""This class represents a reaction added to a message along with the number of times it was
added.
Objects of this class are comparable in terms of equality. Two objects of this class are
considered equal, if the :attr:`type` and :attr:`total_count` is equal.
.. versionadded:: 20.8
Args:
type (:class:`telegram.ReactionType`): Type of the reaction.
total_count (:obj:`int`): Number of times the reaction was added.
Attributes:
type (:class:`telegram.ReactionType`): Type of the reaction.
total_count (:obj:`int`): Number of times the reaction was added.
"""
__slots__ = (
"total_count",
"type",
)
def __init__(
self,
type: ReactionType, # pylint: disable=redefined-builtin
total_count: int,
*,
api_kwargs: Optional[JSONDict] = None,
):
super().__init__(api_kwargs=api_kwargs)
# Required
self.type: ReactionType = type
self.total_count: int = total_count
self._id_attrs = (
self.type,
self.total_count,
)
self._freeze()
@classmethod
def de_json(cls, data: Optional[JSONDict], bot: "Bot") -> Optional["ReactionCount"]:
"""See :meth:`telegram.TelegramObject.de_json`."""
data = cls._parse_data(data)
if not data:
return None
data["type"] = ReactionType.de_json(data.get("type"), bot)
return super().de_json(data=data, bot=bot)