0

I need to create anonymous messages so that a person can send a question to the developer and he answered it.but I have an error, instead of responding, the bot simply does not send a message to the user

import telebot

bot = telebot.TeleBot('')
developer_chat_id = ''

@bot.message_handler(commands=['start'])
def start(message):
    bot.send_message(message.chat.id, 'Your answer.')

@bot.message_handler(func=lambda message: True)
def ask_developer(message):
    if str(message.chat.id) == developer_chat_id:
        bot.send_message(developer_chat_id, 'No you can't send a message to yourself')
    else:
        bot.send_message(developer_chat_id, f'User: {message.text}')

@bot.message_handler(func=lambda message: message.reply_to_message is not None and str(message.reply_to_message.chat.id) == developer_chat_id, content_types=['text'])
def answer_question(message):
    bot.send_message(message.reply_to_message.reply_to_message.chat.id, message.text)

bot.polling()

I expected the code to be correct, but instead of answering, the bot writes you can't send a message to yourself

CallMeStag
  • 5,467
  • 1
  • 7
  • 22
idfdf2
  • 1

1 Answers1

0

you can't make it anonymous

If you want to reply to a user you have to remember his id. That means you can't make it anonymous.

now about why you always receive you can't send a message to yourself

Your function answer_question never called because it is under ask_developer function that handles every text message. Telebot will check conditions (your lambda functions you made) from top to down and call first function to match.

To fix this you should move answer_question above ask_developer.

now why this will not work?

in the function ask_developer you send a message to a developer with no reply.

Trying to access message.reply_to_message.reply_to_message.chat in answer_question function will always throw an error because message.reply_to_message.reply_to_message is NONE.

how to make feedback system?

Simple way would be saving id of a user asked a question in pair with id of a message your bot sent to a developer.

Then, when a developer answers to a message, you can simply read id of a message from a bot, and with it determine receiver of this message.

To save id of the message you receive from bot:

msg = bot.send_message(developer_chat_id, f'User: {message.text}')
msg_id = msg.id

It all can be done with json library. Json usage:

import json

# sample dict
data = {"feedback": {"id_msg_from_bot": "id_of_user"}}

# save it to file
with open("data.json", "w") as f:
    json.dump(data, f)


# load it from file
with open("data.json") as f:
    d = json.load(f)

print(d)