mirror of
https://github.com/pyrogram/pyrogram
synced 2025-08-27 04:18:04 +00:00
Update welcome_bot.py example
Delete the previous message in case of consecutive member joins and send a new one containing the names of all the previous new members
This commit is contained in:
parent
436c48d1c2
commit
19ad70455f
@ -6,22 +6,40 @@ to make it only work for specific messages in a specific chat.
|
|||||||
|
|
||||||
from pyrogram import Client, Emoji, Filters
|
from pyrogram import Client, Emoji, Filters
|
||||||
|
|
||||||
MENTION = "[{}](tg://user?id={})"
|
USER = "**{}**"
|
||||||
MESSAGE = "{} Welcome to [Pyrogram](https://docs.pyrogram.ml/)'s group chat {}!"
|
MESSAGE = "{} Welcome to [Pyrogram](https://docs.pyrogram.ml/)'s group chat {{}}!".format(Emoji.SPARKLES)
|
||||||
|
|
||||||
app = Client("my_account")
|
app = Client("dan_prod")
|
||||||
|
|
||||||
|
enabled_groups = Filters.chat("PyrogramLounge")
|
||||||
|
last_welcomes = {}
|
||||||
|
|
||||||
|
|
||||||
@app.on_message(Filters.chat("PyrogramChat") & Filters.new_chat_members)
|
@app.on_message(enabled_groups & Filters.new_chat_members)
|
||||||
def welcome(client, message):
|
def welcome(client, message):
|
||||||
# Build the new members list (with mentions) by using their first_name
|
chat_id = message.chat.id
|
||||||
new_members = [MENTION.format(i.first_name, i.id) for i in message.new_chat_members]
|
|
||||||
|
|
||||||
# Build the welcome message by using an emoji and the list we built above
|
# Get the previous welcome message and members, if any
|
||||||
text = MESSAGE.format(Emoji.SPARKLES, ", ".join(new_members))
|
previous_welcome, previous_members = last_welcomes.pop(chat_id, (None, []))
|
||||||
|
|
||||||
# Send the welcome message
|
# Delete the previous message, if exists
|
||||||
message.reply(text, disable_web_page_preview=True)
|
if previous_welcome:
|
||||||
|
previous_welcome.delete()
|
||||||
|
|
||||||
|
# Build the new members list by using their first_name. Also append the previous members, if any
|
||||||
|
new_members = [USER.format(i.first_name) for i in message.new_chat_members] + previous_members
|
||||||
|
|
||||||
|
# Build the welcome message by using an emoji and the list we created above
|
||||||
|
text = MESSAGE.format(", ".join(new_members))
|
||||||
|
|
||||||
|
# Actually send the welcome and save the new message and the new members list
|
||||||
|
last_welcomes[message.chat.id] = message.reply(text, disable_web_page_preview=True), new_members
|
||||||
|
|
||||||
|
|
||||||
|
@app.on_message(enabled_groups)
|
||||||
|
def reset(client, message):
|
||||||
|
# Don't make the bot delete the previous welcome in case someone talks in the middle
|
||||||
|
last_welcomes.pop(message.chat.id, None)
|
||||||
|
|
||||||
|
|
||||||
app.run() # Automatically start() and idle()
|
app.run() # Automatically start() and idle()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user