2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-31 06:16:06 +00:00

Update examples

This commit is contained in:
Dan
2018-04-14 18:46:54 +02:00
parent 4965e0b4f8
commit b5a78ed1d4
13 changed files with 80 additions and 248 deletions

View File

@@ -1,52 +1,32 @@
from pyrogram import Client, Emoji
from pyrogram.api import types
from pyrogram import Client, Emoji, Filters
"""
This is the Welcome Bot in @PyrogramChat
The code is commented to help you understand each part
It also uses the Emoji module to easily add emojis in your text messages
It uses the Emoji module to easily add emojis in your text messages and Filters
to make it only work for specific messages in a specific chat
"""
# Your Supergroup ID
SUPERGROUP_ID = 1387666944
app = Client("my_account")
def update_handler(client, update, users, chats):
# Supergroup messages are contained in the "UpdateNewChannelMessage" update type
if isinstance(update, types.UpdateNewChannelMessage):
message = update.message
# When a user joins, a "MessageService" is received
if isinstance(message, types.MessageService):
# Check if the message is sent to your SUPERGROUP_ID
if message.to_id.channel_id == SUPERGROUP_ID:
# A "MessageService" contains the "action" field.
# The action for user joins is "MessageActionChatAddUser" if the user
# joined using the username, otherwise is "MessageActionChatJoinedByLink" if
# the user joined a private group by link
if isinstance(message.action, (types.MessageActionChatAddUser, types.MessageActionChatJoinedByLink)):
# Now send the welcome message. Extra info about a user (such as the first_name, username, ...)
# are contained in the users dictionary and can be accessed by the user ID
client.send_message(
SUPERGROUP_ID,
"{} Welcome to [Pyrogram](https://docs.pyrogram.ml/)'s "
"group chat, [{}](tg://user?id={})!".format(
Emoji.SPARKLES, # Add an emoji
users[message.from_id].first_name,
users[message.from_id].id
),
reply_to_message_id=message.id,
disable_web_page_preview=True
)
@app.on_message(Filters.chat("PyrogramChat") & Filters.new_chat_members)
def welcome(client, message):
new_members = ", ".join([
"[{}](tg://user?id={})".format(i.first_name, i.id)
for i in message.new_chat_members]
)
text = "{} Welcome to [Pyrogram](https://docs.pyrogram.ml/)'s group chat {}!".format(
Emoji.SPARKLES,
new_members
)
client.send_message(
message.chat.id, text,
reply_to_message_id=message.message_id,
disable_web_page_preview=True
)
def main():
client = Client("example")
client.set_update_handler(update_handler)
client.start()
client.idle()
if __name__ == "__main__":
main()
app.start()
app.idle()