public inbox for [email protected]
 help / color / mirror / Atom feed
From: Muhammad Rizki <[email protected]>
To: Ammar Faizi <[email protected]>
Cc: Muhammad Rizki <[email protected]>,
	GNU/Weeb Mailing List <[email protected]>
Subject: [PATCH v2 16/18] Add create_chat_link() function
Date: Fri, 29 Jul 2022 07:45:05 +0700	[thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>

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 <[email protected]>
---
 .../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 <[email protected]>
 #
 
-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


  parent reply	other threads:[~2022-07-29  0:46 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-29  0:44 [PATCH v2 00/18] New feature to handle atom URLs and broadcast Muhammad Rizki
2022-07-29  0:44 ` [PATCH v2 01/18] Add atom_broadcast.sql Muhammad Rizki
2022-07-29  0:44 ` [PATCH v2 02/18] Add ATOM_URLS and BROADCAST_CHAT methods Muhammad Rizki
2022-07-29  0:44 ` [PATCH v2 03/18] Fix MySQL InternalError: Unread result found Muhammad Rizki
2022-07-29  0:44 ` [PATCH v2 04/18] Create /add_atom command Muhammad Rizki
2022-07-29  0:44 ` [PATCH v2 05/18] Add utility functions for /add_atom Muhammad Rizki
2022-07-29  0:44 ` [PATCH v2 06/18] Refactor database methods Muhammad Rizki
2022-07-29  0:44 ` [PATCH v2 07/18] Add insert_atom() in scraper/db.py Muhammad Rizki
2022-07-29  0:44 ` [PATCH v2 08/18] Rename admin.py to debugger.py Muhammad Rizki
2022-07-29  0:44 ` [PATCH v2 09/18] Add delete_atom() function in scraper/db.py Muhammad Rizki
2022-07-29  0:44 ` [PATCH v2 10/18] Add utility function buton_numbers() Muhammad Rizki
2022-07-29  0:45 ` [PATCH v2 11/18] Move bot commands related files to packages/plugins/commands/ Muhammad Rizki
2022-07-29  0:45 ` [PATCH v2 12/18] Add del_atom callback query Muhammad Rizki
2022-07-29  0:45 ` [PATCH v2 13/18] Add insert_broadcast() in scraper/db.py Muhammad Rizki
2022-07-29  0:45 ` [PATCH v2 14/18] Add delete_broadcast() " Muhammad Rizki
2022-07-29  0:45 ` [PATCH v2 15/18] Create /add_bc bot command Muhammad Rizki
2022-07-29  0:45 ` Muhammad Rizki [this message]
2022-07-29  0:45 ` [PATCH v2 17/18] Add /del_bc " Muhammad Rizki
2022-07-29  0:45 ` [PATCH v2 18/18] Add del_chat callback query Muhammad Rizki
2022-07-29  0:57 ` [PATCH v2 00/18] New feature to handle atom URLs and broadcast Ammar Faizi
2022-07-29  1:01   ` Muhammad Rizki
2022-07-29  1:06     ` Ammar Faizi
  -- strict thread matches above, loose matches on Subject: below --
2022-07-29  0:40 [PATCH v1 " Muhammad Rizki
2022-07-29  0:40 ` [PATCH v2 16/18] Add create_chat_link() function Muhammad Rizki

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