From 19ad70455fef1bc4328b23dbe905d604aa0671c1 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 10 Dec 2018 15:15:21 +0100 Subject: [PATCH] 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 --- examples/welcome_bot.py | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/examples/welcome_bot.py b/examples/welcome_bot.py index 5dbb44fb..2598a802 100644 --- a/examples/welcome_bot.py +++ b/examples/welcome_bot.py @@ -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()