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 EB5BB7FA25; Wed, 27 Jul 2022 02:47:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1658890047; bh=fJHoUcdTLj9ZZxec22FqGa+1uOq3UNsMy1H5ov49JwY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gDg4gF7KDBwXJ21pJLN9Yb5sjOgxZfx6tNbeCDt+94tLehywkSS/U+/Am+jFwY/M6 lFt4//f+AAjxPtrrWzHB2LtIohUKmyOouzw3NPr0MwZsQ1BmHBmIjOVWRn7w2jtO4L iD/DLU/ZUgTyLzD3LNyVapdltmTx31AzZYdTimCHI/y/21WlE4HvpG56ucBIlZ7r/4 52vGLOoAeI5IM+CEVWJ85WocCs+8DuICHPK0uv84Z5ghU/gSXB7JX90ylxI2MUp8yX FXT9DZ5Ud3O9kiuD6o2deUfV15bHuYV8tPX6vfiIUBixUI881QiclmlRSltmktE5q0 ahMOr18oOHAPw== From: Muhammad Rizki To: Ammar Faizi Cc: Muhammad Rizki , GNU/Weeb Mailing List Subject: [PATCH v1 16/18] Add create_chat_link() function Date: Wed, 27 Jul 2022 09:46:32 +0700 Message-Id: <20220727024634.1995-17-kiizuha@gnuweeb.org> X-Mailer: git-send-email 2.34.1.windows.1 In-Reply-To: <20220727024634.1995-1-kiizuha@gnuweeb.org> References: <20220727024634.1995-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