From 7b8c93ee2d83559eb8fbe0000e533897e92e2d3e Mon Sep 17 00:00:00 2001 From: keyiflerolsun Date: Sat, 21 Jan 2023 03:04:14 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=95=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🕊 --- README.md | 36 +++++++++++++++++++++++++++--------- setup.py | 2 +- src/convopyro/__init__.py | 16 +++++++++------- 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index d9d7366..135a371 100644 --- a/README.md +++ b/README.md @@ -27,16 +27,34 @@ answer = client.listen.CallbackQuery(filters.user(update.from_user.id)) ``` # Example -```Python - -from convopyro import listen_message - -@app.on_message(filters.command('start')) -async def start(client, message): - await client.send_mesage(messsage.chat.id, "What's your name?") - answer = await listen_message(client, messsage.chat.id, timeout=None) - await answer.reply(f'hello {answer.text}') +```python + +from pyrogram import Client, filters +from pyrogram.types import Message, InlineKeyboardButton, InlineKeyboardMarkup, CallbackQuery + +from convopyro import listen_message, stop_listen + +@Client.on_message(filters.command(["start"], ["!",".","/"]) & filters.private) +async def start(client:Client, message:Message): + await client.send_message( + chat_id = message.chat.id, + text = "What's your name?", + reply_markup = InlineKeyboardMarkup( + [[InlineKeyboardButton(text="Stop Listen", callback_data="stop_listen")]] + ) + ) + + answer = await listen_message(client, message.chat.id, timeout=None) + if answer: + await answer.reply(f"hello {answer.text}") + else: + await message.reply("Canceled Answer") + +@Client.on_callback_query(filters.regex(pattern=r"^stop_listen$")) +async def listen_stopped(client:Client, callback_query:CallbackQuery): + await stop_listen(client, callback_query.from_user.id) + await callback_query.message.delete() ``` diff --git a/setup.py b/setup.py index b830f18..151b2f0 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ setuptools.setup( name="convopyro", - version="0.3", + version="0.5", author="Ripe", author_email="ripeey@protonmail.com", description="A conversation plugin class for pyrogram using inbuild Update Handlers", diff --git a/src/convopyro/__init__.py b/src/convopyro/__init__.py index cdaf045..5f48f7a 100644 --- a/src/convopyro/__init__.py +++ b/src/convopyro/__init__.py @@ -1,6 +1,6 @@ #!/usr/bin/python3 from collections import OrderedDict -from typing import Union +from typing import Union, List import pyrogram, asyncio class Conversation(): @@ -128,12 +128,11 @@ async def __remove(self, _id, update = None): self.handlers[_id] = update event.set() - async def Cancel(self, _id): - if str(_id) in self.handlers: - await self.__remove(str(_id)) - return True - else: + async def Cancel(self, _id) -> bool: + if str(_id) not in self.handlers: return False + await self.__remove(str(_id)) + return True def __getattr__(self, name): async def wrapper(*args, **kwargs): @@ -145,8 +144,11 @@ async def wrapper(*args, **kwargs): from pyrogram.types import Message from asyncio.exceptions import TimeoutError -async def listen_message(client:Client, chat_id:int, timeout=None) -> Union[Message, None]: +async def listen_message(client:Client, chat_id:Union[int, str, List[Union[int, str]]], timeout:Union[int, None]=None) -> Union[Message, None]: try: return await client.listen.Message(filters.chat(chat_id), timeout=timeout) except TimeoutError: return None + +async def stop_listen(client:Client, chat_id:Union[int, str, List[Union[int, str]]]) -> bool: + return await client.listen.Cancel(filters.chat(chat_id)) \ No newline at end of file From b533fee3308ea581fcc022ad5237328008cb7a30 Mon Sep 17 00:00:00 2001 From: keyiflerolsun Date: Sat, 21 Jan 2023 03:18:30 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=95=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🕊 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 135a371..3f98ae1 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Complete list of handlers to be used without `Handlers` postfix :- Use the package manager [pip](https://pip.pypa.io/en/stable/) to install or simply copy the class file to your project. ```bash -pip install git+https://github.com/Ripeey/Conversation-Pyrogram +pip install -U convopyro ``` # Basic Usage