Skip to content

Commit

Permalink
Merge pull request #7 from rus-ai/main
Browse files Browse the repository at this point in the history
Add support for restricted privacy user forwards
  • Loading branch information
ohld authored Aug 31, 2021
2 parents ab9638e + 8776453 commit 93782bf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 28 additions & 8 deletions handlers.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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):
Expand All @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit 93782bf

Please sign in to comment.