2022-06-20 13:55:14 +00:00
|
|
|
import contextlib
|
2022-05-23 12:40:30 +00:00
|
|
|
from os import sep, remove, mkdir
|
|
|
|
from os.path import exists
|
2022-06-26 12:59:36 +00:00
|
|
|
from typing import List, Optional, Union
|
2022-06-20 13:55:14 +00:00
|
|
|
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
|
|
|
from httpx import AsyncClient
|
|
|
|
|
2023-01-23 14:15:07 +00:00
|
|
|
from pyrogram import Client as OldClient
|
2023-01-31 16:24:56 +00:00
|
|
|
from pyrogram.types import Chat as OldChat, Message as OldMessage, Dialog
|
2022-06-26 12:59:36 +00:00
|
|
|
|
|
|
|
from pyromod.utils.conversation import Conversation
|
2023-03-12 03:56:01 +00:00
|
|
|
from pyromod.utils.errors import (
|
|
|
|
AlreadyInConversationError,
|
|
|
|
TimeoutConversationError,
|
|
|
|
ListenerCanceled,
|
|
|
|
)
|
2022-06-26 12:59:36 +00:00
|
|
|
|
2022-05-23 12:40:30 +00:00
|
|
|
from sqlitedict import SqliteDict
|
|
|
|
|
|
|
|
# init folders
|
|
|
|
if not exists("data"):
|
|
|
|
mkdir("data")
|
|
|
|
sqlite = SqliteDict(f"data{sep}data.sqlite", autocommit=True)
|
|
|
|
|
|
|
|
|
|
|
|
def get_sudo_list():
|
|
|
|
return sqlite.get("sudo_list", [])
|
|
|
|
|
|
|
|
|
|
|
|
def _status_sudo():
|
|
|
|
return sqlite.get("sudo_enable", False)
|
|
|
|
|
|
|
|
|
|
|
|
def safe_remove(name: str) -> None:
|
2022-06-20 13:55:14 +00:00
|
|
|
with contextlib.suppress(FileNotFoundError):
|
2022-05-23 12:40:30 +00:00
|
|
|
remove(name)
|
2022-06-20 13:55:14 +00:00
|
|
|
|
|
|
|
|
2023-01-23 14:15:07 +00:00
|
|
|
class Message(OldMessage):
|
|
|
|
arguments: str
|
|
|
|
parameter: List
|
|
|
|
forum_topic: Optional[bool] = None
|
|
|
|
chat: "Chat"
|
|
|
|
|
|
|
|
def obtain_message(self) -> Optional[str]:
|
2023-03-12 03:56:01 +00:00
|
|
|
"""Obtains a message from either the reply message or command arguments."""
|
2023-01-23 14:15:07 +00:00
|
|
|
|
|
|
|
def obtain_user(self) -> Optional[int]:
|
2023-03-12 03:56:01 +00:00
|
|
|
"""Obtains a user from either the reply message or command arguments."""
|
2023-01-23 14:15:07 +00:00
|
|
|
|
|
|
|
async def delay_delete(self, delete_seconds: int = 60) -> Optional[bool]:
|
2023-03-12 03:56:01 +00:00
|
|
|
"""Deletes the message after a specified amount of seconds."""
|
2023-01-23 14:15:07 +00:00
|
|
|
|
|
|
|
async def safe_delete(self, revoke: bool = True) -> None:
|
2023-03-12 03:56:01 +00:00
|
|
|
"""Safely deletes the message."""
|
2023-01-23 14:15:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Client(OldClient):
|
2022-06-20 13:55:14 +00:00
|
|
|
job: Optional[AsyncIOScheduler] = None
|
2022-05-23 12:40:30 +00:00
|
|
|
|
2022-06-26 12:59:36 +00:00
|
|
|
async def listen(self, chat_id, filters=None, timeout=None) -> Optional[Message]:
|
2023-03-12 03:56:01 +00:00
|
|
|
"""Listen for a message in a conversation."""
|
2022-06-26 12:59:36 +00:00
|
|
|
|
2023-03-12 03:56:01 +00:00
|
|
|
async def ask(
|
|
|
|
self, chat_id, text, filters=None, timeout=None, *args, **kwargs
|
|
|
|
) -> Optional[Message]:
|
|
|
|
"""Ask a message in a conversation."""
|
2022-06-26 12:59:36 +00:00
|
|
|
|
|
|
|
def cancel_listener(self, chat_id):
|
2023-03-12 03:56:01 +00:00
|
|
|
"""Cancel the conversation with the given chat_id."""
|
2022-06-26 12:59:36 +00:00
|
|
|
|
|
|
|
def cancel_all_listeners(self):
|
2023-03-12 03:56:01 +00:00
|
|
|
"""Cancel all conversations."""
|
2022-06-26 12:59:36 +00:00
|
|
|
|
2023-03-12 03:56:01 +00:00
|
|
|
def conversation(
|
|
|
|
self, chat_id: Union[int, str], once_timeout: int = 60, filters=None
|
|
|
|
) -> Optional[Conversation]:
|
|
|
|
"""Initialize a conversation with the given chat_id."""
|
2022-06-26 12:59:36 +00:00
|
|
|
|
2023-01-31 16:24:56 +00:00
|
|
|
async def get_dialogs_list(self) -> List[Dialog]:
|
2023-03-12 03:56:01 +00:00
|
|
|
"""Get a list of all dialogs."""
|
2023-01-31 16:24:56 +00:00
|
|
|
|
2022-05-23 12:40:30 +00:00
|
|
|
|
2023-01-23 14:15:07 +00:00
|
|
|
class Chat(OldChat):
|
|
|
|
is_forum: Optional[bool] = None
|
2022-05-23 12:40:30 +00:00
|
|
|
|
2023-01-23 14:15:07 +00:00
|
|
|
async def listen(self, chat_id, filters=None, timeout=None) -> Optional[Message]:
|
2023-03-12 03:56:01 +00:00
|
|
|
"""Listen for a message in a conversation."""
|
2022-05-25 11:26:50 +00:00
|
|
|
|
2023-03-12 03:56:01 +00:00
|
|
|
async def ask(
|
|
|
|
self, chat_id, text, filters=None, timeout=None, *args, **kwargs
|
|
|
|
) -> Optional[Message]:
|
|
|
|
"""Ask a message in a conversation."""
|
2022-06-20 13:55:14 +00:00
|
|
|
|
2023-01-23 14:15:07 +00:00
|
|
|
def cancel_listener(self, chat_id):
|
2023-03-12 03:56:01 +00:00
|
|
|
"""Cancel the conversation with the given chat_id."""
|