|
| 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 |
0 commit comments