Package: convopyro
Conversation-PyrogramA conversation plugin class for pyrogram using inbuild Update HandlersComplete list of handlers to be used withoutHandlerspostfix :-https://docs.pyrogram.org/api/handlers#indexInstallationUse the package managerpipto install or simply copy the class file to your project.pipinstallgit+https://github.com/Ripeey/Conversation-PyrogramBasic Usagemain.pyWhere the Client is initializedfrompyrogramimportClientfromconvopyroimportConversationapp=Client('MyApp')Conversation(app)# That's it!Then at anyupdate handleranswer=client.listen.CallbackQuery(filters.user(update.from_user.id))ExamplefrompyrogramimportClient,filtersfrompyrogram.typesimportMessage,InlineKeyboardButton,InlineKeyboardMarkup,CallbackQueryfromconvopyroimportlisten_message,stop_listen@Client.on_message(filters.command(["start"],["!",".","/"])&filters.private)asyncdefstart(client:Client,message:Message):awaitclient.send_message(chat_id=message.chat.id,text="What's your name?",reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton(text="Stop Listen",callback_data="stop_listen")]]))answer=awaitlisten_message(client,message.chat.id,timeout=None)ifanswer:awaitanswer.reply(f"hello{answer.text}")else:awaitmessage.reply("Canceled Answer")@Client.on_callback_query(filters.regex(pattern=r"^stop_listen$"))asyncdeflisten_stopped(client:Client,callback_query:CallbackQuery):awaitstop_listen(client,callback_query.from_user.id)awaitcallback_query.message.delete()Advanced UsageThe conversation class has 2 primary methodslisten.Handler(HandlerslikeMessage, CallbackQuery...etc) andlisten.Cancelfor ending an ongoing listener.listen.Handler()The conversationlisten.Message(or any otherHandler)takes 3 parameters, default isNonebut eitherfilteroridas parameter is required.filters :SingleorCombinedis required but this isoptionalifidis passed with a valid singleuserorchatfilter (which will learn below).id :An unique id for each listen, this could bestr, a singleuser filterorchat filter, this is mostlyoptionaland only needed toCancel()a conversation listen.
Ifuser/chat filteris passed then itcombinesitself withfiltersso you dont need to repeat again infiltersusing&, where as ifstris used it's just used normally asid.timeout :Waiting time in secondsintfor getting a responseoptional.ReturnUpdate:  Based on handlers used could be one of received updates such asMessage,CallbackQuery, etc.None: When listen gets cancel usinglisten.Cancela None is return as response at listen callback.Exception: Anasyncio.TimeoutErroris raised if provided waiting time get's over.listen.Cancel()The conversationlisten.Canceltakes 1 required parameter. This method is used to cancel a specific conversation listen.id: An unique id provided during listen, this could bestr, a singleuser filterorchat filter.ReturnBoolean:  False ifidprovided already cancelled or invalid.Example@app.on_callback_query(filters.regex(r'stop'))asyncdef_(client,query):# This will return response None at listenawaitclient.listen.Cancel(filters.user(query.from_user.id))@app.on_message(filters.regex(r'hi'))asyncdef_(client,message):button=InlineKeyboardMarkup([[InlineKeyboardButton('Cancel Question',callback_data='stop')]])question=awaitclient.send_message(message.chat.id,'Enter your name in 5s.',reply_markup=button)# A nice flow of conversationtry:response=awaitclient.listen.Message(filters.text,id=filters.user(message.from_user.id),timeout=5)exceptasyncio.TimeoutError:awaitmessage.reply('Too late 5s gone.')else:ifresponse:awaitresponse.reply(f'Hello{response.text}')else:awaitmessage.reply('Okay cancelled question!')finally:awaitquestion.delete()