Skip to content

Commit b47591e

Browse files
committed
Turn examples asynchronous
1 parent ecc90ca commit b47591e

File tree

151 files changed

+619
-858
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

151 files changed

+619
-858
lines changed

compiler/docs/compiler.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,6 @@ def get_title_list(s: str) -> list:
534534
Chat.promote_member
535535
Chat.get_member
536536
Chat.get_members
537-
Chat.iter_members
538537
Chat.add_members
539538
Chat.join
540539
Chat.leave

docs/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ help:
2020
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
2121

2222
lhtml: # live html
23-
sphinx-autobuild --host $(shell ifconfig | grep "inet " | grep -v 127.0.0.1 | cut -d\ -f2) -b html "$(SOURCEDIR)" "$(BUILDDIR)/html" $(SPHINXOPTS)
23+
sphinx-autobuild --host $(shell ifconfig | grep "inet " | grep -v 127.0.0.1 | cut -d\ -f2) \
24+
--watch ../pyrogram \
25+
-b html "$(SOURCEDIR)" "$(BUILDDIR)/html" $(SPHINXOPTS)

docs/source/start/errors.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ Errors with Values
8989

9090
Exception objects may also contain some informative values. For example, ``FloodWait`` holds the amount of seconds you
9191
have to wait before you can try again, some other errors contain the DC number on which the request must be repeated on.
92-
The value is stored in the ``x`` attribute of the exception object:
92+
The value is stored in the ``value`` attribute of the exception object:
9393

9494
.. code-block:: python
9595
@@ -100,5 +100,5 @@ The value is stored in the ``x`` attribute of the exception object:
100100
try:
101101
... # Your code
102102
except FloodWait as e:
103-
await asyncio.sleep(e.x) # Wait "x" seconds before continuing
103+
await asyncio.sleep(e.value) # Wait N seconds before continuing
104104
...

docs/source/start/examples/bot_keyboards.rst

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,51 +12,57 @@ like send_audio(), send_document(), send_location(), etc...
1212
.. code-block:: python
1313
1414
from pyrogram import Client
15-
from pyrogram.types import ReplyKeyboardMarkup, InlineKeyboardMarkup, InlineKeyboardButton
15+
from pyrogram.types import (ReplyKeyboardMarkup, InlineKeyboardMarkup,
16+
InlineKeyboardButton)
1617
1718
# Create a client using your bot token
1819
app = Client("my_bot", bot_token="123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11")
1920
20-
with app:
21-
app.send_message(
22-
"me", # Edit this
23-
"This is a ReplyKeyboardMarkup example",
24-
reply_markup=ReplyKeyboardMarkup(
25-
[
26-
["A", "B", "C", "D"], # First row
27-
["E", "F", "G"], # Second row
28-
["H", "I"], # Third row
29-
["J"] # Fourth row
30-
],
31-
resize_keyboard=True # Make the keyboard smaller
32-
)
33-
)
34-
35-
app.send_message(
36-
"me", # Edit this
37-
"This is a InlineKeyboardMarkup example",
38-
reply_markup=InlineKeyboardMarkup(
39-
[
40-
[ # First row
41-
InlineKeyboardButton( # Generates a callback query when pressed
42-
"Button",
43-
callback_data="data"
44-
),
45-
InlineKeyboardButton( # Opens a web URL
46-
"URL",
47-
url="https://docs.pyrogram.org"
48-
),
21+
22+
async def main():
23+
async with app:
24+
await app.send_message(
25+
"me", # Edit this
26+
"This is a ReplyKeyboardMarkup example",
27+
reply_markup=ReplyKeyboardMarkup(
28+
[
29+
["A", "B", "C", "D"], # First row
30+
["E", "F", "G"], # Second row
31+
["H", "I"], # Third row
32+
["J"] # Fourth row
4933
],
50-
[ # Second row
51-
InlineKeyboardButton( # Opens the inline interface
52-
"Choose chat",
53-
switch_inline_query="pyrogram"
54-
),
55-
InlineKeyboardButton( # Opens the inline interface in the current chat
56-
"Inline here",
57-
switch_inline_query_current_chat="pyrogram"
58-
)
34+
resize_keyboard=True # Make the keyboard smaller
35+
)
36+
)
37+
38+
await app.send_message(
39+
"me", # Edit this
40+
"This is a InlineKeyboardMarkup example",
41+
reply_markup=InlineKeyboardMarkup(
42+
[
43+
[ # First row
44+
InlineKeyboardButton( # Generates a callback query when pressed
45+
"Button",
46+
callback_data="data"
47+
),
48+
InlineKeyboardButton( # Opens a web URL
49+
"URL",
50+
url="https://docs.pyrogram.org"
51+
),
52+
],
53+
[ # Second row
54+
InlineKeyboardButton( # Opens the inline interface
55+
"Choose chat",
56+
switch_inline_query="pyrogram"
57+
),
58+
InlineKeyboardButton( # Opens the inline interface in the current chat
59+
"Inline here",
60+
switch_inline_query_current_chat="pyrogram"
61+
)
62+
]
5963
]
60-
]
64+
)
6165
)
62-
)
66+
67+
68+
app.run(main())

docs/source/start/examples/callback_queries.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ It uses the @on_callback_query decorator to register a CallbackQueryHandler.
1212
1313
1414
@app.on_callback_query()
15-
def answer(client, callback_query):
16-
callback_query.answer(f"Button contains: '{callback_query.data}'", show_alert=True)
15+
async def answer(client, callback_query):
16+
await callback_query.answer(
17+
f"Button contains: '{callback_query.data}'",
18+
show_alert=True)
1719
1820
1921
app.run() # Automatically start() and idle()
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
echobot
2-
=======
1+
echo_bot
2+
========
33

44
This simple echo bot replies to every private text message.
55

@@ -14,8 +14,8 @@ It uses the ``@on_message`` decorator to register a ``MessageHandler`` and appli
1414
1515
1616
@app.on_message(filters.text & filters.private)
17-
def echo(client, message):
18-
message.reply(message.text)
17+
async def echo(client, message):
18+
await message.reply(message.text)
1919
2020
2121
app.run() # Automatically start() and idle()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
get_history
2+
===========
3+
4+
This example shows how to get the full message history of a chat, starting from the latest message.
5+
6+
.. code-block:: python
7+
8+
from pyrogram import Client
9+
10+
app = Client("my_account")
11+
12+
13+
async def main():
14+
async with app:
15+
# "me" refers to your own chat (Saved Messages)
16+
async for message in app.get_chat_history("me"):
17+
print(message)
18+
19+
20+
app.run(main())

docs/source/start/examples/get_chat_members.rst

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,16 @@ This example shows how to get all the members of a chat.
77
88
from pyrogram import Client
99
10+
# Target channel/supergroup
11+
TARGET = -100123456789
12+
1013
app = Client("my_account")
11-
target = "pyrogramchat" # Target channel/supergroup
1214
13-
with app:
14-
for member in app.iter_chat_members(target):
15-
print(member.user.first_name)
15+
16+
async def main():
17+
async with app:
18+
async for member in app.get_chat_members(TARGET):
19+
print(member)
20+
21+
22+
app.run(main())

docs/source/start/examples/get_dialogs.rst

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ This example shows how to get the full dialogs list (as user).
99
1010
app = Client("my_account")
1111
12-
with app:
13-
for dialog in app.iter_dialogs():
14-
print(dialog.chat.title or dialog.chat.first_name)
12+
13+
async def main():
14+
async with app:
15+
async for dialog in app.get_dialogs():
16+
print(dialog.chat.title or dialog.chat.first_name)
17+
18+
19+
app.run(main())

docs/source/start/examples/get_history.rst

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)