Z
Z
Zpengineer2019-12-03 17:53:26
Python
Zpengineer, 2019-12-03 17:53:26

How to display the next list item on button click in Python Telegram Bot?

Good day. I started learning Python recently, I became interested in developing bots for Telegram. I am writing a bot for my needs. The bottom line is that the bot receives a Json object from the desired site, I enter the necessary elements into an array, and by pressing the button I display the data. How can I make it so that when the button is clicked again, the next element is displayed?

with urlopen("ссылка") as response:
        source = response.read()

data = json.loads(source)


i = 0
profile = []
while i <= 50:
    name = data[i]['display_name']
    age = data[i]['display_age']
    foto = data[i]['profile_images']['thumbnail_image_big']
    height = data[i]['height']
    weight = data[i]['weight']
    tags = data[i]['tags'][0]
    silka = data[i]['chat_url']
    info = name + age+ height + weight + tags
    i = i + 1
    profile.append(info)

@bot.message_handler(commands=['start'])
def startpg(message):
    startmenu = types.ReplyKeyboardMarkup(True, True)
    startmenu.row('Начать')
    bot.send_message(message.chat.id, 'Добро пожаловать!', reply_markup=startmenu)

@bot.message_handler(content_types=['text'])
def osnov(message):
    global keks
    keks = 'keks'

    if message.text == 'Начать':
        send = bot.send_message(message.chat.id, 'Введите своё имя:')
        bot.register_next_step_handler(send, next1)
    elif message.text == 'Текст 1':
        if keks == 'keks':
            knopki = types.InlineKeyboardMarkup()
            but_1 = types.InlineKeyboardButton(text='Выбрать еще', callback_data='next')
            but_2 = types.InlineKeyboardButton(text='Перейти', url=silka)
            knopki.add(but_1, but_2)
            bot.send_message(message.chat.id, info, parse_mode="HTML", reply_markup=knopki)


    elif message.text == 'Текст 2':
        if keks == 'keks':
            knopki = types.InlineKeyboardMarkup()
            but_1 = types.InlineKeyboardButton(text='Выбрать еще', callback_data='next')
            but_2 = types.InlineKeyboardButton(text='Перейти', url=silka)
            knopki.add(but_1, but_2)
            bot.send_message(message.chat.id, info, parse_mode="HTML", reply_markup=knopki)

def next1(message):
    send = bot.send_message(message.chat.id, 'Очень приятно {name}, введите ваш город'.format(name=message.text))
    bot.register_next_step_handler(send, next2)

def next2(message):
    global keks
    keks = 'keks'
    forwhat = types.ReplyKeyboardMarkup(True, False)
    forwhat.row('Текст 1')
    forwhat.row('Текст 2')
    bot.send_message(message.chat.id, 'текст'.format(sity=message.text), reply_markup=forwhat)

@bot.callback_query_handler(func=lambda c: True)
def inlin(c):

        if c.data == 'next':
            global keks
            keks = 'exit'
            knopki = types.InlineKeyboardMarkup()
            but_1 = types.InlineKeyboardButton(text='Выбрать еще', callback_data='next')
            but_2 = types.InlineKeyboardButton(text='Перейти', url=silka)
            knopki.add(but_1, but_2)
            bot.send_message(c.message.chat.id, profile, parse_mode="HTML", reply_markup=knopki)

bot.polling()

It is necessary to display the next profile when pressing the Select button, and the silka url should also increase. So far, I have managed to display 1 and 2 elements, and then I'm breaking my head)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question