From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NO_DNS_FOR_FROM autolearn=no autolearn_force=no version=3.4.6 Received: from localhost.localdomain (unknown [101.128.126.135]) by gnuweeb.org (Postfix) with ESMTPSA id 918477E375; Sun, 8 Jan 2023 06:15:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1673158561; bh=lXakaSffhcWQGN8Zf6mS+c54TVizuCOfV5d6ghLAXmI=; h=From:To:Cc:Subject:Date:From; b=dCEcCkJAKV0j1wjn3y8UrFNvg41BSS9a/R4qd99UrIjubHMkGfi77om9wPovcJA2H 3MYsSvSijf8vcmYLyKHH9GFg/ihnInpXTEsnHd9Vqx9vtPL4g/bfPNcmzZ6EjkFSIX 0RR3H2jyzH+M2+9S2YVk59CmQ3J8lwrQ3oUcO3f3bEmHZIeWfTOHG4/MutNNLrKSoJ /VEMKAOgmJjfN/fIzgkvhXnlx3N+SS8yQxaC89w7L4/XQtqzAz58Dxm8NkxQ1Ru6BF dNp6/uEsQKtQRpCt/xb13/1UiS1Y9lpvlVTt1PDKW+oyxpPdM/yBcSHsYknTUJcyyb BeSsMgDXcwn0w== From: Muhammad Rizki To: Ammar Faizi Cc: Muhammad Rizki , Alviro Iskandar Setiawan , GNU/Weeb Mailing List Subject: [PATCH v2 00/11] Improvements, refactors, and features Date: Sun, 8 Jan 2023 13:15:32 +0700 Message-Id: <20230108061543.1780-1-kiizuha@gnuweeb.org> X-Mailer: git-send-email 2.34.1.windows.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: Hir sir, This is v2 of Improvements, refactors, and features. This series is to refactor function, squash a commit, and refactor the Telegram bot runner. In the last series, I didn't care so much with the Discord implementation. So, I forget to add a reconnect MySQL method for the Discord bot. In this series, I've added it and implement it in the listener.py exactly in the handle_db_error(). In the last commit of the series, Telegram report_err() is implemented in the listener.py. In this series, I've been move it to the Telegram bot custom client instead. I've added a report_err() for the Discord bot too in the client. I've added DaemonException() error class too to easily save a thread URL if errors may happen, this error class is to easily reproducing errors. Some unnecessary try/except block is already removed and add the try/except block again in the __handle_atom_url(), we need to catch the thread URL to easily reproducing errors if errors may happen in the future. I've implemented handle_db_error() for the Discord bot too. What's new? - Added a report_err() for the Discord bot. - Added a DaemonException() error class for both bots. - Added a ping() for reconnecting MySQL conn for the Discord bot. - Added a handle_db_error() for the Discord bot mail listener. Code Refactors: v2: 1) Move Telegram bot connection to the listener's run() function. We use start(), idle() and stop() rather than the run() that are the same with 3 of them. The different is, we need to connect to the Telegram server first, then we add a scheduler to listen a new email message, and then idle() the Telegram bot to be able to manage the atom URLs and broadcast chats. Why ordering from start() -> idle() -> stop()? As I said, we need to connect to the Telegram server first, since the code only is an initialization, we shouldn't worry about them, they are the same as pryogram's run(). 2) Change the report_err() codes for both Telegram and Discord, we use DaemonException as it's argument type. v1: 1) Move report_err() to the DaemonTelegram() for the Telegram bot. 2) Remove try/except for both bots in their listener.py. 3) Ensure the Telegram bot has been started, so start the client earlier. 4) Rename the Telegram bot custom client to "DaemonTelegram". There are 11 patches in this series: - Patch 1 is to remove unnecessary try/except in the listener for DC bot. - Patch 2 is to change the TG runner method to perform graceful exit. - Patch 3 is to add a DaemonException() error class to handle error. - Patch 4 is to refactor report_err() and move it to the DaemonClient(). - Patch 5 is to implement the DaemonException() and change report_err(). - Patch 6 is to remove unnecessary try/except in the listener for TG bot. - Patch 7 is to add a report_err() for the Discord bot. - Patch 8 is to implement report_err() and DaemonException() for DC bot. - Patch 9 is to add a ping() for reconnecting the MySQL connection. - Patch 10 is to add a handle_db_error() to handle the MySQL errors. - Patch 11 is to rename the Telegram bot custom client. How to set log channel ID for the Telegram bot: 1. Make a channel for log in the Telegram. 2. Copy the channel ID, if you unable to see the ID you can use API or bot who has the feature, or use a 64gram desktop. 3. Paste the copied ID into the config.py in the telegram directory. How to set log channel ID for the Discord bot: 1. Make a channel for log in your Discord server. 2. Copy the channel ID by right click and copy, if you unable to get the ID you can go to your account settings, then enable the `Developer mode` or use bot who has the feature. 3. Paste the copied ID into the config.py in the dscord directory. How to use: 1. Execute the db.sql file in the daemon directory. 2. Setup .env file, the example is there with suffix .example, this file name must remove the suffix name .example. 3. Set up the config.py in each bot directory, such as dscord and telegram. The example is there with suffix .example & the file name must remove suffix name .example. 4. Run `pip3 install -r requirements.txt` in each bot directory. 5. STORAGE_DIR env value must `storage` to make it work fine. 6. Run the bot by `python3 dc.py` or `python3 tg.py`. Please give it a test and give me more comments or suggestion, thanks! ## Changelog v1 -> v2: - Move Telegram bot runner - Change the report_err() codes for both Telegram and Discord - Change all commits title prefixes - Change some commits descriptions Signed-off-by: Muhammad Rizki --- Muhammad Rizki (11): discord: Remove unnecessary try/except block telegram: Perform graceful exit when interrupted by a signal Add DaemonException() class telegram: Refactor report_err() and move to the DaemonClient() class telegram: Implement the DaemonException() and refactor report_err() telegram: listener: Remove unnecessary try/except block discord: add report_err() for the Discord bot discord: Implement the report_err() and DaemonException() error class discord: database: Add ping() method for reconnect MySQL discord: listener: Add handle_db_error to handle the MySQL errors telegram: Rename the Telegram bot custom client daemon/dscord/database/core.py | 6 +++ daemon/dscord/gnuweeb/client.py | 8 ++++ daemon/dscord/mailer/listener.py | 48 ++++++++++++++----- daemon/exceptions.py | 18 +++++++ daemon/telegram/mailer/listener.py | 42 ++++++++-------- daemon/telegram/packages/__init__.py | 2 +- daemon/telegram/packages/client.py | 10 +++- daemon/telegram/packages/decorator.py | 2 +- .../packages/plugins/callbacks/del_atom.py | 6 +-- .../packages/plugins/callbacks/del_chat.py | 6 +-- .../packages/plugins/commands/manage_atom.py | 10 ++-- .../plugins/commands/manage_broadcast.py | 10 ++-- .../packages/plugins/commands/scrape.py | 6 +-- daemon/tg.py | 8 ++-- 14 files changed, 122 insertions(+), 60 deletions(-) create mode 100644 daemon/exceptions.py base-commit: d72337c346686b7faa48ee654c967dba1dfebea0 -- Muhammad Rizki