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,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 Received: from localhost.localdomain (unknown [101.128.125.94]) by gnuweeb.org (Postfix) with ESMTPSA id 7050E7E328; Fri, 29 Jul 2022 00:41:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1659055316; bh=fJHoUcdTLj9ZZxec22FqGa+1uOq3UNsMy1H5ov49JwY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DO4jagF1VzitqB40f+vVMBxDM34e02IH2wTgDLCMd6Ue40gQBvasEn9u9K+x+RlQN 61KFpsC6nh2zWfOmyO3aHm9QRElpCxidG2w8TGVELYGxaV30XP/s5GUKFbGDJYOMYq ZT0enSvz+rlQDXmQA/jcKM5UfnLe8ZPNhXFa2XpDVJueHJt79HIkhA57pLEDPxfrrF qFjy4/v2PorFPTTWNLQ2xCULn7fB6sPa7QokM10M5QmD45zd4MpsvGpPsvWn7wmkG4 f7ZF2/A0LBrjEOIuUgOgdCoR4kJoMwiE66hAjcSs1AGkXjSG/DQpQUi4C/VC3HBlQC sZRONVM87ry3g== From: Muhammad Rizki To: Ammar Faizi Cc: Muhammad Rizki , GNU/Weeb Mailing List Subject: [PATCH v1 16/18] Add create_chat_link() function Date: Fri, 29 Jul 2022 07:40:25 +0700 Message-Id: <20220729004046.1890-17-kiizuha@gnuweeb.org> X-Mailer: git-send-email 2.34.1.windows.1 In-Reply-To: <20220729004046.1890-1-kiizuha@gnuweeb.org> References: <20220729004046.1890-1-kiizuha@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: I want to easily create a chat link when the pyrogram's Chat object sometimes doesn't have an invite_link value (None), so I create it myself and call it in packages/plugins/commands/manage_broadcast.py Signed-off-by: Muhammad Rizki --- .../packages/plugins/commands/manage_broadcast.py | 3 ++- daemon/scraper/utils.py | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/daemon/packages/plugins/commands/manage_broadcast.py b/daemon/packages/plugins/commands/manage_broadcast.py index 8397508..e0cc0dc 100644 --- a/daemon/packages/plugins/commands/manage_broadcast.py +++ b/daemon/packages/plugins/commands/manage_broadcast.py @@ -6,6 +6,7 @@ from pyrogram.types import Message from pyrogram import filters, enums from packages import DaemonClient +from scraper import utils @DaemonClient.on_message( @@ -24,7 +25,7 @@ async def add_broadcast(c: DaemonClient, m: Message): type=str(m.chat.type), created_at=m.date, username=m.chat.username, - link=m.chat.invite_link + link=utils.create_chat_link(m.chat) ) if inserted is None: diff --git a/daemon/scraper/utils.py b/daemon/scraper/utils.py index d894c32..e1c0c77 100644 --- a/daemon/scraper/utils.py +++ b/daemon/scraper/utils.py @@ -4,7 +4,7 @@ # Copyright (C) 2022 Ammar Faizi # -from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton +from pyrogram.types import Chat, InlineKeyboardMarkup, InlineKeyboardButton from email.message import Message from typing import Dict from slugify import slugify @@ -263,3 +263,16 @@ def button_numbers(data: list, callback_prefix: str, limit: int = 8): buttons = [lst[i:i + limit ] for i in range(0, len(lst), limit)] return InlineKeyboardMarkup(buttons) + + +def create_chat_link(chat: Chat): + chat_id_str = str(chat.id).replace("-100","") + if chat.invite_link: + chat_link = chat.invite_link + + elif chat.username: + chat_link = f"t.me/{chat.username}" + else: + chat_link = f"t.me/c/{chat_id_str}/1" + + return chat_link -- Muhammad Rizki