Last active
May 8, 2024 09:10
-
-
Save MohammadHosseinGhorbani/1e54adda3cf8839ce0453a8861ca77b6 to your computer and use it in GitHub Desktop.
Conversations in pyrogram (no extra package needed)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pyrogram import Client, filters | |
app = Client('CONVERSATION_EXAMPLE') | |
conversations = {} | |
infos = {} | |
def conv_filter(conversation_level): | |
def func(_, __, message): | |
return conversations.get(message.from_user.id) == conversation_level | |
return filters.create(func, "ConversationFilter") | |
@app.on_message(filters.command('start') & filters.private) | |
async def start(client, message): | |
await message.reply_text('reply your name') | |
conversations.update({message.from_user.id: "name"}) | |
infos.update({message.from_user.id: {}}) | |
@app.on_message(conv_filter("name") & filters.private) | |
async def name_handler(client, message): | |
await message.reply_text('now send your age') | |
infos.get(message.from_user.id).update({"name": message.text}) | |
conversations.update({message.from_user.id: 'age'}) | |
@app.on_message(conv_filter("age") & filters.private) | |
async def age_handler(client, message): | |
user_infos = infos.get(message.from_user.id) | |
user_infos.update({"age": message.text}) | |
await message.reply_text(f'your name: {user_infos.get("name")}, your age: {user_infos.get("age")}') | |
conversations.pop(message.from_user.id) | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
+++