forked from pyrogram/pyrogram
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyboard_button.py
More file actions
82 lines (68 loc) · 2.91 KB
/
Copy pathkeyboard_button.py
File metadata and controls
82 lines (68 loc) · 2.91 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
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-2019 Dan Tès <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 pyrogram.api.types import KeyboardButton as RawKeyboardButton
from pyrogram.api.types import KeyboardButtonRequestPhone, KeyboardButtonRequestGeoLocation
from ..pyrogram_type import PyrogramType
class KeyboardButton(PyrogramType):
"""This object represents one button of the reply keyboard.
For simple text buttons String can be used instead of this object to specify text of the button.
Optional fields are mutually exclusive.
Args:
text (``str``):
Text of the button. If none of the optional fields are used, it will be sent as a message when
the button is pressed.
request_contact (``bool``, *optional*):
If True, the user's phone number will be sent as a contact when the button is pressed.
Available in private chats only.
request_location (``bool``, *optional*):
If True, the user's current location will be sent when the button is pressed.
Available in private chats only.
"""
__slots__ = ["text", "request_contact", "request_location"]
def __init__(
self,
text: str,
request_contact: bool = None,
request_location: bool = None
):
super().__init__(None)
self.text = str(text)
self.request_contact = request_contact
self.request_location = request_location
@staticmethod
def read(o):
if isinstance(o, RawKeyboardButton):
return o.text
if isinstance(o, KeyboardButtonRequestPhone):
return KeyboardButton(
text=o.text,
request_contact=True
)
if isinstance(o, KeyboardButtonRequestGeoLocation):
return KeyboardButton(
text=o.text,
request_location=True
)
def write(self):
# TODO: Enforce optional args mutual exclusiveness
if self.request_contact:
return KeyboardButtonRequestPhone(text=self.text)
elif self.request_location:
return KeyboardButtonRequestGeoLocation(text=self.text)
else:
return RawKeyboardButton(text=self.text)