Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,29 @@ answer = client.listen.CallbackQuery(filters.user(update.from_user.id))
# Example
```Python

from convopyro import listen_message
from convopyro import listen_message,cancel_listen

@app.on_message(filters.command('start'))
@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}')
send = await client.send_message(
message.chat.id,
"What's your name?",
reply_markup=InlineKeyboardMarkup(
[[InlineKeyboardButton("Cancel listen", callback_data="cancel_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")


@app.on_callback_query(filters.regex(pattern=r"^cancel_listen$"))
async def listen_closed(client: Client, callback_query: CallbackQuery):
await cancel_listen(client, callback_query.from_user.id)
await callback_query.message.delete()

```

Expand Down
26 changes: 10 additions & 16 deletions src/convopyro/__init__.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
#!/usr/bin/python3
from collections import OrderedDict
from typing import Union
from typing import Union, List
import pyrogram, asyncio

class Conversation():
"""
A conversation plugin class for pyrogram using inbuild Update Handlers.
Complete list of handlers to be used without `Handlers` postfix :-
https://docs.pyrogram.org/api/handlers#index


Usage:
In main.py where `Client` is initialized:

app = Client('MyBot')
Conversation(app) # That's it!

Then just use inside any handler `client.listen`:

@app.on_message()
def button_click(client, update):
answer = client.listen.CallbackQuery(filters.user(update.from_user.id))

Method client.listen.Message(or any other types)
Parameters:
filters:
Expand All @@ -35,12 +30,10 @@ def button_click(client, update):
-> pyrogram.filters.chat
-> str
if pyrogram filter's `user` or `chat` is passed as `id` then it gets combined with rest `filters`.

Default is `None` but either filter or id is required.

timeout:
In seconds (int) for waiting time of getting a response.

Returns:
`update` (like pyrogram.types.Message ...etc) if user reponded within given conditions.
`None` if listen cancelled using `listen.Cancel`
Expand All @@ -53,7 +46,6 @@ async def start(client, message):
if reply_msg:
reply_msg.reply(f'hello {reply_msg.text}')


Method client.listen.Cancel
Parameters:
id:
Expand Down Expand Up @@ -128,12 +120,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):
Expand All @@ -145,8 +136,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)
return await client.listen.Message(filters.chat(chat_id), timeout=timeout,id=filters.chat(chat_id))
except TimeoutError:
return None

async def cancel_listen(client:Client, chat_id:Union[int, str, List[Union[int, str]]]) -> bool:
return await client.listen.Cancel(filters.chat(chat_id))