M
M
me2021-01-24 22:06:05
Python
me, 2021-01-24 22:06:05

How to clear the message variable so that the bot doesn't loop?

Hello! I'm making a bot that will display data from the database by rowid in order of priority and convert them into a questionnaire type like a picture and a text below.

When you click on the "Search profiles" button, the bot drops the first profile from the database and adds +1 to counts, thereby I select the id from the list by this value and again select the necessary data for the user.

When you click on the "More" button, the bot loops and sends a message with the word "More" to the sendannk function and iterates through the profiles to the very end, then the profiles end and the bot goes to the main menu.

The problem is that every time I click on the “more” button, the bot shows the next profile and waits for the user to click “More” (if necessary)

How can I do all this?

I tried bot.register_next_step_handler(message,count, sendannk) , but I can't pass count as an argument, it throws an error TypeError: 'NoneType' object is not callable

Help, maybe you can do what you need in a different way

def search(message):

    keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True)
    keyboard.add(*[types.KeyboardButton(name) for name in ['Еще', 'Остановить']])
    bot.send_message(message.chat.id, "Сейчас поищем!", reply_markup=keyboard)
    count = 0
    sendannk(message,count)



def sendannk(message,count):
    try:
        ids = '12345678'
        with sq.connect('DATABASE.db') as con:
            cur = con.cursor() 
            cur.execute('''SELECT city FROM users WHERE userid = ?''', (ids,))
            datacit = cur.fetchall()
            datacit = datacit[0][0] ##город  юзера
            cur.execute('''SELECT rowid FROM users WHERE city = ? ''', (datacit,))
            usersrowid = cur.fetchall()
            
        lst = [] #rowid анкет по городу 
        
        for s in usersrowid:
            sstr = str(s)
            sssr = sstr.replace(')','').replace('(','').replace(',','')
            lst.append(sssr)
            
        cont = len(lst)
        rowidus = lst[count] 
        
        with sq.connect('DATABASE.db') as con:
            cur = con.cursor() 
            cur.execute('''SELECT name,old,city,photo,descr,cont FROM users WHERE rowid = ? ''',(rowidus,))
            data = cur.fetchall()

        dbdata = data[0]
        usercity = dbdata[0]   
        username = dbdata[1]
        userold = dbdata[2]
        userphoto = dbdata[3]   
        userdescr = dbdata[4]
        usercont = dbdata[5]
        caption = username + ', ' + userold + ', ' + usercity + '\n' + userdescr + '\n' + usercont
        
        
        bot.send_photo(message.chat.id, userphoto, caption)
        if message.text == 'Еще':
            count += 1
            sendannk(message,count)

    except:
        bot.send_message(message.chat.id, 'Анкеты кончились')
        start_message(message)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
soremix, 2021-01-24
@dabudi

Do not use such function calls, especially since the content in the message variable does not change in any way. Use https://github.com/eternnoir/pyTelegramBotAPI/blob... Let's say so
if message.text == 'Еще':

@bot.message_handler(commands=['search'])
def start_search(message):
    keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True)
    keyboard.add(*[types.KeyboardButton(name) for name in ['Еще', 'Остановить']])
    bot.send_message(message.chat.id, "Сейчас поищем!", reply_markup=keyboard)
    sendannk(message, 0)

def sendannk(message, count):
    if message.text == 'Остановить':
        print('stop')
    elif message.text == 'Еще' or count == 0: # Вместо count == 0 можно указать начальное сообщение, чтоб первый раз функция выполнилась, а не по колбэку, в этом случае было бы message.text == '/search', не увидил ничего криминального в каунт = 0 поэтому пусть будет
        try:
            ids = '12345678'
            with sq.connect('DATABASE.db') as con:
                cur = con.cursor() 
                cur.execute('''SELECT city FROM users WHERE userid = ?''', (ids,))
                datacit = cur.fetchall()
                datacit = datacit[0][0] ##город  юзера
                cur.execute('''SELECT rowid FROM users WHERE city = ? ''', (datacit,))
                usersrowid = cur.fetchall()
                
            lst = [] #rowid анкет по городу 
            
            for s in usersrowid:
                sstr = str(s)
                sssr = sstr.replace(')','').replace('(','').replace(',','')
                lst.append(sssr)
                
            cont = len(lst)
            rowidus = lst[count] 
            
            with sq.connect('DATABASE.db') as con:
                cur = con.cursor() 
                cur.execute('''SELECT name,old,city,photo,descr,cont FROM users WHERE rowid = ? ''',(rowidus,))
                data = cur.fetchall()

            dbdata = data[0]
            usercity = dbdata[0]   
            username = dbdata[1]
            userold = dbdata[2]
            userphoto = dbdata[3]   
            userdescr = dbdata[4]
            usercont = dbdata[5]
            caption = username + ', ' + userold + ', ' + usercity + '\n' + userdescr + '\n' + usercont
            
            
            bot.send_photo(message.chat.id, userphoto, caption)
            
            count += 1
            msg = bot.send_message(message.chat.id, "Искать еще?")
            bot.register_next_step_handler(msg, sendannk, count)

        except:
            bot.send_message(message.chat.id, 'Анкеты кончились')
            start_message(message)

through an additional function so as not to touch if in sendannk

@bot.message_handler(commands=['search'])
def start_search(message):
    keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True)
    keyboard.add(*[types.KeyboardButton(name) for name in ['Еще', 'Остановить']])
    bot.send_message(message.chat.id, "Сейчас поищем!", reply_markup=keyboard)
    sendannk(message, 0)


def step_handler(message, count=0):
    if message.text == 'Еще':
        sendannk(message, count)
    elif message.text == 'Остановить':
        print('stop')


def sendannk(message, count):
    try:
        
        ids = '12345678'
        with sq.connect('DATABASE.db') as con:
            cur = con.cursor() 
            cur.execute('''SELECT city FROM users WHERE userid = ?''', (ids,))
            datacit = cur.fetchall()
            datacit = datacit[0][0] ##город  юзера
            cur.execute('''SELECT rowid FROM users WHERE city = ? ''', (datacit,))
            usersrowid = cur.fetchall()
            
        lst = [] #rowid анкет по городу 
        
        for s in usersrowid:
            sstr = str(s)
            sssr = sstr.replace(')','').replace('(','').replace(',','')
            lst.append(sssr)
            
        cont = len(lst)
        rowidus = lst[count] 
        
        with sq.connect('DATABASE.db') as con:
            cur = con.cursor() 
            cur.execute('''SELECT name,old,city,photo,descr,cont FROM users WHERE rowid = ? ''',(rowidus,))
            data = cur.fetchall()

        dbdata = data[0]
        usercity = dbdata[0]   
        username = dbdata[1]
        userold = dbdata[2]
        userphoto = dbdata[3]   
        userdescr = dbdata[4]
        usercont = dbdata[5]
        caption = username + ', ' + userold + ', ' + usercity + '\n' + userdescr + '\n' + usercont
        
        
        bot.send_photo(message.chat.id, userphoto, caption)

        count += 1
        msg = bot.send_message(message.chat.id, "Искать еще?")
        bot.register_next_step_handler(msg, step_handler, count)

    except:
        bot.send_message(message.chat.id, 'Анкеты кончились')
        start_message(message)

D
Dmitry, 2017-06-17
@slo_nik

Good evening.
Error 404 indicates that the file was not found, in your case, most likely, the form handler file.
Check the path to the send.php file

N
Nurba Nurba, 2017-06-17
@nurba91

does the send.php file exist?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question