MTPyroger/examples/welcome_bot.py

46 lines
1.5 KiB
Python
Raw Normal View History

2018-05-06 09:56:25 +00:00
"""This is the Welcome Bot in @PyrogramChat.
2018-04-14 16:46:54 +00:00
It uses the Emoji module to easily add emojis in your text messages and Filters
2018-10-09 14:37:53 +00:00
to make it only work for specific messages in a specific chat.
2018-03-09 12:23:53 +00:00
"""
from pyrogram import Client, Emoji, Filters
USER = "**{}**"
MESSAGE = "{} Welcome to [Pyrogram](https://docs.pyrogram.ml/)'s group chat {{}}!".format(Emoji.SPARKLES)
2018-10-09 14:37:53 +00:00
2018-12-10 14:19:54 +00:00
enabled_groups = Filters.chat("PyrogramChat")
last_welcomes = {}
2018-04-14 16:46:54 +00:00
2018-12-10 14:19:54 +00:00
app = Client("my_account")
@app.on_message(enabled_groups & Filters.new_chat_members)
2018-04-14 16:46:54 +00:00
def welcome(client, message):
chat_id = message.chat.id
# Get the previous welcome message and members, if any
previous_welcome, previous_members = last_welcomes.pop(chat_id, (None, []))
# 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
2018-04-14 16:46:54 +00:00
@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)
2018-04-14 16:46:54 +00:00
2018-06-22 11:30:18 +00:00
app.run() # Automatically start() and idle()