From 8776453ffd6a6cb51bae4fc933d0a7bd36cd3b42 Mon Sep 17 00:00:00 2001 From: rus-ai Date: Thu, 15 Jul 2021 23:11:32 +0300 Subject: [PATCH] Add support for restricted privacy user forwards --- README.md | 8 ++++++++ handlers.py | 36 ++++++++++++++++++++++++++++-------- settings.py | 2 ++ 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 157f805..bd4985e 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,14 @@ TELEGRAM_SUPPORT_CHAT_ID= # chat_id where the bot will forward all incoming mes # optional params HEROKU_APP_NAME= # name of your Heroku app for webhook setup WELCOME_MESSAGE= # text of a message that bot will write on /start command + +# If user don't allow forward his messages Bot adds his comment with thue user_id to reply +# Support team must reply to "bot reply", not to original user forwarded message +# Customize message for support team here: +REPLY_TO_THIS_MESSAGE=User above don't allow forward his messages. Reply to this message. +# If support reply to forwarded messages with hidded sender, bor warns with next error: +WRONG_REPLY=User above don't allow forward his messages. You must reply to bot reply under user forwarded message. + ``` ## Run bot locally diff --git a/handlers.py b/handlers.py index 0838e9e..b3d68fb 100644 --- a/handlers.py +++ b/handlers.py @@ -1,7 +1,8 @@ import os from telegram.ext import CommandHandler, MessageHandler, Filters -from settings import WELCOME_MESSAGE, TELEGRAM_SUPPORT_CHAT_ID +from settings import WELCOME_MESSAGE, TELEGRAM_SUPPORT_CHAT_ID, REPLY_TO_THIS_MESSAGE, WRONG_REPLY + def start(update, context): update.message.reply_text(WELCOME_MESSAGE) @@ -24,7 +25,13 @@ def forward_to_chat(update, context): 'text': 'TEST QOO', 'entities': [], 'caption_entities': [], 'photo': [], 'new_chat_members': [], 'new_chat_photo': [], 'delete_chat_photo': False, 'group_chat_created': False, 'supergroup_chat_created': False, 'channel_chat_created': False, 'from': {'id': 49820636, 'first_name': 'Daniil', 'is_bot': False, 'last_name': 'Okhlopkov', 'username': 'danokhlopkov', 'language_code': 'en'} }""" - update.message.forward(chat_id=TELEGRAM_SUPPORT_CHAT_ID) + forwarded = update.message.forward(chat_id=TELEGRAM_SUPPORT_CHAT_ID) + if not forwarded.forward_from: + context.bot.send_message( + chat_id=TELEGRAM_SUPPORT_CHAT_ID, + reply_to_message_id=forwarded.message_id, + text=f'{update.message.from_user.id}\n{REPLY_TO_THIS_MESSAGE}' + ) def forward_to_user(update, context): @@ -44,12 +51,25 @@ def forward_to_user(update, context): 'group_chat_created': False, 'supergroup_chat_created': False, 'channel_chat_created': False, 'from': {'id': 49820636, 'first_name': 'Daniil', 'is_bot': False, 'last_name': 'Okhlopkov', 'username': 'danokhlopkov', 'language_code': 'en'} }""" - user_id = update.message.reply_to_message.forward_from.id - context.bot.copy_message( - message_id=update.message.message_id, - chat_id=user_id, - from_chat_id=update.message.chat_id - ) + user_id = None + if update.message.reply_to_message.forward_from: + user_id = update.message.reply_to_message.forward_from.id + elif REPLY_TO_THIS_MESSAGE in update.message.reply_to_message.text: + try: + user_id = int(update.message.reply_to_message.text.split('\n')[0]) + except ValueError: + user_id = None + if user_id: + context.bot.copy_message( + message_id=update.message.message_id, + chat_id=user_id, + from_chat_id=update.message.chat_id + ) + else: + context.bot.send_message( + chat_id=TELEGRAM_SUPPORT_CHAT_ID, + text=WRONG_REPLY + ) def setup_dispatcher(dp): diff --git a/settings.py b/settings.py index 5abdb35..543cc41 100644 --- a/settings.py +++ b/settings.py @@ -18,3 +18,5 @@ WELCOME_MESSAGE = os.getenv("WELCOME_MESSAGE", "👋") +REPLY_TO_THIS_MESSAGE = os.getenv("REPLY_TO_THIS_MESSAGE", "REPLY_TO_THIS") +WRONG_REPLY = os.getenv("WRONG_REPLY", "WRONG_REPLY") \ No newline at end of file