0

I am new to telegram bot development. I use python 3.9 My bot works fine when 1 person uses but 2 or more it gives me Error code: 400. Description: Bad Request: message to edit not found I use pytelegrambotapi and imported telebot Here is my code:

import telebot
@bot.message_handler(commands = ["start"])
def start_bot(message):
    global user
    #--------creating new user or load old user data--------
    user = USER(message.chat.id)
    #-----------asking-----------
    if user.isFull():
        user.last_message = bot.send_message(message.chat.id, 
                                             f"Hello {user.fish}", 
                                             reply_markup=MAIN_MENU)
    else:
        bot.delete_message(message.chat.id, message.message_id)
        bot.send_message(message.chat.id, "*Some post", reply_markup=None)
        user.info_post_id = bot.send_message(message.chat.id, user.user_info_checking()).message_id
        user.last_message = bot.send_message(message.chat.id, 'Name:')

@bot.message_handler(content_types=['text'])
def get_name_and_age(message):
    global user
    chat_id = message.chat.id
    if user.last_message.text=='Name:':
        user.fish = message.text
        bot.delete_message(chat_id, message.message_id)
        bot.edit_message_text(chat_id=chat_id,
                                message_id=user.info_post_id,
                                text=user.user_info_checking(),
                                reply_markup=None)
        user.last_message = bot.edit_message_text(chat_id=chat_id,
                                                    message_id=user.last_message.message_id,
                                                    text="Age:",
                                                    reply_markup=None)  

got this error

I need to keep my bot chat clean so I need to delete or edit messages but I got the error. I used user class to save induvidual message and its id to track but still failed:

    def __init__(self, id):
        self.id = id
        self.motion = None
        self.info_post_id = None
        self.last_message = None
        self.last_error = None
        self.post_counter = None
        self.current_post = None
        self.current_post_id = 0
        self.current_post_link = None
        self.current_catalog = None
        self.current_channel_id = None
        self.current_category = None
        self.current_sub_category = None
CallMeStag
  • 5,467
  • 1
  • 7
  • 22
  • I used `users={} @bot.message_handler(commands = ["start"]) def start_bot(message): global users chat_id = str(message.chat.id) print('bot started', chat_id) bot.delete_message(message.chat.id, message.message_id) users[chat_id] = USER(chat_id)` but my friend says if 1000 or more users use this bot and you write it in the object it is bad. What I should do then? And I do not know about webhook and polling it is a commercial bot it should take many requests. Any idea or tutorials to build a strong bot? – Avazbek Vaxobov Nov 04 '21 at 17:07

1 Answers1

0

The issue is likely the use of your global variable user. When two different users are using your bot at the same time, you write data from both of them into this object. Instead you do something like

user_info = defaultdict(USER)

...

def start_bot(message):
    global user_info 
    #--------creating new user or load old user data--------
    user = user_info[message.from_user.id]
    ...

Note that I used message.from_user.id rather then message.chat.id because for messages net in groups chats, message.chat will be the group chat, while message.from_user will be information about the user who sent the message.

It may be that pyTelegramBotAPI has some built-in mechanism for storing user-related data, but I'm not very familiar with that library …

CallMeStag
  • 5,467
  • 1
  • 7
  • 22