You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.rst
+67-3Lines changed: 67 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -54,11 +54,13 @@ Table of contents
54
54
55
55
1. `API`_
56
56
57
-
2. `Logging`_
57
+
2. `The Updater class`_
58
+
59
+
3. `Logging`_
58
60
59
-
3. `Examples`_
61
+
4. `Examples`_
60
62
61
-
4. `Documentation`_
63
+
5. `Documentation`_
62
64
63
65
- `License`_
64
66
@@ -152,10 +154,72 @@ _`Getting started`
152
154
153
155
View the last release API documentation at: https://core.telegram.org/bots/api
154
156
157
+
------
158
+
_`The Updater class`
159
+
------
160
+
161
+
The ``Updater`` class is the new way to create bots with ``python-telegram-bot``. It provides an easy-to-use interface to the ``telegram.Bot`` by caring about getting new updates from telegram and forwarding them to the ``Dispatcher`` class. We can register handler functions in the ``Dispatcher`` to make our bot react to Telegram commands, messages and even arbitrary updates.
162
+
163
+
As with the old method, we'll need an Access Token. To generate an Access Token, we have to talk to `BotFather <https://telegram.me/botfather>`_ and follow a few simple steps (described `here <https://core.telegram.org/bots#botfather>`_).
164
+
165
+
First, we create an ``Updater`` object::
166
+
167
+
>>> from telegram import Updater
168
+
>>> updater = Updater(token='token')
169
+
170
+
For quicker access to the ``Dispatcher`` used by our ``Updater``, we can introduce it locally::
171
+
172
+
>>> dispatcher = updater.dispatcher
173
+
174
+
Now, we need to define a function that should process a specific type of update::
175
+
176
+
>>> def start(bot, update):
177
+
... bot.sendMessage(chat_id=update.message.chat_id, text="I'm a bot, please talk to me!")
178
+
179
+
We want this function to be called on a Telegram message that contains the ``/start`` command, so we need to register it in the dispatcher::
The last step is to tell the ``Updater`` to start working::
184
+
185
+
>>> updater.start_polling()
186
+
187
+
Our bot is now up and running (go ahead and try it)! It's not doing anything yet, besides answering to the ``/start`` command. Let's add another handler function and register it::
Let's add some functionality to our bot. We want to add the ``/caps`` command, that will take some text as parameter and return it in all caps. We can get the arguments that were passed to the command in the handler function simply by adding it to the parameter list::
Check out more examples in the `examples folder <https://github.com/leandrotoledo/python-telegram-bot/tree/master/examples>`_!
216
+
155
217
------
156
218
_`API`
157
219
------
158
220
221
+
Note: Using the ``Bot`` class directly is the 'old' method, but some of this is still important information, even if you're using the ``Updater`` class!
222
+
159
223
The API is exposed via the ``telegram.Bot`` class.
160
224
161
225
To generate an Access Token you have to talk to `BotFather <https://telegram.me/botfather>`_ and follow a few simple steps (described `here <https://core.telegram.org/bots#botfather>`_).
0 commit comments