1717# You should have received a copy of the GNU Lesser Public License
1818# along with this program. If not, see [http://www.gnu.org/licenses/].
1919"""This module contains an object that represents a Telegram InlineKeyboardButton."""
20- from telegram import KeyboardButton , InlineKeyboardButton , TelegramObject
21- from telegram .utils .binaryencoder import ZERO_CHAR3
20+ from telegram import InlineKeyboardButton , KeyboardButton , TelegramObject
21+ from telegram .flow .action import Action
22+ from telegram .utils .binaryencoder import insert_callback_id
2223
2324
2425class ActionButton (TelegramObject ):
@@ -69,40 +70,52 @@ class ActionButton(TelegramObject):
6970 """
7071
7172 def __init__ (self ,
72- caption ,
7373 action ,
74- view_data = None ):
74+ caption = None , # TODO DOCS: "will override..."
75+ view_data = None ,
76+ switch_inline = None ,
77+ switch_inline_current_chat = None ):
78+
7579 if caption is None :
76- raise ValueError ("Buttons without a text caption are not possible." )
80+ if isinstance (action , Action ):
81+ # noinspection PyProtectedMember
82+ if action ._caption :
83+ caption = action .get_caption (view_data )
84+ else :
85+ raise ValueError ("Neither this button nor its action have a caption. Pass the caption argument or "
86+ "set up your Action to have one." )
87+ else :
88+ raise ValueError ("If the action is {} and not an Action object, a caption must be supplied as "
89+ "argument to this ActionButton." .format (type (action )))
90+ else :
91+ caption = action .get_caption (view_data )
92+
7793 if len (caption ) > 108 :
7894 # We have a maximum button caption length of 128 characters until they are truncated
7995 # server-side. We will use the remaining 20 characters to encode a random ID.
8096 raise ValueError ("Button caption must not be longer than 108 characters." )
97+ if caption in (None , '' ):
98+ raise ValueError ("Buttons without a text caption are not possible." )
8199
82100 self .text = caption
101+
102+ if switch_inline and switch_inline_current_chat :
103+ raise ValueError ("Cannot set both switch_inline and switch_inline_current_chat." )
104+ self .switch_inline_query = switch_inline
105+ self .switch_inline_query_current_chat = switch_inline_current_chat
106+
83107 self ._action_id = action
84108 self ._callback = None
85109 self .callback_data = None
86110 self ._is_inline = None
87111
88112 self ._view_data = view_data
89113
90- @classmethod
91- def from_action (cls , action , view_data = None ):
92- caption = action .get_caption (view_data )
93- if caption in (None , '' ):
94- raise ValueError ("Buttons without a text caption are not possible." )
95- return cls (
96- caption = caption ,
97- action = action .id ,
98- view_data = view_data
99- )
100-
101114 def insert_callback (self , callback_manager ):
102115 callback = callback_manager .create_callback (
103116 action_id = self ._action_id ,
104117 data = self ._view_data ,
105- random_id = self ._is_inline
118+ random_id = self ._is_inline and not ( self . switch_inline_query or self . switch_inline_query_current_chat )
106119 )
107120
108121 self ._callback = callback
@@ -125,11 +138,31 @@ def to_dict(self):
125138 if not self ._callback :
126139 raise ValueError ("You need to call the insert_callback method before usage." )
127140
141+ if self .switch_inline_query or self .switch_inline_query_current_chat :
142+ if not self ._is_inline :
143+ raise ValueError ("switch_inline will not work with regular KeyboardButtons, "
144+ "use an InlineKeyboardMarkup instead." )
145+
128146 if self ._is_inline :
129147 self .__bases__ = (InlineKeyboardButton ,)
130- self .callback_data = self ._callback .id
131148 else :
132149 self .__bases__ = (KeyboardButton ,)
133- self .text = self .text + self ._callback .id + ZERO_CHAR3
150+
151+ if not self ._is_inline :
152+ # Regular KeyboardButton
153+ self .text = insert_callback_id (self .text , self ._callback .id )
154+ elif self .switch_inline_query :
155+ # InlineKeyboardButton with switch_inline
156+ self .switch_inline_query = insert_callback_id (self .switch_inline_query , self ._callback .id )
157+ elif self .switch_inline_query_current_chat :
158+ # InlineKeyboardButton with switch_inline_current_chat
159+ self .switch_inline_query_current_chat = insert_callback_id (self .switch_inline_query_current_chat ,
160+ self ._callback .id )
161+ else :
162+ # InlineKeyboardButton with callback_data
163+ self .callback_data = self ._callback .id
164+
165+ print (self .text , type (self ), ': ' ,
166+ self .switch_inline_query or self .switch_inline_query_current_chat or self .callback_data )
134167
135168 return super (ActionButton , self ).to_dict ()
0 commit comments