|
6 | 6 |
|
7 | 7 | from pyrogram import Client, Emoji, Filters |
8 | 8 |
|
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) |
11 | 11 |
|
12 | | -app = Client("my_account") |
| 12 | +app = Client("dan_prod") |
13 | 13 |
|
| 14 | +enabled_groups = Filters.chat("PyrogramLounge") |
| 15 | +last_welcomes = {} |
14 | 16 |
|
15 | | -@app.on_message(Filters.chat("PyrogramChat") & Filters.new_chat_members) |
| 17 | + |
| 18 | +@app.on_message(enabled_groups & Filters.new_chat_members) |
16 | 19 | 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 |
19 | 37 |
|
20 | | - # Build the welcome message by using an emoji and the list we built above |
21 | | - text = MESSAGE.format(Emoji.SPARKLES, ", ".join(new_members)) |
22 | 38 |
|
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) |
25 | 43 |
|
26 | 44 |
|
27 | 45 | app.run() # Automatically start() and idle() |
0 commit comments