How to Reply Message in Unknown ChatBot Telegram with Python?

Question:

I have a ChatBot in Telegram that forwards message from others to me and I can reply their messages by replying messages in bot.
When I reply a message, the bot checks that message user id, and send my message to that id.
But for someone that make privacy and turned forwarded messages from Everybody to Nobody or My Contacts, the bot can’t get their user id (message.reply_to_message.forward_from.id).

Telegram Privacy Settings

import telebot
from api import bot_token, admin_id

key = bot_token
admin_id = admin_id
bot = telebot.TeleBot(key)
start_message = "Now, you cand send message"


@bot.message_handler(commands=['start'])
def start(message):
    sender_fn = message.from_user.first_name + ' '
    if message.from_user.last_name:
        sender_ln = message.from_user.last_name + ' '
    else:
        sender_ln = ""
    sender_id = str(message.from_user.id)
    bot.send_message(message.from_user.id, start_message)
    if message.from_user.username:
        sender_un = '@' + str(message.from_user.username) + ' '
        bot.send_message(admin_id, sender_fn + sender_ln + "Started Bot" + 'n'
                         + sender_un + '(' + sender_id + ')')
    else:
        bot.send_message(admin_id, sender_fn + sender_ln
                         + '(' + sender_id + ')' + " Started Bot")


@bot.message_handler()
def message(message):
    if message.from_user.id == admin_id:
        try:
            sender_id = message.reply_to_message.forward_from.id
            reply_message = message.text
            bot.send_message(sender_id, reply_message)
        except AttributeError:
            bot.send_message(admin_id, "Can't Chat")
    else:
        sender_id = message.from_user.id
        bot.forward_message(admin_id, message.chat.id, message.id)


print("Bot Started!")
bot.polling()

So, how can I send messages to unknown people by bot?!

I thought may fix the problem while we say to the bot to send message
(bot.send_message()) instead of forward (bot.forward_message()). but the bot can’t get the user id again. because hidden user messages don’t have the user id.

@bot.message_handler()
def message(message):
    if message.from_user.id == admin_id:
        try:
            sender_id = message.reply_to_message.forward_from.id
            reply_message = message.text
            bot.send_message(sender_id, reply_message)
        except AttributeError:
            bot.send_message(admin_id, "Can't Chat")
    else:
        sender_id = message.from_user.id
        bot.send_message(admin_id, message.text)

Telegram messages don’t have message.reply_to_message.forward_from.id for users with privacy.

Asked By: MaTiN

||

Answers:

So, I found the answer. I used json to solve the problem. First you should to import the json with import json. Then you can start this code to the start method:

userdata = {
    "id": sender_id,
    "username": sender_un,
    "firstname": sender_fn,
    "lastname": sender_ln
}
path = "./data/{}.json".format(sender_fn)
with open(path, "w") as f:
    json.dump(userdata, f)

And add this code to message method, under the except block, replace with bot.send_message(admin_id, "Can't Chat"):

sender_fn = str(message.reply_to_message.forward_sender_name)
with open("./data/{}.json".format(sender_fn), 'r') as f:
    user = json.load(f)
    bot.send_message(int(user["id"]), reply_message)

That was it. By doing this, the bot saved the users data when the user starts the bot, and then when you want to reply a message, if it couldn’t get the id, it will open the json and will use the id that saved before.
If you have any problem you can text me on telegram.

I have uploaded a completed version of this bot on my github account. You can reach me if you liked it.

Answered By: MaTiN