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:
Dan 2018-12-10 15:15:21 +01:00
parent 436c48d1c2
commit 19ad70455f

View File

@ -6,22 +6,40 @@ to make it only work for specific messages in a specific chat.
from pyrogram import Client, Emoji, Filters
MENTION = "[{}](tg://user?id={})"
MESSAGE = "{} Welcome to [Pyrogram](https://docs.pyrogram.ml/)'s group chat {}!"
USER = "**{}**"
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):
# Build the new members list (with mentions) by using their first_name
new_members = [MENTION.format(i.first_name, i.id) for i in message.new_chat_members]
chat_id = message.chat.id
# Build the welcome message by using an emoji and the list we built above
text = MESSAGE.format(Emoji.SPARKLES, ", ".join(new_members))
# Get the previous welcome message and members, if any
previous_welcome, previous_members = last_welcomes.pop(chat_id, (None, []))
# Send the welcome message
message.reply(text, disable_web_page_preview=True)
# Delete the previous message, if exists
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()