Skip to content

Commit 19ad704

Browse files
committed
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
1 parent 436c48d commit 19ad704

1 file changed

Lines changed: 28 additions & 10 deletions

File tree

examples/welcome_bot.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,40 @@
66

77
from pyrogram import Client, Emoji, Filters
88

9-
MENTION = "[{}](tg://user?id={})"
10-
MESSAGE = "{} Welcome to [Pyrogram](https://docs.pyrogram.ml/)'s group chat {}!"
9+
USER = "**{}**"
10+
MESSAGE = "{} Welcome to [Pyrogram](https://docs.pyrogram.ml/)'s group chat {{}}!".format(Emoji.SPARKLES)
1111

12-
app = Client("my_account")
12+
app = Client("dan_prod")
1313

14+
enabled_groups = Filters.chat("PyrogramLounge")
15+
last_welcomes = {}
1416

15-
@app.on_message(Filters.chat("PyrogramChat") & Filters.new_chat_members)
17+
18+
@app.on_message(enabled_groups & Filters.new_chat_members)
1619
def welcome(client, message):
17-
# Build the new members list (with mentions) by using their first_name
18-
new_members = [MENTION.format(i.first_name, i.id) for i in message.new_chat_members]
20+
chat_id = message.chat.id
21+
22+
# Get the previous welcome message and members, if any
23+
previous_welcome, previous_members = last_welcomes.pop(chat_id, (None, []))
24+
25+
# Delete the previous message, if exists
26+
if previous_welcome:
27+
previous_welcome.delete()
28+
29+
# Build the new members list by using their first_name. Also append the previous members, if any
30+
new_members = [USER.format(i.first_name) for i in message.new_chat_members] + previous_members
31+
32+
# Build the welcome message by using an emoji and the list we created above
33+
text = MESSAGE.format(", ".join(new_members))
34+
35+
# Actually send the welcome and save the new message and the new members list
36+
last_welcomes[message.chat.id] = message.reply(text, disable_web_page_preview=True), new_members
1937

20-
# Build the welcome message by using an emoji and the list we built above
21-
text = MESSAGE.format(Emoji.SPARKLES, ", ".join(new_members))
2238

23-
# Send the welcome message
24-
message.reply(text, disable_web_page_preview=True)
39+
@app.on_message(enabled_groups)
40+
def reset(client, message):
41+
# Don't make the bot delete the previous welcome in case someone talks in the middle
42+
last_welcomes.pop(message.chat.id, None)
2543

2644

2745
app.run() # Automatically start() and idle()

0 commit comments

Comments
 (0)