Skip to content

Commit b5a78ed

Browse files
committed
Update examples
1 parent 4965e0b commit b5a78ed

13 files changed

Lines changed: 84 additions & 252 deletions

examples/README.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@ You can start with [hello_world.py](https://github.com/pyrogram/pyrogram/blob/ma
55
with the more advanced examples.
66

77
Every script is working right away (provided you correctly set up your credentials), meaning
8-
you can simply copy-paste and run, the only things you have to change are the target chats (username, id) and file paths for
9-
sending media (photo, video, ...).
8+
you can simply copy-paste and run, the only things you have to change are your session names and the target chats
109

11-
- [**hello_world.py**](https://github.com/pyrogram/pyrogram/blob/master/examples/hello_world.py)
10+
- [**echo_bot.py**](https://github.com/pyrogram/pyrogram/blob/master/examples/echo_bot.py)
1211
- [**get_history.py**](https://github.com/pyrogram/pyrogram/blob/master/examples/get_history.py)
1312
- [**get_participants.py**](https://github.com/pyrogram/pyrogram/blob/master/examples/get_participants.py)
1413
- [**get_participants2.py**](https://github.com/pyrogram/pyrogram/blob/master/examples/get_participants2.py)
14+
- [**hello_world.py**](https://github.com/pyrogram/pyrogram/blob/master/examples/hello_world.py)
1515
- [**inline_bots.py**](https://github.com/pyrogram/pyrogram/blob/master/examples/inline_bots.py)
16-
- [**updates.py**](https://github.com/pyrogram/pyrogram/blob/master/examples/updates.py)
17-
- [**simple_echo.py**](https://github.com/pyrogram/pyrogram/blob/master/examples/simple_echo.py)
18-
- [**advanced_echo.py**](https://github.com/pyrogram/pyrogram/blob/master/examples/advanced_echo.py)
19-
- [**advanced_echo2.py**](https://github.com/pyrogram/pyrogram/blob/master/examples/advanced_echo2.py)
16+
- [**raw_update_handler.py**](https://github.com/pyrogram/pyrogram/blob/master/examples/raw_update_handler.py)
2017
- [**welcome_bot.py**](https://github.com/pyrogram/pyrogram/blob/master/examples/welcome_bot.py)

examples/advanced_echo.py

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

examples/advanced_echo2.py

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

examples/echo_bot.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from pyrogram import Client, Filters
2+
3+
"""This simple echo bot replies to every private text message"""
4+
5+
app = Client("my_account")
6+
7+
8+
@app.on_message(Filters.text & Filters.private)
9+
def echo(client, message):
10+
client.send_message(
11+
message.chat.id, message.text,
12+
reply_to_message_id=message.message_id
13+
)
14+
15+
16+
app.start()
17+
app.idle()

examples/get_history.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
from pyrogram.api import functions
55
from pyrogram.api.errors import FloodWait
66

7-
client = Client("example")
8-
client.start()
7+
app = Client("my_account")
8+
app.start()
99

1010
target = "me" # "me" refers to your own chat (Saved Messages)
1111
history = [] # List that will contain all the messages of the target chat
@@ -14,9 +14,9 @@
1414

1515
while True:
1616
try:
17-
messages = client.send(
17+
messages = app.send(
1818
functions.messages.GetHistory(
19-
client.resolve_peer(target),
19+
app.resolve_peer(target),
2020
0, 0, offset, limit, 0, 0, 0
2121
)
2222
)
@@ -31,7 +31,7 @@
3131
history.extend(messages.messages)
3232
offset += limit
3333

34-
client.stop()
34+
app.stop()
3535

3636
# Now the "history" list contains all the messages sorted by date in
3737
# descending order (from the most recent to the oldest one)

examples/get_participants.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
from pyrogram.api import functions, types
55
from pyrogram.api.errors import FloodWait
66

7-
client = Client("example")
8-
client.start()
7+
app = Client("my_account")
8+
app.start()
99

1010
target = "username" # Target channel/supergroup
1111
users = [] # List that will contain all the users of the target chat
@@ -14,9 +14,9 @@
1414

1515
while True:
1616
try:
17-
participants = client.send(
17+
participants = app.send(
1818
functions.channels.GetParticipants(
19-
channel=client.resolve_peer(target),
19+
channel=app.resolve_peer(target),
2020
filter=types.ChannelParticipantsSearch(""), # Filter by empty string (search for all)
2121
offset=offset,
2222
limit=limit,
@@ -35,6 +35,6 @@
3535
users.extend(participants.users)
3636
offset += limit
3737

38-
client.stop()
38+
app.stop()
3939

4040
# Now the "users" list contains all the members of the target chat

examples/get_participants2.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
as some user names may not contain ascii letters at all.
1616
"""
1717

18-
client = Client("example")
19-
client.start()
18+
app = Client("my_account")
19+
app.start()
2020

2121
target = "username" # Target channel/supergroup username or id
2222
users = {} # To ensure uniqueness, users will be stored in a dictionary with user_id as key
@@ -31,9 +31,9 @@
3131

3232
while True:
3333
try:
34-
participants = client.send(
34+
participants = app.send(
3535
functions.channels.GetParticipants(
36-
channel=client.resolve_peer(target),
36+
channel=app.resolve_peer(target),
3737
filter=types.ChannelParticipantsSearch(q),
3838
offset=offset,
3939
limit=limit,
@@ -60,4 +60,4 @@
6060

6161
print("Total users: {}".format(len(users)))
6262

63-
client.stop()
63+
app.stop()

examples/hello_world.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from pyrogram import Client
22

3+
"""This example demonstrates a simple API methods usage"""
4+
35
# Create a new Client
46
app = Client("my_account")
57

examples/inline_bots.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
from pyrogram import Client
22

33
# Create a new Client
4-
client = Client("example")
4+
app = Client("my_account")
55

66
# Start the Client
7-
client.start()
7+
app.start()
88

99
# Get bot results for "Fuzz Universe" from the inline bot @vid
10-
bot_results = client.get_inline_bot_results("vid", "Fuzz Universe")
10+
bot_results = app.get_inline_bot_results("vid", "Fuzz Universe")
1111
# Send the first result (bot_results.results[0]) to your own chat (Saved Messages)
12-
client.send_inline_bot_result("me", bot_results.query_id, bot_results.results[0].id)
12+
app.send_inline_bot_result("me", bot_results.query_id, bot_results.results[0].id)
1313

1414
# Stop the client
15-
client.stop()
15+
app.stop()

examples/raw_update_handler.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from pyrogram import Client
2+
3+
"""This example shows how to handle raw updates"""
4+
5+
app = Client("my_account")
6+
7+
8+
@app.on_raw_update()
9+
def raw(client, update, users, chats):
10+
print(update)
11+
12+
13+
app.start()
14+
app.idle()

0 commit comments

Comments
 (0)