Replace code examples with links to the example files on github
This commit is contained in:
parent
03916a4792
commit
4a173500dd
@ -36,100 +36,6 @@ connection.
|
||||
Examples
|
||||
--------
|
||||
|
||||
- Simple Echo example for **Private Messages**:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from pyrogram.api import types
|
||||
|
||||
def update_handler(client, update, users, chats):
|
||||
if isinstance(update, types.UpdateNewMessage): # Filter by UpdateNewMessage (PM and Groups)
|
||||
message = update.message # type: types.Message
|
||||
|
||||
if isinstance(message, types.Message): # Filter by Message to exclude MessageService and MessageEmpty
|
||||
if isinstance(message.to_id, types.PeerUser): # Private Messages (Message from user)
|
||||
client.send_message(message.from_id, message.message, reply_to_message_id=message.id)
|
||||
|
||||
|
||||
|
||||
- Advanced Echo example for both **Private Messages** and **Basic Groups** (with mentions).
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from pyrogram.api import types
|
||||
|
||||
def update_handler(client, update, users, chats):
|
||||
if isinstance(update, types.UpdateNewMessage): # Filter by UpdateNewMessage (PM and Chats)
|
||||
message = update.message
|
||||
|
||||
if isinstance(message, types.Message): # Filter by Message to exclude MessageService and MessageEmpty
|
||||
if isinstance(message.to_id, types.PeerUser): # Private Messages
|
||||
text = '[{}](tg://user?id={}) said "{}" to me ([{}](tg://user?id={}))'.format(
|
||||
users[message.from_id].first_name,
|
||||
users[message.from_id].id,
|
||||
message.message,
|
||||
users[message.to_id.user_id].first_name,
|
||||
users[message.to_id.user_id].id
|
||||
)
|
||||
|
||||
client.send_message(
|
||||
message.from_id, # Send the message to the private chat (from_id)
|
||||
text,
|
||||
reply_to_message_id=message.id
|
||||
)
|
||||
else: # Group chats
|
||||
text = '[{}](tg://user?id={}) said "{}" in **{}** group'.format(
|
||||
users[message.from_id].first_name,
|
||||
users[message.from_id].id,
|
||||
message.message,
|
||||
chats[message.to_id.chat_id].title
|
||||
)
|
||||
|
||||
client.send_message(
|
||||
message.to_id, # Send the message to the group chat (to_id)
|
||||
text,
|
||||
reply_to_message_id=message.id
|
||||
)
|
||||
|
||||
- Advanced Echo example for **Supergroups** (with mentions):
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from pyrogram.api import types
|
||||
|
||||
def update_handler(client, update, users, chats):
|
||||
if isinstance(update, types.UpdateNewChannelMessage): # Filter by UpdateNewChannelMessage (Channels/Supergroups)
|
||||
message = update.message
|
||||
|
||||
if isinstance(message, types.Message): # Filter by Message to exclude MessageService and MessageEmpty
|
||||
if chats[message.to_id.channel_id].megagroup: # Only handle messages from Supergroups not Channels
|
||||
text = '[{}](tg://user?id={}) said "{}" in **{}** supergroup'.format(
|
||||
users[message.from_id].first_name,
|
||||
users[message.from_id].id,
|
||||
message.message,
|
||||
chats[message.to_id.channel_id].title
|
||||
)
|
||||
|
||||
client.send_message(
|
||||
message.to_id,
|
||||
text,
|
||||
reply_to_message_id=message.id
|
||||
)
|
||||
|
||||
.. warning::
|
||||
The Advanced Examples above will make you reply to **all** new messages in private chats and in every single
|
||||
group/supergroup you are in. Make sure you add an extra check to filter them:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
# Filter Groups by ID
|
||||
if message.to_id.chat_id == MY_GROUP_ID:
|
||||
...
|
||||
|
||||
# Filter Supergroups by ID
|
||||
if message.to_id.channel_id == MY_SUPERGROUP_ID:
|
||||
...
|
||||
|
||||
# Filter Supergroups by Username
|
||||
if chats[message.to_id.channel_id].username == MY_SUPERGROUP_USERNAME:
|
||||
...
|
||||
- `Simple Echo <https://github.com/pyrogram/pyrogram/blob/master/examples/simple_echo.py>`_
|
||||
- `Advanced Echo <https://github.com/pyrogram/pyrogram/blob/master/examples/advanced_echo.py>`_
|
||||
- `Advanced Echo 2 <https://github.com/pyrogram/pyrogram/blob/master/examples/advanced_echo2.py>`_
|
||||
|
Loading…
Reference in New Issue
Block a user