The main task during development is a technical support bot. The user writes a problem to the telegram bot, which forwards it to a supergroup topic. Inside the topic, support discusses the problem and calls the reply function to send information to the user in private messages. It is not possible to correctly call the reply method, the bot, instead of responding to reply, simply forwards all messages from the topic to private messages. Example System like Livegram Bot.
async def handle_group_replies(update: Update, context: CallbackContext):
"""Handler message group"""
# Check message in group
if update.message.reply_to_message and update.message.chat_id == GROUP_CHAT_ID:
# Check message reply bot
if update.message.reply_to_message.from_user.id == context.bot.id:
# Take ID topic from message reply
thread_id = update.message.reply_to_message.message_thread_id
# Check topic in dictionary topic_to_user
if thread_id in topic_to_user:
user_id = topic_to_user[thread_id] # Take ID user from ID topic
# Type message
try:
if update.message.text:
await context.bot.send_message(
chat_id=user_id,
text=update.message.text
)
elif update.message.photo:
await context.bot.send_photo(
chat_id=user_id,
photo=update.message.photo[-1].file_id
)
elif update.message.document:
await context.bot.send_document(
chat_id=user_id,
document=update.message.document.file_id
)
elif update.message.audio:
await context.bot.send_audio(
chat_id=user_id,
audio=update.message.audio.file_id
)
elif update.message.video:
await context.bot.send_video(
chat_id=user_id,
video=update.message.video.file_id
)
elif update.message.voice:
await context.bot.send_voice(
chat_id=user_id,
voice=update.message.voice.file_id
)
elif update.message.sticker:
await context.bot.send_sticker(
chat_id=user_id,
sticker=update.message.sticker.file_id
)
logger.info(f"Reply from the group was sent to the user (ID: {user_id}).")
except Exception as e:
logger.error(f"Error sending response to user: {e}")
await update.message.reply_text("Failed to send message.")
else:
logger.warning(f"Topic with ID {thread_id} not found in topic_to_user.")
else:
logger.debug(f"The message is not a response to a bot message, ignore it.")
else:
logger.debug("The message is not a response or was not sent in the group, ignore it.")
At first I tried to work with text, but it turned out that the bot was sending None instead of ignoring it, so I added the appropriate check. Additionally, I tried to rewrite it through another library, but returned to python-telegram-bot. In other cases, the message was completely ignored, for example:
if update.message.reply_to_message and update.message.chat_id == GROUP_CHAT_ID:
thread_id = update.message.reply_to_message.message_thread_id
if thread_id in topic_to_user:
user_id = topic_to_user[thread_id]
original_message = update.message.reply_to_message
forward_message = f"{original_message.text}"
try:
await context.bot.send_message(chat_id=user_id, text=forward_message)