From 2c1430893bb9330436d12d5e1fb4257243c64fd6 Mon Sep 17 00:00:00 2001 From: Tony Pai Date: Sun, 10 Mar 2024 01:29:10 +0800 Subject: [PATCH] Add allowed_users feature thanks to @gianlucaalfa --- README.md | 4 +++- main.py | 12 +++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ac94e0e..6e252b4 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ An AI-powered text summarization Telegram bot that generates concise summaries o ## Usage -Launch a GPT-4 summary bot using OpenAI. +Launch a OpenAI GPT-4 summary bot that only can be used by your friend and you. ```sh docker run -d \ @@ -21,6 +21,7 @@ docker run -d \ -e OPENAI_API_KEY=$OPENAI_API_KEY \ -e TELEGRAM_TOKEN=$YOUR_TG_TOKEN \ -e TS_LANG=$YOUR_LANGUAGE \ + -e ALLOWED_USERS=, \ tonypai/summary-gpt-bot:latest ``` @@ -54,3 +55,4 @@ Bot Variables | LLM_MODEL | LLM Model to use for text summarization (default: gpt-3.5-turbo-16k) | | TELEGRAM_TOKEN | Token for Telegram API (required) | | TS_LANG | Language of the text to be summarized (default: Taiwanese Mandarin) | +| ALLOWED_USERS | You can get your own ID by asking to @myidbot (optional) | diff --git a/main.py b/main.py index d67c25b..88b3cf0 100644 --- a/main.py +++ b/main.py @@ -14,7 +14,8 @@ telegram_token = os.environ.get("TELEGRAM_TOKEN", "xxx") model = os.environ.get("LLM_MODEL", "gpt-3.5-turbo-16k") lang = os.environ.get("TS_LANG", "Taiwanese Mandarin") -chunk_size= int(os.environ.get("CHUNK_SIZE", 10000)) +chunk_size = int(os.environ.get("CHUNK_SIZE", 10000)) +allowed_users = os.environ.get("ALLOWED_USERS", "") def split_user_input(text): # Split the input text into paragraphs @@ -164,6 +165,13 @@ async def handle(command, update, context): chat_id = update.effective_chat.id print("chat_id=", chat_id) + if allowed_users: + user_ids = allowed_users.split(',') + if str(chat_id) not in user_ids: + print(chat_id, "is not allowed.") + await context.bot.send_message(chat_id=chat_id, text="You have no permission to use this bot.") + return + try: if command == 'start': await context.bot.send_message(chat_id=chat_id, text="I can summarize text, URLs, PDFs and YouTube video for you.") @@ -196,8 +204,6 @@ async def handle(command, update, context): text = page.extract_text() text_array.append(text) - print(file_path) - await context.bot.send_chat_action(chat_id=chat_id, action="TYPING") summary = summarize(text_array) await context.bot.send_message(chat_id=chat_id, text=f"{summary}", reply_to_message_id=update.message.message_id, reply_markup=get_inline_keyboard_buttons())