Skip to content

Commit 88be6c0

Browse files
committed
Add documentation for buttons
1 parent de1081a commit 88be6c0

File tree

8 files changed

+846
-172
lines changed

8 files changed

+846
-172
lines changed

docs/api/bot.rst

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,43 @@ components.
269269

270270
Added the ``hidden`` argument.
271271

272+
.. py:decoratormethod:: callback(name)
273+
274+
This decorator adds an handler for the callback with the provided name.
275+
See the chapter about :ref:`buttons and callbacks <buttons>` for more
276+
information about them.
277+
278+
You can :ref:`request the following arguments <bot-structure-hooks-args>`
279+
in the decorated function:
280+
281+
* **query**: the received :py:class:`~botogram.CallbackQuery`
282+
283+
* **chat**: the :py:class:`~botogram.Chat` from which the callback query
284+
was sent
285+
286+
* **message**: the :py:class:`~botogram.Message` related to the callback
287+
query
288+
289+
* **data**: the custom information provided by you along with the call
290+
291+
.. code-block:: python
292+
293+
@bot.command("greeter")
294+
def greeter_command(chat, message):
295+
"""Say hi to the user"""
296+
btns = botogram.Buttons()
297+
btns[0].callback("Click me", "say-hi", message.sender.name)
298+
299+
chat.send("Click the button below", attach=btns)
300+
301+
@bot.callback("say-hi")
302+
def say_hi_callback(query, data):
303+
query.notify("Hi " + data)
304+
305+
:param str name: the name of the callback
306+
307+
.. versionadded:: 0.4
308+
272309
.. py:decoratormethod:: chat_unavailable
273310
274311
The decorated function is called when you try to send a message to a chat

docs/api/buttons.rst

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
.. Copyright (c) 2015-2017 The Botogram Authors (see AUTHORS)
2+
Documentation released under the MIT license (see LICENSE)
3+
4+
.. _api-buttons:
5+
6+
=========================
7+
Buttons and callbacks API
8+
=========================
9+
10+
This page contains the documentation for the APIs related to buttons and
11+
callbacks. See the :ref:`narrative chapter about them <buttons>` for more
12+
information about them.
13+
14+
15+
.. py:class:: botogram.Buttons
16+
17+
This class allows you to build buttons to attach to messages. You must use
18+
it as a list, accessing the various rows with the square brackets.
19+
20+
Each element of this simulated list is automatically created the first time
21+
you access it, and it's an instance of :py:class:`~botogram.ButtonsRow`: you
22+
can use that object to actually populate the buttons. Check out that class'
23+
documentation for all the buttons you can add.
24+
25+
Then, you can send the buttons by attaching them to an outgoing message, as
26+
shown in the example below. You can also reuse the same instance for
27+
multiple messages.
28+
29+
.. code-block:: python
30+
31+
@bot.command("example")
32+
def example_command(chat, message, args):
33+
"""Show some example sites"""
34+
btns = botogram.Buttons()
35+
btns[0].url("Visit example.com", "http://www.example.com")
36+
btns[1].url("example.net", "http://www.example.net")
37+
btns[1].url("example.org", "http://www.example.org")
38+
39+
chat.send("Check out some example sites!", attach=btns)
40+
41+
.. versionadded:: 0.4
42+
43+
44+
.. py:class:: botogram.ButtonsRow
45+
46+
This class represents a row in a set of buttons: you can use it to add the
47+
individual buttons to it. You should not create an instance of this class
48+
directly, but you should get them through the :py:class:`~botogram.Buttons`
49+
class.
50+
51+
.. versionadded:: 0.4
52+
53+
.. py:method:: callback(label, callback, [data=None])
54+
55+
Add a new button to the row, which will call the corresponding callback
56+
when pressed by the user. You need to provide the label of the button,
57+
the name of the callback and an optional string of data, which will be
58+
passed to the callback.
59+
60+
Due to limitations in the Telegram Bot API, you can provide in the
61+
*data* argument a maximum of 32 bytes of text: an exception will be
62+
raised if you provide more.
63+
64+
The information contained in the callback will automatically be signed by
65+
botogram, to prevent tampering, but the data is **not** encrypted: an
66+
user with a special client might be able to see it, without being able to
67+
change it. See :ref:`the security section <buttons-security>` for more
68+
details.
69+
70+
.. code-block:: python
71+
72+
btns = botogram.Buttons()
73+
btns[0].callback("Commit changes", "commit")
74+
btns[0].callback("Checkout master", "checkout", "master")
75+
76+
:param str label: The label of the button shown to the user
77+
:param str callback: The internal name of the callback
78+
:param str data: Extra data provided to the callback (max 32 bytes)
79+
80+
.. py:method:: url(label, url)
81+
82+
Add a new button to the row, which will ask the user if they want to open
83+
the URL you provided. If they agree, the url will be opened in the user's
84+
browser. You need to provide the label of the button and the URL.
85+
86+
.. code-block:: python
87+
88+
btns = botogram.Buttons()
89+
btns[0].url("See example.com", "http://www.example.com")
90+
91+
:param str label: The label of the button shown to the user
92+
:param str url: The URL the user should open when the button is pressed
93+
94+
.. py:method:: switch_inline_query(label, [query="", current_chat=False])
95+
96+
Add a new button to the row, which will switch the user to the inline
97+
query mode of the bot. You need to provide the label of the button, and
98+
optionally the query that should be prefilled in the user's search field.
99+
100+
If *current_chat* is False, the user will be asked in which chat the
101+
inline query should be opened. If you set it to True, the inline query
102+
will be opened in the current chat.
103+
104+
.. code-block:: python
105+
106+
btns = botogram.Buttons()
107+
btns[0].switch_inline_query("Show pictures of cats to your friends!", "cats")
108+
109+
:param str label: The label of the button shown to the user
110+
:param str query: Default query provided to the user
111+
:param bool current_chat: Open in the current chat instead of asking the
112+
user to select one
113+
114+
115+
.. py:class:: botogram.CallbackQuery
116+
117+
This class represents a callback query received from Telegram. It contains
118+
all the information you need about it, and allows you to respond to it.
119+
120+
.. versionadded:: 0.4
121+
122+
.. py:attribute:: id
123+
124+
The unique ID of this callback query. You might need to use it if you
125+
interact with the Bot API directly.
126+
127+
.. py:attribute:: sender
128+
129+
The :py:class:`~botogram.User` who sent the query.
130+
131+
.. py:attribute:: message
132+
133+
The :py:class:`~botogram.Message` with the button that originated the
134+
callback.
135+
136+
.. py:attribute:: chat_instance
137+
138+
An unique string identifying the chat where the message with the button
139+
that called the callback is.
140+
141+
.. py:attribute:: inline_message_id
142+
143+
An unique string identifying the inline message with the button that
144+
originated the callback.
145+
146+
*This attribute can be None if it's not provided by Telegram.*
147+
148+
.. py:attribute:: game_short_name
149+
150+
The short name of the Telegram Game that the user requested to play.
151+
152+
*This attribute can be None if it's not provided by Telegram.*
153+
154+
.. py:method:: notify(text, [alert=False, cache=0])
155+
156+
Send a notification to the user that pressed the button originating the
157+
callback. You need to provide the text of the notification and,
158+
optionally, for how long the action will be cached by the client.
159+
160+
If *alert* is True, the user will be shown an alert window with the
161+
message. Otherwise, the message will be displayed as the client likes,
162+
for example as a toast.
163+
164+
.. code-block:: python
165+
166+
@bot.callback("say-hi")
167+
def say_hi_callback(query):
168+
query.notify("Hi " + query.sender.name + "!")
169+
170+
:param str text: The content of the notification
171+
:param bool alert: Show the notification as an alert to the user
172+
:param int cache: How long the client should cache the response
173+
174+
.. py:method:: open_url(url, [cache=0])
175+
176+
Tell the user's client to open an URL in the browser. This action is
177+
currently restricted only to the bots that agreed the Telegram Games
178+
Terms of Service: check out `the Telegram documentation`_ for more
179+
information about games.
180+
181+
You need to provide the URL of the page, and optionally how long you want
182+
the client to cache the action.
183+
184+
.. code-block:: python
185+
186+
@bot.callback("open-game")
187+
def open_game(query):
188+
query.open_url("http://game.example.com")
189+
190+
:param str url: The URL you want the user to open
191+
:param int cache: How long the client should cache the response
192+
193+
.. _the Telegram documentation: https://core.telegram.org/bots/api#games
194+
195+
.. py:method:: open_private_chat(start_arg, [cache=0])
196+
197+
Tell the user's client to open a private chat with the bot, and to switch
198+
to it. You need to provide a non-empty start argument, which will be
199+
appended to the ``/start`` command the client will send (but it won't be
200+
displayed to the user). Optionally, you can provide how long you want the
201+
client to cache the action.
202+
203+
.. code-block:: python
204+
205+
@bot.open("show-help")
206+
def show_help(query):
207+
query.open_private_chat("show-help-to-the-user")
208+
209+
@bot.command("start")
210+
def start_command(chat, message, args):
211+
if len(args) == 1 and args[0] == "show-help-to-the-user":
212+
chat.send("This is the help message of the bot.")
213+
else:
214+
chat.send("Hi! I'm a bot")
215+
216+
:param str start_arg: The argument to provide to ``/start``
217+
:param int cache: How long the client should cache the response

docs/api/components.rst

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,49 @@ about how to create them in the ":ref:`custom-components`" chapter.
253253

254254
Added the ``hidden`` argument.
255255

256+
.. py:method:: add_callback(name, func)
257+
258+
This method adds an handler for the callback with the provided name.
259+
See the chapter about :ref:`buttons and callbacks <buttons>` for more
260+
information about them.
261+
262+
You can :ref:`request the following arguments <bot-structure-hooks-args>`
263+
in the provided function:
264+
265+
* **query**: the received :py:class:`~botogram.CallbackQuery`
266+
267+
* **chat**: the :py:class:`~botogram.Chat` from which the callback query
268+
was sent
269+
270+
* **message**: the :py:class:`~botogram.Message` related to the callback
271+
query
272+
273+
* **data**: the custom information provided by you along with the call
274+
275+
.. code-block:: python
276+
277+
class GreeterComponent(botogram.Component):
278+
component_name = "greeter"
279+
280+
def __init__(self):
281+
self.add_command("greeter", self.command)
282+
self.add_callback("say-hi", self.say_hi)
283+
284+
def greeter(self, chat, message):
285+
"""Say hi to the user"""
286+
btns = botogram.Buttons()
287+
btns[0].callback("Click me", "say-hi", message.sender.name)
288+
289+
chat.send("Click the button below", attach=btns)
290+
291+
def say_hi(self, query, data):
292+
query.notify("Hi " + data)
293+
294+
:param str name: the name of the callback
295+
:param callable func: The function you want to use
296+
297+
.. versionadded:: 0.4
298+
256299
.. py:method:: add_chat_unavailable_hook(func)
257300
258301
The provided function is called when you try to send a message to a chat

docs/api/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ the development will be made trying to not break anything.
1616

1717
utility
1818
bot
19+
buttons
1920
channels
2021
components
2122
telegram

0 commit comments

Comments
 (0)