pythonpy-telegram-bot-api

telegram bot breaks down when it is used by more than one person


After more than one person uses the bot, the error 2022-06-03 19:46:12,641 (init.py:648 MainThread) ERROR - TeleBot appears: "A request to the Telegram API was unsuccessful. Error code: 400. Description: Bad Request: message text is empty". I read a lot of things, but nothing helped

import telebot
from telebot import types
import gspread
from registration_sheet import gs, sh
from button import button1, button2, mm


token = 'token'
txt = open("C:/Users/nmash/Desktop/Cloud/terms_of_the_agreement.txt", encoding = "utf-8")

class Telegrambot():

    def __init__(self, token):
        super(Telegrambot, self).__init__()
        self.bot = telebot.TeleBot(token)
        self.message_handler()
    
        self.start()

    def start(self):
        self.bot.polling(none_stop=True)
    
    def message_handler(self):
        @self.bot.message_handler(content_types=['text'])

        def message_worker(message):            
        
            if message.text == '/start':
                button1()
                self.bot.send_message(message.from_user.id, txt.read(), reply_markup=mm)            
                            
                #self.bot.send_message(message.from_user.id, "Вы приняли условия соглашения✅", reply_markup=mm)
            if message.text == "Принять условия соглашения✅":
                self.bot.send_message(message.from_user.id, "Вы приняли условия соглашения✅", reply_markup=types.ReplyKeyboardRemove())
            
                self.registration(message)

    def registration(self, message):        
        def message_han(message):
            sent = self.bot.send_message(message.from_user.id, 'Введите своё имя:')
            self.bot.register_next_step_handler(sent, reg)

        def reg(message):           
            name = message.text             
    
            worksheet = sh.sheet1 
            values_list = worksheet.row_values(2)

            number = 0
            while True:
                n = number + 1
                number = n
                values_list = worksheet.row_values(number)
                if values_list == []:
                    userId = number
                    break

            worksheet.append_row([name, userId])

            self.bot.send_message(message.from_user.id,  "Ваше имя: " + name + "\nВаше ID: " + str(number))

        message_han(message)    

Telegrambot(token=token)

Solution

  • I am guessing that the problem hides here:

    txt = open("C:/Users/nmash/Desktop/Cloud/terms_of_the_agreement.txt", encoding = "utf-8")
    
    ...
    
            def message_worker(message):            
            
                if message.text == '/start':
                    button1()
                    self.bot.send_message(message.from_user.id, txt.read(), reply_markup=mm)
    

    Once the opened file is read (txt.read()), next reads for the next users will lead to empty results. One way to avoid that is to read your file in the very beginning an save the result in a variable and the use it as many times as needed.