Skip to content

Commit 1be8ca9

Browse files
committed
Update docs with new content
1 parent 9a44c79 commit 1be8ca9

File tree

8 files changed

+295
-131
lines changed

8 files changed

+295
-131
lines changed

docs/source/faq.rst

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,14 @@ things:
155155
chats).
156156
- The chat id argument you passed is in form of a string; you have to convert it into an integer with ``int(chat_id)``.
157157

158+
UnicodeEncodeError: '<encoding>' codec can't encode …
159+
-----------------------------------------------------
160+
161+
Where ``<encoding>`` might be *ascii*, *cp932*, *charmap* or anything else other than **utf-8**. This error usually
162+
shows up when you try to print something and has very little to do with Pyrogram itself as it is strictly related to
163+
your own terminal. To fix it, either find a way to change the encoding settings of your terminal to UTF-8 or switch to a
164+
better one.
165+
158166
My verification code expires immediately!
159167
-----------------------------------------
160168

@@ -179,8 +187,20 @@ Having said that, here's a list of what Telegram definitely doesn't like:
179187
- Spam, sending unsolicited messages or adding people to unwanted groups and channels.
180188
- Virtual/VoIP and cheap real numbers, because they are relatively easy to get and likely used for spam/flood.
181189

182-
However, you might be right, and your account was deactivated/limited without any reason. This could happen because of
183-
mistakes by either the automatic systems or a moderator. In such cases you can kindly email Telegram at
190+
And here's a good explanation of how, probably, the system works:
191+
192+
.. raw:: html
193+
194+
<script
195+
async src="https://telegram.org/js/telegram-widget.js?5"
196+
data-telegram-post="PyrogramChat/69424"
197+
data-width="100%">
198+
</script>
199+
200+
.. centered:: Join the discussion at `@PyrogramChat <https://t.me/pyrogramchat>`_
201+
202+
However, you might be right, and your account was deactivated/limited without any good reason. This could happen because
203+
of mistakes by either the automatic systems or a moderator. In such cases you can kindly email Telegram at
184204
recover@telegram.org, contact `@smstelegram`_ on Twitter or use `this form`_.
185205

186206
Are there any secret easter eggs?

docs/source/glossary.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ general. Some words may as well link to dedicated articles in case the topic is
99
If you think something interesting could be added here, feel free to propose it by opening a `Feature Request`_.
1010

1111

12+
Terms
13+
-----
14+
1215
.. glossary::
16+
:sorted:
1317

1418
API
1519
Application Programming Interface: a set of methods, protocols and tools that make it easier to develop programs

docs/source/index.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ Meta
122122
:hidden:
123123
:caption: Topic Guides
124124

125-
topics/filters
125+
topics/use-filters
126+
topics/create-filters
126127
topics/more-on-updates
127128
topics/config-file
128129
topics/smart-plugins
@@ -134,6 +135,7 @@ Meta
134135
topics/proxy
135136
topics/bots-interaction
136137
topics/mtproto-vs-botapi
138+
topics/debugging
137139
topics/test-servers
138140
topics/advanced-usage
139141
topics/voice-calls

docs/source/start/invoking.rst

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,40 +24,40 @@ Making API method calls with Pyrogram is very simple. Here's an example we are g
2424
2525
app.stop()
2626
27-
Let's begin by importing the Client class from the Pyrogram package:
27+
#. Let's begin by importing the Client class from the Pyrogram package:
2828

29-
.. code-block:: python
29+
.. code-block:: python
3030
31-
from pyrogram import Client
31+
from pyrogram import Client
3232
33-
Now instantiate a new Client object, "my_account" is a session name of your choice:
33+
#. Now instantiate a new Client object, "my_account" is a session name of your choice:
3434

35-
.. code-block:: python
35+
.. code-block:: python
3636
37-
app = Client("my_account")
37+
app = Client("my_account")
3838
39-
To actually make use of any method, the client has to be started first:
39+
#. To actually make use of any method, the client has to be started first:
4040

41-
.. code-block:: python
41+
.. code-block:: python
4242
43-
app.start()
43+
app.start()
4444
45-
Now, you can call any method you like:
45+
#. Now, you can call any method you like:
4646

47-
.. code-block:: python
47+
.. code-block:: python
4848
49-
print(app.get_me()) # Print information about yourself
49+
print(app.get_me()) # Print information about yourself
5050
51-
# Send messages to yourself:
52-
app.send_message("me", "Hi!") # Text message
53-
app.send_location("me", 51.500729, -0.124583) # Location
54-
app.send_sticker("me", "CAADBAADyg4AAvLQYAEYD4F7vcZ43AI") # Sticker
51+
# Send messages to yourself:
52+
app.send_message("me", "Hi!") # Text message
53+
app.send_location("me", 51.500729, -0.124583) # Location
54+
app.send_sticker("me", "CAADBAADyg4AAvLQYAEYD4F7vcZ43AI") # Sticker
5555
56-
Finally, when done, simply stop the client:
56+
#. Finally, when done, simply stop the client:
5757

58-
.. code-block:: python
58+
.. code-block:: python
5959
60-
app.stop()
60+
app.stop()
6161
6262
Context Manager
6363
---------------

docs/source/start/updates.rst

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,36 +45,37 @@ arrives:
4545
4646
app.run()
4747
48-
Let's examine these four new pieces. First one: a callback function we defined which accepts two arguments -
49-
*(client, message)*. This will be the function that gets executed every time a new message arrives and Pyrogram will
50-
call that function by passing the client instance and the new message instance as argument.
48+
#. Let's examine these four new pieces. First one: a callback function we defined which accepts two arguments -
49+
*(client, message)*. This will be the function that gets executed every time a new message arrives and Pyrogram will
50+
call that function by passing the client instance and the new message instance as argument.
5151

52-
.. code-block:: python
52+
.. code-block:: python
5353
54-
def my_function(client, message):
55-
print(message)
54+
def my_function(client, message):
55+
print(message)
5656
57-
Second one: the :class:`~pyrogram.MessageHandler`. This object tells Pyrogram the function we defined above must only
58-
handle updates that are in form of a :class:`~pyrogram.Message`:
57+
#. Second one: the :class:`~pyrogram.MessageHandler`. This object tells Pyrogram the function we defined above must
58+
only handle updates that are in form of a :class:`~pyrogram.Message`:
5959

60-
.. code-block:: python
60+
.. code-block:: python
6161
62-
my_handler = MessageHandler(my_function)
62+
my_handler = MessageHandler(my_function)
6363
64-
Third: the method :meth:`~pyrogram.Client.add_handler`. This method is used to actually register the handler and let
65-
Pyrogram know it needs to be taken into consideration when new updates arrive and the internal dispatching phase begins.
64+
#. Third: the method :meth:`~pyrogram.Client.add_handler`. This method is used to actually register the handler and let
65+
Pyrogram know it needs to be taken into consideration when new updates arrive and the internal dispatching phase
66+
begins.
6667

67-
.. code-block:: python
68+
.. code-block:: python
6869
69-
app.add_handler(my_handler)
70+
app.add_handler(my_handler)
7071
71-
Last one, the :meth:`~pyrogram.Client.run` method. What this does is simply call :meth:`~pyrogram.Client.start` and a
72-
special method :meth:`~pyrogram.Client.idle` that keeps your main scripts alive until you press ``CTRL+C``; the client
73-
will be automatically stopped after that.
72+
#. Last one, the :meth:`~pyrogram.Client.run` method. What this does is simply call :meth:`~pyrogram.Client.start` and
73+
a special method :meth:`~pyrogram.Client.idle` that keeps your main scripts alive until you press ``CTRL+C``; the
74+
client will be automatically stopped after that.
7475

75-
.. code-block:: python
76+
.. code-block:: python
7677
77-
app.run()
78+
app.run()
7879
7980
Using Decorators
8081
----------------
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
Creating Filters
2+
================
3+
4+
Pyrogram already provides lots of built-in :class:`~pyrogram.Filters` to work with, but in case you can't find
5+
a specific one for your needs or want to build a custom filter by yourself (to be used in a different kind of handler,
6+
for example) you can use :meth:`~pyrogram.Filters.create`.
7+
8+
.. note::
9+
10+
At the moment, the built-in filters are intended to be used with the :class:`~pyrogram.MessageHandler` only.
11+
12+
Custom Filters
13+
--------------
14+
15+
An example to demonstrate how custom filters work is to show how to create and use one for the
16+
:class:`~pyrogram.CallbackQueryHandler`. Note that callback queries updates are only received by bots; create and
17+
:doc:`authorize your bot <../start/auth>`, then send a message with an inline keyboard to yourself. This allows you to
18+
test your filter by pressing the inline button:
19+
20+
.. code-block:: python
21+
22+
from pyrogram import InlineKeyboardMarkup, InlineKeyboardButton
23+
24+
app.send_message(
25+
"username", # Change this to your username or id
26+
"Pyrogram's custom filter test",
27+
reply_markup=InlineKeyboardMarkup(
28+
[[InlineKeyboardButton("Press me", b"pyrogram")]]
29+
)
30+
)
31+
32+
Basic Filters
33+
-------------
34+
35+
For this basic filter we will be using only the first two parameters of :meth:`~pyrogram.Filters.create`.
36+
37+
The code below creates a simple filter for hardcoded, static callback data. This filter will only allow callback queries
38+
containing "Pyrogram" as data, that is, the function *func* you pass returns True in case the callback query data
39+
equals to ``b"Pyrogram"``.
40+
41+
.. code-block:: python
42+
43+
static_data = Filters.create(
44+
name="StaticdData",
45+
func=lambda flt, callback_query: callback_query.data == b"Pyrogram"
46+
)
47+
48+
The ``lambda`` operator in python is used to create small anonymous functions and is perfect for this example, the same
49+
could be achieved with a normal function, but we don't really need it as it makes sense only inside the filter's scope:
50+
51+
.. code-block:: python
52+
53+
def func(flt, callback_query):
54+
return callback_query.data == b"Pyrogram"
55+
56+
static_data = Filters.create(
57+
name="StaticData",
58+
func=func
59+
)
60+
61+
The filter usage remains the same:
62+
63+
.. code-block:: python
64+
65+
@app.on_callback_query(static_data)
66+
def pyrogram_data(client, callback_query):
67+
client.answer_callback_query(callback_query.id, "it works!")
68+
69+
Filters with Arguments
70+
----------------------
71+
72+
A much cooler filter would be one that accepts "Pyrogram" or any other data as argument at usage time.
73+
A dynamic filter like this will make use of the third parameter of :meth:`~pyrogram.Filters.create`.
74+
75+
This is how a dynamic custom filter looks like:
76+
77+
.. code-block:: python
78+
79+
def dynamic_data(data):
80+
return Filters.create(
81+
name="DynamicData",
82+
func=lambda flt, callback_query: flt.data == callback_query.data,
83+
data=data # "data" kwarg is accessed with "filter.data"
84+
)
85+
86+
And its usage:
87+
88+
.. code-block:: python
89+
90+
@app.on_callback_query(dynamic_data(b"Pyrogram"))
91+
def pyrogram_data(client, callback_query):
92+
client.answer_callback_query(callback_query.id, "it works!")

0 commit comments

Comments
 (0)