From: Muhammad Rizki <[email protected]>
To: Ammar Faizi <[email protected]>
Cc: Muhammad Rizki <[email protected]>,
GNU/Weeb Mailing List <[email protected]>
Subject: [PATCH v1 07/11] Re-design send email message to Telegram
Date: Mon, 18 Jul 2022 18:20:03 +0700 [thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>
We want to separate send message function and inherit with the Pyrogram
Client, so it should makes the code clean and clear.
Signed-off-by: Muhammad Rizki <[email protected]>
---
daemon/packages/__init__.py | 1 +
daemon/packages/client.py | 59 +++++++++++++++++++++++++++++++++++++
daemon/run.py | 4 +--
daemon/scraper/bot.py | 35 ++++++----------------
4 files changed, 71 insertions(+), 28 deletions(-)
create mode 100644 daemon/packages/__init__.py
create mode 100644 daemon/packages/client.py
diff --git a/daemon/packages/__init__.py b/daemon/packages/__init__.py
new file mode 100644
index 0000000..efef9ae
--- /dev/null
+++ b/daemon/packages/__init__.py
@@ -0,0 +1 @@
+from .client import DaemonClient
diff --git a/daemon/packages/client.py b/daemon/packages/client.py
new file mode 100644
index 0000000..fd5b5ec
--- /dev/null
+++ b/daemon/packages/client.py
@@ -0,0 +1,59 @@
+from pyrogram import Client
+from pyrogram.enums import ParseMode
+from pyrogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton
+from typing import Union, BinaryIO
+
+
+class DaemonClient(Client):
+ def __init__(self, name: str, api_id: int,
+ api_hash: str, **kwargs):
+ super().__init__(name, api_id,
+ api_hash, **kwargs)
+
+
+ async def send_text_email(
+ self,
+ chat_id: Union[int, str],
+ text: str,
+ reply_to: int,
+ url: str = None,
+ parse_mode: ParseMode = ParseMode.HTML
+ ) -> Message:
+ print("[send_text_email]")
+ return await self.send_message(
+ chat_id=chat_id,
+ text=text,
+ reply_to_message_id=reply_to,
+ parse_mode=parse_mode,
+ reply_markup=InlineKeyboardMarkup([
+ [InlineKeyboardButton(
+ "See the full message",
+ url=url
+ )]
+ ])
+ )
+
+
+ async def send_patch_email(
+ self,
+ chat_id: Union[int, str],
+ doc: Union[str, BinaryIO],
+ caption: str,
+ reply_to: int,
+ url: str = None,
+ parse_mode: ParseMode = ParseMode.HTML
+ ) -> Message:
+ print("[send_patch_email]")
+ return await self.send_document(
+ chat_id=chat_id,
+ document=doc,
+ caption=caption,
+ reply_to_message_id=reply_to,
+ parse_mode=parse_mode,
+ reply_markup=InlineKeyboardMarkup([
+ [InlineKeyboardButton(
+ "See the full message",
+ url=url
+ )]
+ ])
+ )
diff --git a/daemon/run.py b/daemon/run.py
index 83b2cdb..1151ccd 100644
--- a/daemon/run.py
+++ b/daemon/run.py
@@ -8,7 +8,7 @@ from apscheduler.schedulers.asyncio import AsyncIOScheduler
from scraper import BotMutexes
from dotenv import load_dotenv
from mysql import connector
-from pyrogram import Client
+from packages import DaemonClient
from scraper import Scraper
from scraper import Bot
import os
@@ -17,7 +17,7 @@ import os
def main():
load_dotenv()
- client = Client(
+ client = DaemonClient(
"storage/EmailScraper",
api_id=int(os.getenv("API_ID")),
api_hash=os.getenv("API_HASH"),
diff --git a/daemon/scraper/bot.py b/daemon/scraper/bot.py
index 2392b61..93e633a 100644
--- a/daemon/scraper/bot.py
+++ b/daemon/scraper/bot.py
@@ -8,7 +8,7 @@ from apscheduler.schedulers.asyncio import AsyncIOScheduler
from pyrogram.types import InlineKeyboardMarkup
from pyrogram.types import InlineKeyboardButton
from slugify import slugify
-from pyrogram import Client
+from packages import DaemonClient
from scraper import Scraper
from pyrogram import enums
from . import utils
@@ -32,12 +32,11 @@ class Bot():
]
TG_CHAT_IDS = [
- -1001394203410,
- -1001673279485,
+ "kiizuah"
]
- def __init__(self, client: Client, sched: AsyncIOScheduler,
+ def __init__(self, client: DaemonClient, sched: AsyncIOScheduler,
scraper: Scraper, mutexes: BotMutexes, conn):
self.client = client
self.sched = sched
@@ -123,8 +122,8 @@ class Bot():
reply_to, text, url)
else:
text = "#ml\n" + text
- m = await self.__send_text_msg(tg_chat_id, reply_to,
- text, url)
+ m = await self.__send_text_msg(tg_chat_id, text,
+ reply_to, url)
self.db.insert_telegram(email_id, m.chat.id, m.id)
for d, f in files:
@@ -161,10 +160,10 @@ class Bot():
async def __send_patch_msg(self, mail, tg_chat_id, reply_to, text, url):
print("[__send_patch_msg]")
- tmp, fnm, caption, url = Bot.prepare_send_patch(mail, text, url)
+ tmp, doc, caption, url = Bot.prepare_send_patch(mail, text, url)
ret = await self.__handle_telegram_floodwait(
- self.____send_patch_msg,
- *[tg_chat_id, reply_to, fnm, caption, url]
+ self.client.send_patch_email,
+ *[tg_chat_id, doc, caption, reply_to, url]
)
Bot.clean_up_after_send_patch(tmp)
return ret
@@ -207,7 +206,7 @@ class Bot():
async def __send_text_msg(self, *args):
return await self.__handle_telegram_floodwait(
- self.____send_text_msg,
+ self.client.send_text_email,
*args
)
@@ -251,19 +250,3 @@ class Bot():
)]
])
)
-
-
- async def ____send_text_msg(self, tg_chat_id, reply_to, text, url):
- print("[__send_text_msg]")
- return await self.client.send_message(
- tg_chat_id,
- text,
- reply_to_message_id=reply_to,
- parse_mode=enums.ParseMode.HTML,
- reply_markup=InlineKeyboardMarkup([
- [InlineKeyboardButton(
- "See the full message",
- url=url
- )]
- ])
- )
--
Muhammad Rizki
next prev parent reply other threads:[~2022-07-18 11:20 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-18 11:19 [PATCH v1 00/11] Plan to redesign code Muhammad Rizki
2022-07-18 11:19 ` [PATCH v1 01/11] Fix __send_patch_msg function parameter Muhammad Rizki
2022-07-18 12:45 ` Ammar Faizi
2022-07-18 11:19 ` [PATCH v1 02/11] Fix import problem Muhammad Rizki
2022-07-18 11:19 ` [PATCH v1 03/11] Add default temporary directory Muhammad Rizki
2022-07-18 11:31 ` Ammar Faizi
2022-07-18 11:20 ` [PATCH v1 04/11] Move the Telegram bot session into the storage directory Muhammad Rizki
2022-07-18 11:20 ` [PATCH v1 05/11] daemon: Fix raw lore URL on the inline keyboard button Muhammad Rizki
2022-07-18 11:20 ` [PATCH v1 06/11] Add traceback to get the error detail Muhammad Rizki
2022-07-18 11:27 ` Ammar Faizi
2022-07-18 12:24 ` Kiizuha
2022-07-18 11:20 ` Muhammad Rizki [this message]
2022-07-18 12:49 ` [PATCH v1 07/11] Re-design send email message to Telegram Ammar Faizi
2022-07-18 11:20 ` [PATCH v1 08/11] Move ____send_patch_msg Muhammad Rizki
2022-07-18 11:20 ` [PATCH v1 09/11] Move prepare for patch and clean up patch functions Muhammad Rizki
2022-07-18 11:20 ` [PATCH v1 10/11] Create fix_utf8_chars function Muhammad Rizki
2022-07-18 11:20 ` [PATCH v1 11/11] Recode some codes Muhammad Rizki
2022-07-18 11:33 ` Ammar Faizi
2022-07-18 11:28 ` [PATCH v1 00/11] Plan to redesign code Ammar Faizi
2022-07-18 11:34 ` Ammar Faizi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox